Page 1
VI, October 2006
Practical Extraction and Report Language « Perl is a language of getting your job done » Larry Wall « There is more than one way to do it »
Page 2
VI, October 2006
Practical Extraction and Report Language Perl is a language of - - PDF document
Practical Extraction and Report Language Perl is a language of getting your job done There is more than one way to do it Larry Wall VI, October 2006 Page 1 Perl Outline : History Structure of a simple Perl script Perl variables
Page 1
VI, October 2006
Page 2
VI, October 2006
Page 3
VI, October 2006
Page 4
VI, October 2006
Page 5
VI, October 2006
Page 6
VI, October 2006
Page 7
VI, October 2006
Page 8
VI, October 2006
pcX: vioannid$ which perl /usr/bin/perl pcY: vioannid$ which perl /usr/local/bin/perl
#!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world" print "Hello world" ; #tell the program to exit exit ;
Page 9
VI, October 2006
computerX: vioannid$ which perl /usr/bin/perl computerY: vioannid$ which perl /usr/local/bin/perl #!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world" print "Hello world" ; #tell the program to exit exit ;
[embnet01@bc2-linux3 ~]$ which perl /import/bc2/soft/bin/perl5/perl #!/import/bc2/soft/bin/perl5/perl Page 10
VI, October 2006
#!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world " print "Hello world" ; #tell the program to exit exit ;
Page 11
VI, October 2006
#!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world" print "Hello world" ; #tell the program to exit exit ;
Page 12
VI, October 2006
#!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world" print "Hello world" ; #tell the program to exit exit ;
Page 13
VI, October 2006
#!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world" print "Hello world" ; #tell the program to exit exit ;
Page 14
VI, October 2006
#!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world" print "Hello world" ; #tell the program to exit exit ;
Page 15
VI, October 2006
#!/usr/local/bin/perl use strict; use warnings; #tell the program to print "Hello world" print "Hello world" ; #tell the program to exit exit ;
vioannid$ ./hello.pl Hello worldvioannid$ vioannid$ chmod a+x hello.pl Page 16
VI, October 2006
#!/usr/local/bin/perl use strict; use warnings; #play with the print statement #words separated by newline print "Hello\nworld\n" ; #words separated by tabs & a final newline print "Hello\tworld\n" ; #usage of the period to cat strings print "Hello"."world"."\n"; #tell the program to exit exit ; vioannid$ ./hello2.pl Hello world Hello world Helloworld vioannid$
Page 17
VI, October 2006
Page 18
VI, October 2006
my $variable_1 = "Hello world !\n"; #note the quotes my $variable_two = 30; #note the absence of quotes
Page 19
VI, October 2006
my $pi = 3.14159; # floating point number my $avogadro = 6.02e23; # scientific notation my $cash = 33651235421; # huge integer my $cash2 = 33_651_235_421; # huge integer with underlines for visibility my $result = 25; # integer
1 + 1 # 1 plus 1, or 2 2.5 - 1.5 # 2.5 minus 1.5, or 1 2 * 6 # 2 times 6, or 12 14 / 2 # 14 divided by 2, or 7 10.2 / 3.14159 # 10.2 divided by 3.14159, or 3.24676358149854 10 % 3 # 10 modulo 3, or 1 10 / 3 # 10 divided by 3, or 3.333333333 (ALWAYS floating-point) 2 ** 3 # 2 to the power 3, or 8 my $total = $result + $cash2; # $total has now the value: 33651235446 Page 20
VI, October 2006
#no variable and backslash interpolation my $name = 'Vassilios'; #literal string my $hello = 'hello\tsir!'; #if printed, outputs: hello\tsir my $cost = 'The meeting costs $100' ; #if printed, outputs: The meeting costs $100 my $good_guy = '$name is a good guy!' #if printed, outputs: $name is a good guy!
#variable and backslash interpolation my $hello = "hello\tsir!"; #if printed, outputs: hello sir my $good_guy2 = "$name is a good guy!" #if printed, outputs: Vassilios is a good guy!
Construct Meaning \n newline \r return \t tab \\ backslash \" double quote \$, \@, \& etc…
Page 21
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my $pet_string = "This is to TEST\nsome string functions"; print $pet_string; # lc returns lowercased version of $pet_string my $lc_string = lc($pet_string); print $lc_string; # uc returns uppercased version of $pet_string my $uc_string = uc($pet_string); print $uc_string; # length returns the length of $pet_string my $length_string = length($pet_string); print "$length_string"; print "*"x10; print "\t".("*"x10)."\n"; exit; Important strings functions I This is to TEST some string functions this is to test some string functions THIS IS TO TEST SOME STRING FUNCTIONS 38 ********** ********** Page 22
VI, October 2006
#!/usr/bin/perl my $pet_string = "TEST of some\nstring functions\n"; print $pet_string; # reverses $pet_string (character by character) my $rev_string = reverse($pet_string); print $rev_string."\n"; # takes 3 arguments: string value, zero-based # initial position and length for the substring my $sub_string = substr ($pet_string,0,15); print $sub_string."\n"; # if a string ends in a newline character, chomp removes it my $chomp_string = $pet_string; print $chomp_string; chomp($chomp_string); print $chomp_string; # chop removes the last character of a string my $chop_string = $chomp_string; print $chop_string; chop($chop_string); print $chop_string; exit;
TEST of some string functions snoitcnuf gnirts emos fo TSET TEST of some TEST of some string functions TEST of some string functions TEST of some string functionsTEST of some string functionsTEST of some string functionvioannid$
Page 23
VI, October 2006
Page 24
VI, October 2006
#!/usr/bin/perl # array of strings my @string_numbers = ('One', 'Two', 'Three', 'Four'); print @string_numbers; print "\n"; # array of integers my @numeric_numbers = (1..5); print "@numeric_numbers\n"; my @numeric_numbers2 = (1,2,3,4,5); print "@numeric_numbers2\n"; print "$string_numbers[2]\n"; exit;
OneTwoThreeFour 1 2 3 4 5 1 2 3 4 5 Three
Page 25
VI, October 2006
#!/usr/bin/perl # array of strings my @string_numbers = ('One', 'Two', 'Three', 'Four'); # replaces One with Zero in @string_numbers $string_numbers[0] = 'Zero'; print "@string_numbers\n"; my @sort_string_numbers = sort @string_numbers; print "@sort_string_numbers\n"; print scalar @string_numbers; print "\n"; my $nb = @string_numbers; print "$nb\n"; exit;
Zero Two Three Four Four Three Two Zero 4 4 Page 26
VI, October 2006
#!/usr/bin/perl my @string_numbers = ('Zero', 'Two', 'Three', 'Four'); my $end = $string_numbers[$#string_numbers]; print "$end\n"; # swap values ($gene1, $gene2) = ($gene2, $gene1); # variable interpolation my @login = ("$username", "$password"); my @anyarray = (6, "hello", @string_numbers ); my @rev_anyarray = reverse @rev_anyarray; print "@anyarray\n"; print "@rev_anyarray\n"; exit;
Four 6 hello Zero Two Three Four Four Three Two Zero hello 6
Page 27
VI, October 2006
Page 28
VI, October 2006
Page 29
VI, October 2006
Page 30
VI, October 2006
Page 31
VI, October 2006
Page 32
VI, October 2006
Value Key
Mon Monday Tue Tuesday Wed Wednesday Thu Thursday Fri Friday Sat Saturday Sun Sunday
#!/usr/bin/perl my %codon3 = ( "TTT" => "Phe", "TTA" => "Leu", ); print $codon3{'TTT'}; print "\n"; exit; vioannid$ ./hash.pl Phe vioannid$
Page 33
VI, October 2006
Page 34
VI, October 2006
#!/usr/bin/perl my %some_hash = ("John", "Travolta", "Betty", "Bossy"); print $some_hash{"John"}."\n"; my %capitals = ( 'china' => 'beijing', 'france' => 'paris', 'italy' => 'rome', 'switzerland' => 'bern', ); print %capitals; print "\n"; print $capitals{'china'}."\n"; my @k = keys %capitals; my @v = values %capitals; print "@k\n"; print "@v\n"; exit; Travolta switzerlandbernitalyromefranceparischinabeijing beijing switzerland italy france china bern rome paris beijing
Page 35
VI, October 2006
#!/usr/bin/perl my %capitals = ( 'china' => 'beijing', 'france' => 'paris', 'italy' => 'rome', 'switzerland' => 'bern', ); my $nb = keys %capitals; print "$nb\n"; my %rev_capitals = reverse %capitals; print %rev_capitals; print "\n"; print $rev_capitals{'china'}."\n"; print $rev_capitals{'beijing'}."\n"; exit; 4 beijingchinabernswitzerlandparisfranceromeitaly china Page 36
VI, October 2006
#!/usr/bin/perl print "Please enter your Lastname: "; my $lastname = <STDIN>; print "Please enter your Firstname: "; my $firstname = <STDIN>; print "Hello $firstname $lastname,\n I hope you like Perl programming !\n"; exit; Please enter your Lastname: Please enter your Firstname: Vassilios Ioannidis Hello Vassilios Ioannidis , I hope you like Perl programming !
Page 37
VI, October 2006
Please enter your Lastname: Please enter your Firstname: Vassilios Ioannidis Hello Vassilios Ioannidis, I hope you like Perl programming ! #!/usr/bin/perl print "Please enter your Lastname: "; my $lastname = <STDIN>; chomp $lastname; print "Please enter your Firstname: "; my $firstname = <STDIN>; chomp $firstname; print "Hello $firstname $lastname,\n I hope you like Perl programming !\n"; exit; Page 38
VI, October 2006
Page 39
VI, October 2006
#!/usr/local/bin/perl use strict; use warnings; my $x = 100; my $y = 99; if ($x > $y) { print "\"$x\" is numerically greater than \"$y\"\n" ; } else { print "\"$x\" is numerically smaller than \"$y\"\n" ; } if ($x gt $y) { print "\"$x\" is alphabetically greater than \"$y\"\n" ; } else { print "\"$x\" is alphabetically smaller than \"$y\"\n" ; } exit ; vioannid$ ./string_num_comp.pl "100" is numerically greater than "99" "100" is alphabetically smaller than "99" vioannid$ Page 40
VI, October 2006
$a += 1; # same as $a = $a + 1; #same as $a++; $a -= 1; # same as $a = $a - 1; #same as $a--; $a .= "\n"; # same as $a = $a. "\n";
Page 41
VI, October 2006
Page 42
VI, October 2006
#!/usr/bin/perl use strict; use warnings; print "\nEnter your name (then press \"return\" when done):\t"; #get information from the terminal window my $name = <STDIN>; #remove trailing "\n" if any chomp $name; if ($name eq "Couchepin") { print "Hello Mr President !\n" ; } exit ;
Page 43
VI, October 2006
#!/usr/bin/perl use strict; use warnings; print "\nEnter your name (then press \"return\" when done):\t"; #get information from the terminal window my $name = <STDIN>; #remove trailing "\n" if any chomp $name; if ($name eq "Couchepin") { print "Hello Mr President !\n" ; } else { print "Hello $name !\n" ; } exit ; Page 44
VI, October 2006
#!/usr/bin/perl use strict; use warnings; print "\nEnter your name (then press \"return\" when done):\t"; #get information from the terminal window my $name = <STDIN>; #remove trailing "\n" if any chomp $name; if ($name eq "Couchepin") { print "Hello Mr President !\n" ; } elsif ($name eq "Falquet") { print "Good day to you Master $name !\n" ; } else { print "Hello $name !\n" ; } exit ;
Page 45
VI, October 2006
foreach my $element ( @array ) { # do something with the element }
foreach my $key (keys %hash) { print "The value of $key is $hash{$key}\n"; }
#execute the contents of the block as long as $i is less than, or equal to 10 or while $i is smaller than 10. } Page 46
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my $counter; for ($counter=1;$counter<=10;$counter++) { print "I can count up to $counter !\n"; } exit ; I can count up to 1 ! I can count up to 2 ! I can count up to 3 ! I can count up to 4 ! I can count up to 5 ! I can count up to 6 ! I can count up to 7 ! I can count up to 8 ! I can count up to 9 ! I can count up to 10 !
Page 47
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my @names = ( "Simon","Arnaud","Todd","Natacha", "Francesca","Jan","Jeremy","Claudia", "Magdalena","Marcel","James","Joachim", "Sutada","Mingkwan","sivaraman","Ralf", "Kurt","Liviu","Dinesh","Eleonore", "Paul","Fekadu" ); foreach my $name (@names) { print "Hello $name !\n"; } exit ; vioannid$ ./list_fname.pl Hello Simon ! Hello Arnaud ! Hello Todd ! Hello Natacha ! Hello Francesca ! Hello Jan ! Hello Jeremy ! Hello Claudia ! Hello Magdalena ! Hello Marcel ! Hello James ! Hello Joachim ! Hello Sutada ! Hello Mingkwan ! Hello sivaraman ! Hello Ralf ! Hello Kurt ! Hello Liviu ! Hello Dinesh ! Hello Eleonore ! Hello Paul ! Hello Fekadu ! vioannid$ Page 48
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my @names = ( "Simon","Arnaud","Todd","Natacha", "Francesca","Jan","Jeremy","Claudia", "Magdalena","Marcel","James","Joachim", "Sutada","Mingkwan","sivaraman","Ralf", "Kurt","Liviu","Dinesh","Eleonore", "Paul","Fekadu" ); foreach (sort @names) { print "Hello $_ !\n"; } exit ; vioannid$ ./list_fname.pl Hello Arnaud ! Hello Claudia ! Hello Dinesh ! Hello Eleonore ! Hello Fekadu ! Hello Francesca ! Hello James ! Hello Jan ! Hello Jeremy ! Hello Joachim ! Hello Kurt ! Hello Liviu ! Hello Magdalena ! Hello Marcel ! Hello Mingkwan ! Hello Natacha ! Hello Paul ! Hello Ralf ! Hello Simon ! Hello Sutada ! Hello Todd ! Hello sivaraman ! vioannid$
Page 49
VI, October 2006
$& The string matched by the last successful pattern match. $` The string preceding whatever was matched by the last successful pattern match. $' The string following whatever was matched by the last successful pattern match. $! If a system or library call fails, it sets this variable This means that the value of $! is meaningful only immediately after a failure. $/ The input record separator, newline by default . $$ The process number of the Perl running this script. @ARGV commandline arguments (space separation by default). note: $ARGV[0] first commandline argument … Page 50
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my %names = ( "Barkow"=>"Simon", "Basle"=>"Arnaud", "Blevins"=>"Todd", "Bodenhausen"=>"Natacha", "Botta"=>"Francesca", "Kerschgens"=>"Jan", "Keusch"=>"Jeremy", "Kutter"=>"Claudia", "Livingstone"=>"Magdalena", "Meury"=>"Marcel", "Moore"=>"James", "Muller"=>"Joachim", "Mungpakdee"=>"Sutada", "Nipitwattanaphon"=>"Mingkwan", "Padavattan"=>"sivaraman", "Paul"=>"Ralf", "Tobler"=>"Kurt", "Vanoaica"=>"Liviu", "Vellore Palanivelu"=>"Dinesh", "Wassmann"=>"Paul", "Yadetie"=>"Fekadu", "von Castelmur"=>"Eleonore", ); foreach my $key (sort keys %names) { print "The firstname of $key is $names{$key}\n"; } exit ; vioannid$ ./list_fname_hash.pl The firstname of Barkow is Simon The firstname of Basle is Arnaud The firstname of Blevins is Todd The firstname of Bodenhausen is Natacha The firstname of Botta is Francesca The firstname of Kerschgens is Jan The firstname of Keusch is Jeremy The firstname of Kutter is Claudia The firstname of Livingstone is Magdalena The firstname of Meury is Marcel The firstname of Moore is James The firstname of Muller is Joachim The firstname of Mungpakdee is Sutada The firstname of Nipitwattanaphon is Mingkwan The firstname of Padavattan is sivaraman The firstname of Paul is Ralf The firstname of Tobler is Kurt The firstname of Vanoaica is Liviu The firstname of Vellore Palanivelu is Dinesh The firstname of Wassmann is Paul The firstname of Yadetie is Fekadu The firstname of von Castelmur is Eleonore vioannid$
Page 51
VI, October 2006
while ( condition ) { #execute the contents of the block }
while (1) { #execute the contents of the block forever ! }
In Perl some variables are considered true:
For example: $lang = "Perl"; # < true $version = 5.6; # < true $zero = 0; # < false $empty = ""; # < false @states = (); # < false %table = (1 => "one"); # < true Page 52
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my $number = 1; while ($number<=10) { print "I can count up to $number !"; $number+=1; #Ha ! } exit ;
#!/usr/bin/perl use strict; use warnings; my $number = 1; while ($number<=10) { print "I can count up to $number !"; } exit ; #really ?
I can count up to 1 ! I can count up to 1 ! I can count up to 1 ! I can count up to 1 ! I can count up to 1 ! I can count up to 1 ! I can count up to 1 ! I can count up to 1 ! I can count up to 1 ! I can count up to 1 ! I can count up to 1 ! I can count up to 1 ! I can count up to 1 ! I can count up to 1 ! I can count up to 1 ! I can count up to 1 ! ^C vioannid$
Page 53
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my $number = 1; while ($number<=10) { print "I can count up to $number !"; $number+=1; #Ha ! } exit ;
#!/usr/bin/perl use strict; use warnings; my $number = 1; while ($number<=10) { print "I can count up to $number !"; $number = $number+1; #same as $number += 1; #same as $number++; } exit ; vioannid$ ./list_while_array.pl I can count up to 1 ! I can count up to 2 ! I can count up to 3 ! I can count up to 4 ! I can count up to 5 ! I can count up to 6 ! I can count up to 7 ! I can count up to 8 ! I can count up to 9 ! I can count up to 10 ! vioannid$ Page 54
VI, October 2006
Page 55
VI, October 2006