Perl: Difference between revisions
Line 601: | Line 601: | ||
else | else | ||
if ($password eq $guess) { | if ($password eq $guess) { | ||
print "Pass, friend.\n"; | |||
} else { | } else { | ||
die "Go away, imposter!\n"; | |||
} | } | ||
elseif | elseif | ||
if ( <condition 1> ) { | if ( <condition 1> ) { | ||
<action> | |||
} elsif ( <condition 2> ) { | } elsif ( <condition 2> ) { | ||
<second action> | |||
} elsif ( <condition 3> ) { | } elsif ( <condition 3> ) { | ||
... | ... | ||
} else { | } else { | ||
<if all else fails> | |||
} | } | ||
Revision as of 21:30, 24 June 2012
Reading
http://www.perl.org/books/beginning-perl/
Perl
Pathologically Eclectic Rubbish Lister
Larry Wall wrote it.
variables
the three types of variables in Perl are:
scalars = numbers, strings, or references denoted $ arrays = sequentially-numbered lists of scalars denoted @ hashes.= key-referenced lists of scalars denoted %
operators
Operator Num Str -------------------------------------- Equal == eq Not equal != ne Less than < lt Greater than > gt Less than or equal to <= le Greater than or equal to >= ge
man pages
perl - overview perlfaq - frequently asked questions perltoc - doc TOC perldata - data structures perlsyn - syntax perlop - operators and precedence perlrun - execution perlfunc - builtin functions perltrap - traps for the unwary perlstyle - style guide "perldoc" and "perldoc -f"
cpan
Comprehensive perl archive network
Perl kurs
Christer Ekholm First two letters and first letter of last name@init.se TIMTOWTDI (There Is More Than One Way To Do It)
Dictionary
regexp regular expression . a* a? ^a a$,perls RE för andra. semantik meaning that is expressed in a language and words notation ways to represent a mathematical equation analogi motsvarighet, nagot svart beskrivs enkelt iteration upprepning deklarera förklara definera definera subrutiner context sammanhang explicit uttryckligen implicit underförstått logiska operatorer and,or,not,||,&&,! aritmetisk +-/* bitoperatorer AND,OR,XOR,leftand right shift. re reguljära utryck, mönsterpassning. statement If functions are the verbs of Perl, then statements are the sentences. scalar literal constant varaiable that cant change interpolated Variable has been expanded Boolean operators The idea of combining values that represent truth and falsehood is called Boolean logic,after George Boole, who invented the concept in 1847, and we call the operators that do the combining 'Boolean operators'.
boolean operators
numeriska sträng == != eq ne < > lt gt <= >= le ge <=> cmp
comparison
#!/usr/bin/perl use warnings; print"Is two equal to four? ", 2 == 4, "\n"; print "OK, then, is six equal to six? ", 6 == 6, "\n"; print"So, two isn't equal to four? ", 2 != 4, "\n"; print"Five is more than six? ", 5 > 6, "\n"; print "Seven is less than sixteen? ", 7 < 16, "\n"; print "Two is equal to two? ", 2 == 2, "\n"; print "One is more than one? ", 1 > 1, "\n"; print "Six is not equal to seven? ", 6 != 7, "\n"; print"Seven is less than or equal to sixteen? ", 7 <= 16, "\n"; print "Two is more than or equal to two? ", 2 >= 2, "\n";
Is two equal to four? OK, then, is six equal to six? 1 So, two isn't equal to four? 1 Five is more than six? Seven is less than sixteen? 1 Two is equal to two? 1 One is more than one? Six is not equal to seven? 1 Seven is less than or equal to sixteen? 1 Two is more than or equal to two? 1
print "1 <= 0 => -1", "\n"; print "Compare six and nine? ", 6 <=> 9, "\n"; print "Compare seven and seven? ",7 <=> 7, "\n"; print "Compare eight and four? ", 8 <=> 4, "\n";
print 6 > 3 && 12 > 4, "\n"; # 1 print 9 > 7 || 6 > 8, "\n"; # 1 print !(2>3), "\n"; # 1 print !2>3, "\n"; # 0
How to get information
perldoc -f variabler Show help page for variables perldoc -f -X Show file handle test. perldoc perlform Format information perldoc Getopt::Long Show help for Getopt perldoc -q Letar i perl faq perldoc perlmodlib Lista inbyggda moduler perldoc perlpod
Debug
emacs M-x perldb Debugga perl i emacs perl -d:ptkdb /scrpt Debugga perl
Perl code
use strict; explesist namnge package variable use warning; show warning messages perl -w show warning messages use Getopt::Long: Use getoptions module
package mypkg; change package
Perl Oneliners:
perl -ne 'print if /a/' Grep all lines containing a. (-n create a loop) (-e, allows you to define Perl code to be executed by the compiler) perl -e 'print join("\n",@INC),"\n"' Var söker perl efter moduler perl -c kod.pl Kontrollera koden.
regex
. one key ^ beginning of line $ end of line /^[xfi]/ Starting with one x or f or i /^[h-j]/ Starting with one h or i or j () Deluttryck quantifier how much a string matches. ? Noll eller en gång a* a with 0 to unlimit a a+ one or more a {m} exackt m ggr {m,} minst m ggr {m,n} minst m max n ggr =~ m|^abc| matchar abc i början .+? matchar så lite som möjligt modifierare /i/i matchar i case insensitiv a.b a any key b ^a.c$ starting with a any key end with c X1* X1 folles by 0 to many 1 ^[]abc] strings starting with any of ]abc [^zxy] Does not have any of zxy in possition a{8} aaaaaaaa uttr{m,n} uttr förekommer mellan m och n gånger ^ *$ empty line or only blanks
Special meaning
$ skalär @ lista % hash, associativa vektorer $# maximun index in list $@ string that eval generates. \s remove space,tab,lf... \s+ godtyckligt \l Convert to lower case. $_ Default output input, the special $_ variable, which is often used in Perl functions as a 'default value'. $$ processid $? exit status @_ variable to send to subroutin @ARGV argument list. ('-v','-g','10','logfile') $|=1; dont flush output stty $"=','; dubbelfnuttade listor _ fil operatorer och stat kan återänväda stat förfrågan. $. Radräknare $1 Delutryck som matchade local $/ @INC directories to find per modules, add directory export PERL5LIB=/bin:/hej &sub subrutin $var2=\$var refere var2 to var @list('a',1,$a) add tree elements to list @list([@odd]) add copy of list called anonymous list $list[5] 6 possition in list @list = qw<Jan Feb>; Generate list $hash{'var'} element with index var in hash %hash{'a'}='5'; add key a value 5 if (exist($hash{a})){ run if key a exist i hash hash delete($hash{'a'}); delete key a fron hash split(' ',$str); split list output whith ' '
my lexikal variabel, lokal variabel $main::var main variabel
styrstrukturer
- iterationer
while [EXPR] BLOCK until [EXPR] BLOCK do BLOCK while [EXPR] do BLOCK until [EXPR] for VAR (EXPR) BLOCK for (EXPR1; EXPR2; EXPR3) BLOCK
- selection
if (EXPR) BLOCK if (EXPR) BLOCK1 elsif (EXPR2) BLOCK2... elxe BLOCKn unless (EXPR) BLOCK
filehandels
STDIN,STDOUT,STDERR
sträng list konverteringar
join (EXPR,LIST) @words = ("merry", "go", "round"); $str = join("-",@words);
split (REGEXP,EXPR) $str = "sys:x:3:3:sys:/dev:/bin/sh"; @fields = split (/:/, $str); for $rad ( @fields ){ print "$rad\n"; }
sub usage { print "use this command with optional -v" }
&subrutin(15,101,38) arguments are sent to a subroutin as a list
command options
use Getopt::Long; my %opts; GetOptions(\%opts,'v','g=i') || &usage; print "$opts{g}\n";
sub usage { print STDERR "Usage: option.pl [-v][-g n]"; exit 1; }
globala symboltabellen
foreach my $var ( keys %main::){ print "$var\n";
formatting.
my $rand1 = int(rand (90)); my $rand2 = int(rand (10)); my $rand3 = int(rand (50)); printf ("%6d %6d %6d\n",$rand1,$rand2,$rand3);
filehandlers
print "Who are you?\n"; my $name = <STDIN>; chomp($name); print "You are $name!!!\n";
print contents of file.
my $file = "/tmp/test"; open(IN,'<',$file) || die "open $file failed: $!"; # Loopa på rader i infilen. while ( <IN> ) { print $_; }
diamond operator @ARGV
#!/usr/bin/perl while (<>){ print; }
list files
opendir(DIR,"/etc"); my @files = sort grep(/x/,readdir(DIR)); closedir(DIR);
user external program
open(PING,'-|','/bin/ping -c 3 pizza'); while (defined(my $ping = <PING>)){ print "$ping"; } close PROG;
Backslash magic
\t tab (HT, TAB) \n newline (LF, NL) \r return (CR) \f form feed (FF) \a alarm (bell) (BEL) \e escape (think troff) (ESC) \033 octal char (think of a PDP-11) \x1B hex char \c[ control char \l lowercase next char (think vi) \u uppercase next char (think vi) \L lowercase till \E (think vi) \U uppercase till \E (think vi) \E end case modification (think vi) \Q quote (disable) pattern metacharacters till \E \w Match a "word" character (alphanumeric plus "_") \W Match a non-word character \s Match a whitespace character \S Match a non-whitespace character \d Match a digit character \D Match a non-digit character
keywords
Words that perl is already aware of are called keywords, and they come in several classes.
print is one example of the class called functions
functions
hex() Convert Hexadecimal to number oct() Convert Octal to number ord() character's value reverse () Reverse keypairs. keys () get key value from hash. sort () sort array values () which returns a list of all of the values in the hash each () which returns each hash entry as a key-value pair. chomp () remove final new line. length () length exists () if key exists in hash int(rand (90)) Generate a random number.
control keywords
There are also control keywords, such as if and else.
block
We can also group together a bunch of statements into a block – which is a bit like a paragraph – by
surrounding them with braces: {...}
number systems.
print 255, octal print 0377, binary print 0b11111111, hexadecimal print 0xFF,
Alternative Delimiters
the first acting like a single-quoted string and the second, like a double-quoted string.
q// and qq//,
here documents
print<<EOF; This is a here-document. It starts on the line after the two arrows, and it ends when the text following the arrows is found at the beginning of a line, like this: EOF
Write label with 'EOF' and her text will be single quoted.
arithmetic Operator
#!/usr/bin/perl use warnings; print "21 - 25 is: ", 25 - 21, "\n"; print "4 + 13 - 7 is: ", 4 + 13 - 7, "\n"; print "7 * 15 is ", 7 * 15, "\n"; print "249 / 3 is ", 249 / 3, "\n"; print 3 + 7 * 15, "\n"; print ((3 + 7) * 15);
21 - 25 is: 4 4 + 13 - 7 is: 10 7 * 15 is 105 249 / 3 is 83 108 150
lazy evaluation
Perl uses a technique called lazy evaluation. As soon as it knows the answer to the question, it stops working.
4 >= 2 and print "Four is more than or equal to two\n";
string operators
concatenation operator print "Print " . "one ". "string " . "here" . "\n"; repetition operator print "Andreas halfface! "x3, "\n"; # Andreas halfface! Andreas halfface! Andreas halfface!
precedence operators
-> ** ! ~ \ =~ !~ * / % x + - . << >> < > <= >= lt gt le ge == != <=> eq ne cmp & | ^ && || .. ... ?: , => not and or xor
Operating and Assigning at Once
Operations, like fetching a value, modifying it, or storing it, are very common, so there's a special syntax for them. Generally:
$a = $a <some operator> $b;
can be written as
$a <some operator>= $b;
$a = 6 * 9; print "Six nines are ", $a, "\n"; # Six nines are 54 $a += 3; print "Plus three is ", $a, "\n"; # Plus three is 57 $a /= 3; print "All over three is ", $a, "\n"; # All over three is 19
operator: Autoincrement and Autodecrement
++ and --
# First we set up our variables, giving the values 4 and 10 to $a and $b, respectively. : $a=4; $b=10; print "Our variables are ", $a, " and ", $b, "\n"; # Now in the following line, the assignment happens before the increment. So $b is set to $a's current value, 4 and then $a is autoincremented, becoming 5. $b=$a++; print "After incrementing, we have ", $a, " and ", $b, "\n"; # This time, however, the incrementing takes place first. $a is now 6, and $b is set to twice that, 12. $b=++$a*2; print "Now, we have ", $a, " and ", $b, "\n"; # Finally, $b is decremented first and becomes 11. $a is set to $b plus 4, which is 15. $a=--$b+4; print "Finally, we have ", $a, " and ", $b, "\n";
use strict
use strict to verify that we declare variables.
Lexical variable
my $variable;
global variable
our declare globals
standard input to scripts
print "Please enter something interesting\n"; $comment = <STDIN>;
array
array consistes of a list of elements
hash
hash. associative arrays look a bit like arrays where each element is associated with another value.
define hash alternatives
However, there's another trick. When your lists are made up purely from single words, you can specify them with the qw// operator. You can choose any paired brackets or non-word characters as your delimiters. The following lists are all identical:
('one', 'two', 'three', 'four') qw/one two three four/ qw(one two three four) qw<one two three four> qw{one two three four} qw[one two three four] qw|one two three four|
defining word from hash
[-1] # Count from backward from end. (19, 68, 47, 60, 53, 51, 58, 55, 47)[4, 5, 6] # list slice, Define words from list.
list allowed on left side of assignment operator.Right hand sid is build up first
($mone, $mtwo) = (1, 3);
$mone is set to 1, and $mtwo to 3. But how does this work? Perl allows lists on the left-hand side of an assignment operator – we say that lists are legal lvalues. When we assign one list to another, the right-hand list is built up first. Then perl assigns each element in turn, from the right hand side of the statement to the left. So 1 is assigned to $mone, and then 3 is assigned to $mtwo.
define series of elements to list
(1 .. 6) The right-hand number must, however, be higher than the left-hand one, s print "Counting up: ", (1 .. 6), "\n"; # Counting up: 123456 print "Counting down: ", reverse(1 .. 6), "\n"; # Counting down: 654321
stringifying
Forcing variables to make sense in a string is called stringifying them.
scalar context & list context
print @array1; $scalar1 = @array1;
- The first line is in list context. In list context, an array returns the list of its elements.
- In the second line however, the assignment wants to see a single result, or scalar value, and therefore it is in scalar context. In scalar context an array returns the number of its elements.
change context
We can force something to be in scalar context when it expects to be in list context by using the scalar operator.
print @array1; print scalar @array1;
Do you want a scalar or array
my @days = qw(Monday Tuesday Wednesday Thursday Friday Saturday Sunday); print "@days[1, 3, 5]", "\n"; # Tuesday Thursday Saturday print scalar $days[3], "\n"; # Thursday print "@days[0..3]", "\n"; # Monday Tuesday Wednesday Thursday print "$days[6]", "\n"; # Sunday
The prime rule is this: the prefix represents what you want to get, not what you've got. So @ represents a list of values, and $ represents a single scalar. Hence, when we're getting a single scalar from an array, we never prefix the variable with @ – that would mean a list. A single scalar is always prefixed with a $.
array index/array subscript
[3] We call the number in the square brackets the array index or array subscript.
for loop
my @array = qw(America Asia Europe Africa); my $element; for $element (@array) { print $element, "\n"; }
for loop example
my @array=(10, 20, 30, 40); print "Before: @array\n"; for (@array) { $_ *= 2 } print "After: @array\n";
The block must start with an opening brace and end with a closing brace, and the list or array that we're running over must be surrounded by parentheses. If we don't supply an iterator variable of our own, perl uses the special $_ variable, which is often used in Perl functions as a 'default value'. Note that the for loop doesn't require a semicolon after the block. So, when processing a for loop, perl makes the iterator a copy of each element of the list or array in turn, and then runs the block. If the block happens to change the value of the iterator, the corresponding array element changes as well. We can double each element of an array like this:
push/pop
pop We use pop @array to remove the top element from the array and it returns that element push @array, $scalar will add the scalar onto the top of the stack. # push @pileofpaper, "leaflet", "bank statement";
Shift and Unshift
First we unshift() an element onto the array, and the element appears at the beginning of the list. We then use shift to take off the first element, ignoring what it was.
sort
my @unsorted = (1, 2, 11, 24, 3, 36, 40, 4); my @string = sort { $a cmp $b } @unsorted; print "String sort: @string\n"; my @number = sort { $a <=> $b } @unsorted; print "Numeric sort: @number\n";
String sort: 1 11 2 24 3 36 4 40 Numeric sort: 1 2 3 4 11 24 36 40
Hashes
Define a hash.
my %where=( Gary => "Dallas", Lucy => "Exeter", Ian => "Reading", Samantha => "Oregon" );
If we want to make the relationship a little clearer, as well as highlighting the fact that we're dealing with a hash, we can use the => operator. the => operator acts like a 'quoting comma'. Essentially, it's a comma, but whatever appears on the left hand side of it is treated as a double-quoted string:
# Add to hash $where{Eva} = "Uxbridge";
# Delete from hash delete $where{Lucy};
convert array to hash
Because hashes and arrays are both built from structures that look like lists, you can convert between them, from array to hash like this:
@array = qw(Gary Dallas Lucy Exeter Ian Reading Samantha Oregon); %where = @array;
And then back to an array, like so:
@array = %where;
lookup value in hash
"Gary lives in ", $where{Gary}, "\n";
iterating hashes
This gives us a list of the keys (all of the scalars on the left-hand
keys (%hash).
This will iterate on all hash keys.
my %where=( Gary => "Dallas", Lucy => "Exeter", Ian => "Reading", Samantha => "Oregon" ); for (keys %where) { print "$_ lives in $where{$_}\n"; }
if
if ( <some test> ) { <do something> }
If key does not exist exit.
if (not exists $rates{$to}) { die "I don't know anything about $to as a currency\n"; }
conditions
An empty string, "", is false. The number zero and the string "0" are both false. An empty list, (), is false. The undefined value is false. Everything else is true.
tests
if variable is definee
if (defined $a) { print "\$a has a value.\n"; }
Logical Conjunctions
Join together several tests into one, by the use of the logical operators. Here's a summary of those:
$a and $b True if both $a and $b are true. $a or $b True if either of $a or $b, or both are true. not $a True if $a is not true.
if (not exists $rates{$to})
unless (exists $rates{$to}) { die "I don't know anything about {$to} as a currency\n"; }
Multiple Choice
else
if ($password eq $guess) { print "Pass, friend.\n"; } else { die "Go away, imposter!\n"; }
elseif
if ( <condition 1> ) { <action> } elsif ( <condition 2> ) { <second action> } elsif ( <condition 3> ) { ... } else { <if all else fails> }