Perl: Difference between revisions
(→Debug) |
|||
(57 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
Pathologically Eclectic Rubbish Lister | Pathologically Eclectic Rubbish Lister | ||
Larry Wall wrote it. | Larry Wall wrote it. | ||
==useful commands== | |||
scalar localtime # Generate date | |||
=remove linefeed on last line= | |||
perl -pe 'chomp if eof' | |||
=convert epoch unix time to iso date= | |||
perl -ne 'use POSIX; s/([\d.]+)/strftime "%Y-%m-%d %H:%M:%S", localtime $1/e,print if /./' | |||
==variables== | ==variables== | ||
Line 12: | Line 19: | ||
hashes.= key-referenced lists of scalars denoted % | hashes.= key-referenced lists of scalars denoted % | ||
==operators== | ==boolean operators== | ||
Operator Num Str | Operator Num Str | ||
-------------------------------------- | -------------------------------------- | ||
Line 59: | Line 66: | ||
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 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'. | ||
== | ==functions (Commands operator)== | ||
rename($target,$target.".old") # Rename a file. | |||
glob("*l"); # List all files ending with l | |||
print $_, " " x (30-length($_)); # The length of the file name plus the number of spaces will always add up to thirty, so we have nicely arranged columns | |||
==comparison== | ==comparison== | ||
Line 112: | Line 117: | ||
==Debug== | ==Debug== | ||
emacs M-x perldb Debugga perl i emacs | emacs M-x perldb Debugga perl i emacs | ||
perl -d:ptkdb /scrpt Debugga perl | perl -d:ptkdb /scrpt Debugga perl | ||
perl -d:Trace # yum install perl-Devel-Trace | |||
http://www.drdobbs.com/using-the-perl-debugger/184404744 | |||
==Perl code== | ==Perl code== | ||
Line 121: | Line 128: | ||
package mypkg; change package | package mypkg; change package | ||
==use base== | |||
use base qw(Mother Father); | |||
This pragma lets a programmer conveniently declare a derived class based upon the listed parent classes. The declaration above is roughly equivalent to: | |||
BEGIN { | |||
require Mother; | |||
require Father; | |||
push @ISA, qw(Mother Father); | |||
} | |||
==Perl Oneliners:== | ==Perl Oneliners:== | ||
Line 141: | Line 159: | ||
{m,} minst m ggr | {m,} minst m ggr | ||
{m,n} minst m max n ggr | {m,n} minst m max n ggr | ||
=~ m|^abc| | =~ m|^abc| match with alternate delimiter a line starting with abc. | ||
=~ Match regexp. If pattern found return 1 else undefined. | =~ Match regexp. If pattern found return 1 else undefined. | ||
!~ Match regexp. If pattern is not found return 1 else undefined. | !~ Match regexp. If pattern is not found return 1 else undefined. | ||
.+? matchar så lite som möjligt | .+? matchar så lite som möjligt | ||
==regexp modifiers== | ==regexp modifiers== | ||
/i/i matchar i case insensitiv | /i/i matchar i case insensitiv | ||
Line 206: | Line 225: | ||
if (EXPR) BLOCK1 elsif (EXPR2) BLOCK2... elxe BLOCKn | if (EXPR) BLOCK1 elsif (EXPR2) BLOCK2... elxe BLOCKn | ||
unless (EXPR) BLOCK | unless (EXPR) BLOCK | ||
==sträng list konverteringar== | ==sträng list konverteringar== | ||
Line 254: | Line 270: | ||
chomp($name); | chomp($name); | ||
print "You are $name!!!\n"; | print "You are $name!!!\n"; | ||
STDIN # Standard in | |||
STDOUT # Standard out | |||
STDERR # Standard error | |||
==Open a file handle for reading== | |||
open FH, 'filename.txt' or die $!; # Open filehandle for reading. | |||
special variables, designed to give you a way of getting at various things that Perl wants to tell you. In this case, Perl is passing on an error message from the system, | |||
==Open file handle for writing== | |||
open FH, "> $filename" or die $!; | |||
==append to existing file== | |||
open FH, ">> $filename" or die $!; | |||
==open file for reading== | |||
open FH, "< $filename"; | |||
==open directory== | |||
opendir DH, "." or die "Couldn't open the current directory: $!"; | |||
Now to read each file in the directory, we use readdir on the directory handle: | |||
==print contents of file.== | ==print contents of file.== | ||
Line 267: | Line 300: | ||
If @ARGV has no elements, then the diamond operator will read from STDIN-either from the keyboard or from a redirected file. | If @ARGV has no elements, then the diamond operator will read from STDIN-either from the keyboard or from a redirected file. | ||
#!/usr/bin/perl | #!/usr/bin/perl | ||
# while (<ARGV>) # Could be abbreviated to <> | |||
while (<>){ | while (<>){ | ||
print; | print; | ||
} | } | ||
This <...> construction is called the diamond operator, or readline operator: | |||
==list files== | ==list files== | ||
Line 372: | Line 408: | ||
concatenation operator print "Print " . "one ". "string " . "here" . "\n"; | concatenation operator print "Print " . "one ". "string " . "here" . "\n"; | ||
repetition operator print "Andreas halfface! "x3, "\n"; # Andreas halfface! Andreas halfface! Andreas halfface! | repetition operator print "Andreas halfface! "x3, "\n"; # Andreas halfface! Andreas halfface! Andreas halfface! | ||
1c # print lc("FuNnY StRiNg"); # convert to lower case. | |||
uc # print uc("FuNnY StRiNg"); # convert to lower case. | |||
==precedence operators== | ==precedence operators== | ||
-> | -> | ||
Line 654: | Line 693: | ||
</nowiki></pre> | </nowiki></pre> | ||
==regexp matching== | ==regexp matching== | ||
#The regular expression engine starts as soon as it can, grabs as much as it can, then tries to finish as soon as it can, while taking the first decision available to it. | |||
'ye(s|t)' The text matches the pattern 'ye(s|t)' | 'ye(s|t)' The text matches the pattern 'ye(s|t)' | ||
(andreas)? Match andreas or nothing. | (andreas)? Match andreas or nothing. | ||
Line 665: | Line 705: | ||
{,3} '3 or fewer' | {,3} '3 or fewer' | ||
{9} Match 9 times. | {9} Match 9 times. | ||
==backreference== | |||
Each time it sees a set of parentheses, it copies the matched text inside into a numbered variable – | |||
the first matched group goes in $1, the second group in $2, and so on. By looking at these variables, which we call the backreference variables, | |||
==greedy== | |||
(.*?) Search for any key zero or more times. Match as little as possible.(non greedy) | |||
==substitution== | |||
s/regexp/substitute/ Substitute first occurance of regexp with substitute | |||
s/regexp/substitute/g Substitute regexp with substitute globally. | |||
s/(\w+)\s+(\w+)/$2 $1/; Substitute with backreference. Swap the first two words. | |||
s{old text}{new text}g; Changed delimiters. Pair delimiters. | |||
s{old text} | |||
{new text}g; You can have new line or space between delimiters. | |||
==regexp modifiers== | |||
/i Case insensitive | |||
/m treat the string as multiple lines. | |||
/s treat the string as a single line. | |||
/g match multiple times. | |||
/x allow the use of whitespace and comments inside a match. | |||
==split== | |||
my @fields = split /:/, $passwd; Create array with each field of passwd | |||
==join== | |||
my $passwd2 = join "#", @fields; | |||
==transliteration== | |||
$string =~ tr/0-9/a-j/; "2011064" into "cabbage" | |||
$string =~ tr/ //d; Remove spaces. | |||
==inline modifiers== | |||
===inline comments=== | |||
(?#) Example: /^Today's (?# This is ignored, by the way)date:/ | |||
(?i)one way to do it! | |||
===inline case insensetive=== | |||
(?i)one way to do it! | |||
===Disable backreference=== | |||
/(?:X-)?Topic: (\w+)/; # Disable first back reference. | |||
===Lookaheads and Lookbehinds=== | |||
s/fish cake/cream cake/ # These do the same thing. | |||
/fish(?= cake)/ # These do the same thing. | |||
==input record separator== | |||
$/ is called the 'input record separator' | |||
$/ = "\n%\n"; # Record separator is now an empty %. | |||
$/ = undef; # Undefine input record separator. | |||
What the star – or, to give it its proper name, the glob – does is actually very subtle: | |||
==glob *== | |||
*a = *b makes everything called a – that is $a, @a, %a, and the filehandle called a – | |||
into an alias for everything called b. This is more than just setting them to the same | |||
value – it makes them the same thing | |||
==select== | |||
select FILEHANDLE; # change the default filehandle | |||
==buffering== | |||
$| = 1; # Perl turns off buffering for the currently selected filehandle. | |||
==Opening Pipes== | |||
open LYNX, "lynx –source http://www.perl.com/ |" or die "Can't open lynx: $!"; # The pipe symbol | at the end of the string tells Perl to run the command and collect the output. | |||
==file tests== | |||
if (-e "somefile.dat") {...} # If file exists. | |||
-e True if the file exists. | |||
-f True if the file is a plain file – not a directory. | |||
-d True if the file is a directory. | |||
-z True if the file has zero size. | |||
-s True if the file has nonzero size – returns size of file in bytes. | |||
-r True if the file is readable by you. | |||
-w True if the file is writable by you. | |||
-x True if the file is executable by you. | |||
-o True if the file is owned by you. | |||
==anonymous references== | |||
Anonymous References | |||
To get an array reference instead of an array, use square brackets [] instead of parentheses. | |||
To get a hash reference instead of a hash, use curly braces {} instead of parentheses. | |||
So, referring to our examples above, instead of doing this: | |||
my @array = (1, 2, 3, 4, 5); | |||
my $array_r = \@array; | |||
we can go straight to an array reference like this: | |||
my $array_r = [1, 2, 3, 4, 5]; | |||
Likewise, to get a hash reference, instead of doing this: | |||
my %hash = ( apple => "pomme", pear => "poire" ); | |||
my $hash_r = \%hash; | |||
we say: | |||
my $hash_r = { apple => "pomme", pear => "poire" }; | |||
my @array = ( 100,200,[ 2,4,[ 1,2,[ 10,20,30,40,50 ],3,4 ],6,8 ],300,400 ); | |||
==notation shorthands== | |||
Instead of ${$ref}, we can say $ref-> | |||
==matrices== | |||
$array[$row]->[$column] # chessboard matrics | |||
$array[0]->[0] # First row first column. | |||
==Autovivification== | |||
If we assign values to a reference, perl will automatically create all appropriate references necessary to make it work. | |||
my $ref; | |||
$ref->{UK}->{England}->{Oxford}->[1999]->{Population} = 500000; | |||
Is equal to making. | |||
$ref = {}; | |||
$ref->{UK} = {}; | |||
$ref->{UK}->{England} = {}; | |||
$ref->{UK}->{England}->{Oxford} = []; | |||
$ref->{UK}->{England}->{Oxford}->[1999] = {}; | |||
$ref->{UK}->{England}->{Oxford}->[1999]->{Population} = 500000; | |||
==links== | ==links== | ||
[[Category:Applications]] | [[Category:Applications]] | ||
[[Category:Unix]] | [[Category:Unix]] |
Latest revision as of 21:11, 25 October 2017
Reading
http://www.perl.org/books/beginning-perl/
Perl
Pathologically Eclectic Rubbish Lister
Larry Wall wrote it.
useful commands
scalar localtime # Generate date
remove linefeed on last line
perl -pe 'chomp if eof'
convert epoch unix time to iso date
perl -ne 'use POSIX; s/([\d.]+)/strftime "%Y-%m-%d %H:%M:%S", localtime $1/e,print if /./'
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 %
boolean 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'.
functions (Commands operator)
rename($target,$target.".old") # Rename a file. glob("*l"); # List all files ending with l print $_, " " x (30-length($_)); # The length of the file name plus the number of spaces will always add up to thirty, so we have nicely arranged columns
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 -d:Trace # yum install perl-Devel-Trace http://www.drdobbs.com/using-the-perl-debugger/184404744
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
use base
use base qw(Mother Father);
This pragma lets a programmer conveniently declare a derived class based upon the listed parent classes. The declaration above is roughly equivalent to:
BEGIN { require Mother; require Father; push @ISA, qw(Mother Father); }
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| match with alternate delimiter a line starting with abc. =~ Match regexp. If pattern found return 1 else undefined. !~ Match regexp. If pattern is not found return 1 else undefined. .+? matchar så lite som möjligt
regexp modifiers
/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
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";
STDIN # Standard in STDOUT # Standard out STDERR # Standard error
Open a file handle for reading
open FH, 'filename.txt' or die $!; # Open filehandle for reading.
special variables, designed to give you a way of getting at various things that Perl wants to tell you. In this case, Perl is passing on an error message from the system,
Open file handle for writing
open FH, "> $filename" or die $!;
append to existing file
open FH, ">> $filename" or die $!;
open file for reading
open FH, "< $filename";
open directory
opendir DH, "." or die "Couldn't open the current directory: $!";
Now to read each file in the directory, we use readdir on the directory handle:
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
If no file handle is used with the diamond operator, Perl will examine the @ARGV special variable. If @ARGV has no elements, then the diamond operator will read from STDIN-either from the keyboard or from a redirected file. #!/usr/bin/perl # while (<ARGV>) # Could be abbreviated to <> while (<>){ print; }
This <...> construction is called the diamond operator, or readline operator:
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 /\Q$pattern\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! 1c # print lc("FuNnY StRiNg"); # convert to lower case. uc # print uc("FuNnY StRiNg"); # convert to lower case.
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(foreach)
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})
# Another way of saying if not exist 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> }
last
The keyword last in the body will break out of the loop.
next
If you want to skip the rest of the processing of the body, but don't want to exit the loop, you can use next
label
A label goes before the for, while, or until of a loop, and ends with a colon. OUTER: while (<STDIN>) { chomp; INNER: for my $check (@getout) { last if $check eq $_; }
metacharacters
. * ? + [ ] ( ) { } ^ $ | \
character classes
\d [0-9] Digits 0 to 9. \w [0-9A-Za-z_] A 'word' character allowable in a Perl variable name. \s [ \t\n\r] A whitespace character that is, a space, a tab, a newline or a return. \D [^0-9] Any non-digit. \W [^0-9A-Za-z_] A non-'word' character. \S [^ \t\n\r] A non-blank character. \b Wordboundry
posix
[[:alpha:]] [a-zA-Z] An alphabetic character. [[:alnum:]] [0-9A-Za-z] An alphabetic or numeric character. [[:digit:]] \d A digit, 0-9. [[:lower:]] [a-z] A lower case letter. [[:upper:]] [A-Z] An upper case letter. [[:punct:]] [!"#$%&'()*+,-./:;<=>?@\[\\\]^_`{|}~] A punctuation character – note the escaped characters [, \, and ]
regexp matching
- The regular expression engine starts as soon as it can, grabs as much as it can, then tries to finish as soon as it can, while taking the first decision available to it.
'ye(s|t)' The text matches the pattern 'ye(s|t)' (andreas)? Match andreas or nothing.
quantifiers
? One or none. + One or more. * Zero or unlimited \s{2,3} Space 2 to 3 times. {2,} '2 or more' {,3} '3 or fewer' {9} Match 9 times.
backreference
Each time it sees a set of parentheses, it copies the matched text inside into a numbered variable – the first matched group goes in $1, the second group in $2, and so on. By looking at these variables, which we call the backreference variables,
greedy
(.*?) Search for any key zero or more times. Match as little as possible.(non greedy)
substitution
s/regexp/substitute/ Substitute first occurance of regexp with substitute s/regexp/substitute/g Substitute regexp with substitute globally. s/(\w+)\s+(\w+)/$2 $1/; Substitute with backreference. Swap the first two words. s{old text}{new text}g; Changed delimiters. Pair delimiters. s{old text} {new text}g; You can have new line or space between delimiters.
regexp modifiers
/i Case insensitive /m treat the string as multiple lines. /s treat the string as a single line. /g match multiple times. /x allow the use of whitespace and comments inside a match.
split
my @fields = split /:/, $passwd; Create array with each field of passwd
join
my $passwd2 = join "#", @fields;
transliteration
$string =~ tr/0-9/a-j/; "2011064" into "cabbage" $string =~ tr/ //d; Remove spaces.
inline modifiers
inline comments
(?#) Example: /^Today's (?# This is ignored, by the way)date:/ (?i)one way to do it!
inline case insensetive
(?i)one way to do it!
Disable backreference
/(?:X-)?Topic: (\w+)/; # Disable first back reference.
Lookaheads and Lookbehinds
s/fish cake/cream cake/ # These do the same thing. /fish(?= cake)/ # These do the same thing.
input record separator
$/ is called the 'input record separator' $/ = "\n%\n"; # Record separator is now an empty %. $/ = undef; # Undefine input record separator. What the star – or, to give it its proper name, the glob – does is actually very subtle:
glob *
*a = *b makes everything called a – that is $a, @a, %a, and the filehandle called a – into an alias for everything called b. This is more than just setting them to the same value – it makes them the same thing
select
select FILEHANDLE; # change the default filehandle
buffering
$| = 1; # Perl turns off buffering for the currently selected filehandle.
Opening Pipes
open LYNX, "lynx –source http://www.perl.com/ |" or die "Can't open lynx: $!"; # The pipe symbol | at the end of the string tells Perl to run the command and collect the output.
file tests
if (-e "somefile.dat") {...} # If file exists.
-e True if the file exists. -f True if the file is a plain file – not a directory. -d True if the file is a directory. -z True if the file has zero size. -s True if the file has nonzero size – returns size of file in bytes. -r True if the file is readable by you. -w True if the file is writable by you. -x True if the file is executable by you. -o True if the file is owned by you.
anonymous references
Anonymous References To get an array reference instead of an array, use square brackets [] instead of parentheses. To get a hash reference instead of a hash, use curly braces {} instead of parentheses.
So, referring to our examples above, instead of doing this:
my @array = (1, 2, 3, 4, 5); my $array_r = \@array;
we can go straight to an array reference like this:
my $array_r = [1, 2, 3, 4, 5];
Likewise, to get a hash reference, instead of doing this:
my %hash = ( apple => "pomme", pear => "poire" ); my $hash_r = \%hash;
we say:
my $hash_r = { apple => "pomme", pear => "poire" };
my @array = ( 100,200,[ 2,4,[ 1,2,[ 10,20,30,40,50 ],3,4 ],6,8 ],300,400 );
notation shorthands
Instead of ${$ref}, we can say $ref->
matrices
$array[$row]->[$column] # chessboard matrics $array[0]->[0] # First row first column.
Autovivification
If we assign values to a reference, perl will automatically create all appropriate references necessary to make it work.
my $ref; $ref->{UK}->{England}->{Oxford}->[1999]->{Population} = 500000;
Is equal to making.
$ref = {}; $ref->{UK} = {}; $ref->{UK}->{England} = {}; $ref->{UK}->{England}->{Oxford} = []; $ref->{UK}->{England}->{Oxford}->[1999] = {}; $ref->{UK}->{England}->{Oxford}->[1999]->{Population} = 500000;