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 : Filehandles & File Tests Subroutines (functions)
Page 1
VI, October 2006
Page 2
VI, October 2006
Page 3
VI, October 2006
Page 4
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 5
VI, October 2006
vioannid$ cat listparticipants.csv "Barkow","Simon","ETHZ","8057","Mr." "Basle","Arnaud","University of Basel","4056","Dr. (Mr.)" "Blevins","Todd","FMI","4058","Mr." "Bodenhausen","Natacha","University of Lausanne","1015","Mrs." "Botta","Francesca","University of Fribourg","6601","Mrs." "Kerschgens","Jan","EPFL","1015","Mr." "Keusch","Jeremy","FMI","4058","Dr. (Mr.)" "Kutter","Claudia","FMI","4058","Mrs." "Livingstone","Magdalena","ETHZ","8057","Mrs." "Meury","Marcel","University of Basel","4056","Mr." "Moore","James","University of Basel","4056","Dr. (Mr.)" "Muller","Joachim","University of Bern","3012","Dr. (Mr.)" "Mungpakdee","Sutada","other","5008","Mrs." "Nipitwattanaphon","Mingkwan","University of Lausanne","CH - 1015","Mrs." "Padavattan","sivaraman","University of Basel","4056","Dr. (Mr.)" "Paul","Ralf","University of Basel","4056","Dr. (Mr.)" "Tobler","Kurt","University of Zurich","8057","Dr. (Mr.)" "Vanoaica","Liviu","EPFL","1066","Mr." "Vellore Palanivelu","Dinesh","University of Basel","4056","Dr. (Mr.)" "von Castelmur","Eleonore","University of Basel","4056","Mrs." "Wassmann","Paul","University of Basel","4056","Mr." "Yadetie","Fekadu","other","N-5008","Dr. (Mr.)" vioannid$ Page 6
VI, October 2006
#!/usr/bin/perl use strict; use warnings;
while (<FILE>) { if (m/^\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\"/) { print "Hello $5 $2 $1 from $4, $3 !\n"; } else {} } exit;
vioannid$ ./argv.pl Hello Mr. Simon Barkow from 8057, ETHZ ! Hello Dr. (Mr.) Arnaud Basle from 4056, University of Basel ! Hello Mr. Todd Blevins from 4058, FMI ! . . . Hello Dr. (Mr.) Fekadu Yadetie from N-5008, other ! vioannid$
Page 7
VI, October 2006
#!/usr/bin/perl use strict; use warnings; print @ARGV; my $nb_arg = @ARGV; my $argument = $ARGV[0]; print "\n$nb_arg\n"; print "The invocation argument is: $argument\n"; exit;
vioannid$ ./argv3.pl listparticipants The invocation argument is: listparticipants vioannid$ 1 listparticipants Page 8
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my $filename = $ARGV[0];
while (<FILE>) { if (m/^\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\"/) { print "Hello $5 $2 $1 from $4, $3 !\n"; } else {} } exit;
vioannid$ ./argv.pl listparticipants.csv Hello Mr. Simon Barkow from 8057, ETHZ ! Hello Dr. (Mr.) Arnaud Basle from 4056, University of Basel ! Hello Mr. Todd Blevins from 4058, FMI ! . . . Hello Dr. (Mr.) Fekadu Yadetie from N-5008, other ! vioannid$
Page 9
VI, October 2006
Page 10
VI, October 2006
File is readable by effective user/group.
File is writable by effective user/group.
File is executable by effective user/group.
File is readable by real user/group.
File is writable by real user/group.
File is executable by real user/group.
File is owned by real user.
File exists.
File has zero size.
File has nonzero size (returns size).
File is a plain file.
File is a directory.
File is a symbolic link.
File is a named pipe (FIFO).
File is a socket.
File is a block special file.
File is a character special file.
Filehandle is opened to a tty.
File has setuser bit set.
File has setgroup bit set.
File has sticky bit set.
File is a text file.
File is a binary file (opposite of -T).
Age of file (at startup) in days since modification.
Age of file (at startup) in days since last access.
Age of file (at startup) in days since inode change. . . . if (-e $filename) { #do something } . . .
Page 11
VI, October 2006
Page 12
VI, October 2006
>3BHS1_RAT PGWSCLVTGAGGFVGQRIIRMLVQEKELQEVRALDKVFRPETKEEFSKLQTKAKVTMLEG DILDAQYLRRACQGISVVIHTAAVIDVSHVLPRQTILDVNLKGTQNILEACVEASVPAFI YCSTVDVAGPNSYKKIILNGHEEEHHESTWSDAYPYSKRMAEKAVLAANGSILKNGGTLH TCALRPMYIYGERSPFLSVMILAALKNKGILNVTGKFSIANPVYVGNVAWAHILAARGLR DPKKSQNVQGQFYYISDDTPHQSYDDLNCTLSKEWGLRLDSSWSLPLPLLYWLAFLLETV SFLLRPFYNYRPPFNCHLVTLSNSKFTFSYKKAQRDLGYVPLVSWEEAKQKTSEWIGTLV EQHRETLDTKSQ >3BHS2_RAT PGWSCLVTGAGGFVGQRIIRMLVQEKELQEVRALDKVFRPETKEEFSKLQTKAKVTMLEG DILDAQYLRRACQGISVVIHTASVMDFSRVLPRQTILDVNLKGTQNLLEAGIHASVPAFI YCSTVDVAGPNSYKKTILNGREEEHHESTWSNPYPYSKKMAEKAVLAANGSILKNGGTLH TCALRPMYIYGERGQFLSRIIIMALKNKGVLNVTGKFSIVNPVYVGNVAWAHILAARGLR DPKKSQNIQGQFYYISDDTPHQSYDDLNCTLSKEWGLRLDSSWSLPLPLLYWLAFLLETV SFLLRPFYNYRPPFNCHLVTLSNSKFTFSYKKAQRDLGYEPLVSWEEAKQKTSEWIGTLV EQHRETLDTKSQ >3BHS4_RAT PGWSCLVTGAGGFLGQRIVQLLVQEKDLKEVRVLDKVFRPETREEFFNLGTSIKVTVLEG DILDTQCLRRACQGISVVIHTAALIDVTGVNPRQTILDVNLKGTQNLLEACVQASVPAFI . . .
Page 13
VI, October 2006
ID 3BHS1_RAT STANDARD; PRT; 372 AA. AC P22071; . . . . . SQ SEQUENCE 372 AA; 41906 MW; F989617C1AF18949 CRC64; PGWSCLVTGA GGFVGQRIIR MLVQEKELQE VRALDKVFRP ETKEEFSKLQ TKAKVTMLEG DILDAQYLRR ACQGISVVIH TAAVIDVSHV LPRQTILDVN LKGTQNILEA CVEASVPAFI YCSTVDVAGP NSYKKIILNG HEEEHHESTW SDAYPYSKRM AEKAVLAANG SILKNGGTLH TCALRPMYIY GERSPFLSVM ILAALKNKGI LNVTGKFSIA NPVYVGNVAW AHILAARGLR DPKKSQNVQG QFYYISDDTP HQSYDDLNCT LSKEWGLRLD SSWSLPLPLL YWLAFLLETV SFLLRPFYNY RPPFNCHLVT LSNSKFTFSY KKAQRDLGYV PLVSWEEAKQ KTSEWIGTLV EQHRETLDTK SQ // ID 3BHS2_RAT STANDARD; PRT; 372 AA. AC P22072; . . . . . SQ SEQUENCE 372 AA; 42145 MW; EDAB175F3F33334B CRC64; PGWSCLVTGA GGFVGQRIIR MLVQEKELQE VRALDKVFRP ETKEEFSKLQ TKAKVTMLEG DILDAQYLRR ACQGISVVIH TASVMDFSRV LPRQTILDVN LKGTQNLLEA GIHASVPAFI YCSTVDVAGP NSYKKTILNG REEEHHESTW SNPYPYSKKM AEKAVLAANG SILKNGGTLH TCALRPMYIY GERGQFLSRI IIMALKNKGV LNVTGKFSIV NPVYVGNVAW AHILAARGLR DPKKSQNIQG QFYYISDDTP HQSYDDLNCT LSKEWGLRLD SSWSLPLPLL YWLAFLLETV SFLLRPFYNY RPPFNCHLVT LSNSKFTFSY KKAQRDLGYE PLVSWEEAKQ KTSEWIGTLV EQHRETLDTK SQ //
Page 14
VI, October 2006
$/=">"; while (<INFILE>) { # assigns each line in turn to $_ print "Entry: $_"; }
vioannid$ ./test.pl wgetz-1 Entry:>Entry:3BHS1_RAT PGWSCLVTGAGGFVGQRIIRMLVQEKELQEVRALDKVFRPETKEEFSKLQTKAKVTMLEG DILDAQYLRRACQGISVVIHTAAVIDVSHVLPRQTILDVNLKGTQNILEACVEASVPAFI YCSTVDVAGPNSYKKIILNGHEEEHHESTWSDAYPYSKRMAEKAVLAANGSILKNGGTLH TCALRPMYIYGERSPFLSVMILAALKNKGILNVTGKFSIANPVYVGNVAWAHILAARGLR DPKKSQNVQGQFYYISDDTPHQSYDDLNCTLSKEWGLRLDSSWSLPLPLLYWLAFLLETV SFLLRPFYNYRPPFNCHLVTLSNSKFTFSYKKAQRDLGYVPLVSWEEAKQKTSEWIGTLV EQHRETLDTKSQ >Entry:3BHS2_RAT PGWSCLVTGAGGFVGQRIIRMLVQEKELQEVRALDKVFRPETKEEFSKLQTKAKVTMLEG DILDAQYLRRACQGISVVIHTASVMDFSRVLPRQTILDVNLKGTQNLLEAGIHASVPAFI YCSTVDVAGPNSYKKTILNGREEEHHESTWSNPYPYSKKMAEKAVLAANGSILKNGGTLH TCALRPMYIYGERGQFLSRIIIMALKNKGVLNVTGKFSIVNPVYVGNVAWAHILAARGLR DPKKSQNIQGQFYYISDDTPHQSYDDLNCTLSKEWGLRLDSSWSLPLPLLYWLAFLLETV SFLLRPFYNYRPPFNCHLVTLSNSKFTFSYKKAQRDLGYEPLVSWEEAKQKTSEWIGTLV EQHRETLDTKSQ . . . . .
Page 15
VI, October 2006
Page 16
VI, October 2006
#!/usr/local/bin/perl use strict; use warnings; my @names1 = ("Pedro", "Claire", "Yemima", "Fabien" ,"Francisco"); foreach (@names1 ) { my $size = length($_); print '*'x($size+2),"\n"; print "*$_*\n"; print '*'x($size+2),"\n"; } exit ;
******* *Pedro* ******* ******** *Claire* ******** ******** *Yemima* ******** ******** *Fabien* ******** *********** *Francisco* ***********
Page 17
VI, October 2006
#!/usr/local/bin/perl use strict; use warnings; my @names1 = ("Pedro", "Claire", "Yemima", "Fabien" ,"Francisco"); foreach (@names1 ) { my $size = length($_); print '*'x($size+2),"\n"; print "*$_*\n"; print '*'x($size+2),"\n"; } my @names2 = ("Sandra Yukie", "Simona", "Christophe", "Dominique", "Michaela"); foreach (@names2 ) { my $size = length($_); print '*'x($size+2),"\n"; print "*$_*\n"; print '*'x($size+2),"\n"; } my @names3 = ("Lionel", "Gabriele", "Michael", "Charlotte", "Subhash", "Adam"); foreach (@names3 ) { my $size = length($_); print '*'x($size+2),"\n"; print "*$_*\n"; print '*'x($size+2),"\n"; } exit ;
******* *Pedro* ******* ******** *Claire* ******** ******** *Yemima* ******** ******** *Fabien* ******** *********** *Francisco* *********** Page 18
VI, October 2006
Page 19
VI, October 2006
#defining subroutine sub myfunc { my $param = shift(@_); . . . return $result; } # calling a function $calcul = myfunc($value); #defining subroutine sub myproc { my $param = shift(@_); . . . return; } # calling procedure myproc($value); Some Perl commands tell the Perl interpreter to do something. A statement starting with a "verb" is generally purely imperative. We often call these "verbs" procedures: a frequently seen command is the print command. Some verbs are for asking questions, and are useful in conditional statements. Other verbs translate their input parameters into return values, just as a recipe tells you how to turn raw ingredients into something (hopefully)
Page 20
VI, October 2006
#!/usr/local/bin/perl use strict; use warnings; my @names1 = ("Pedro", "Claire", "Yemima", "Fabien" ,"Francisco"); my @names2 = ("Sandra Yukie", "Simona", "Christophe", "Dominique", "Michaela"); my @names3 = ("Lionel", "Gabriele", "Michael", "Charlotte", "Subhash", "Adam"); my @names4 = ("Sebastian", "Tu", "Sergey", "Olusegun", "Joel", "Uta", "Viviane"); my @names5 = ("Stanislav", "Kyrill", "Petr", "Sebastien", "Haleh"); &pretty_print(@names1); &pretty_print(@names2); &pretty_print(@names3); &pretty_print(@names4); &pretty_print(@names5); exit ; sub pretty_print { foreach (@_) { my $size = length($_); print '*'x($size+2),"\n"; print "*$_*\n"; print '*'x($size+2),"\n"; } }
******* *Pedro* ******* ******** *Claire* ******** ******** *Yemima* ******** ******** *Fabien* ******** *********** *Francisco* ***********
Page 21
VI, October 2006
#!/usr/bin/perl use strict; use warnings; #call the helloworld function #& is optional with parentheses helloworld(); #tell the program to exit exit; sub helloworld{ print "hello World !\n"; } vioannid$ ./sub_hello.pl hello World ! vioannid$
Page 22
VI, October 2006
#!/usr/bin/perl use strict; use warnings; # pass 2 arguments to the plus function # receive the output in $sum my $sum = plus(12,34); print "$sum\n"; exit; sub plus{ my($x, $y)=@_; return $x+$y; } vioannid$ ./sub_plus.pl 46 vioannid$
Page 23
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my @vals=(1, 4, 5, 8); my $sum = &plus(@vals); print "sum=$sum\n"; sub plus{ my @values = @_; my $add = 0; foreach(@values){ $add += $_; } return $add; } vioannid$ ./sub_plus_array.pl sum=18 vioannid$ Page 24
VI, October 2006
my $nbParam = scalar @_;
Page 25
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my @list = ( "Simon","Arnaud", "Todd","Natacha" ); foreach my $name (@list) { print "Hello $name !\n"; } print "Hello $name !\n"; exit ; vioannid$ ./scope.pl Global symbol "$name" requires explicit package name at ./scope.pl line 14. Execution of ./scope.pl aborted due to compilation errors. vioannid$
Page 26
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my $a = 5; my $b = 10; print '$a before: '."$a\n"; print '$b before: '."$b\n\n"; double($a,$b); print '$a after: '."$a\n"; print '$b after: '."$b\n"; exit ; sub double { $a = shift; #$a = $_[0]; $b = shift; #$b = $_[1]; $a=$a*2; $b=$b*2; print 'double, $a: '."$a\n"; print 'double, $b: '."$b\n\n"; } vioannid$ ./ref.pl $a before: 5 $b before: 10 double, $a: 10 double, $b: 20 $a after: 10 $b after: 20 vioannid$
Page 27
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my $a = 5; my $b = 10; print '$a before: '."$a\n"; print '$b before: '."$b\n\n"; double($a,$b); print '$a after: '."$a\n"; print '$b after: '."$b\n"; exit ; sub double { my $a = shift; #my $a = $_[0]; my $b = shift; #my $b = $_[1]; $a=$a*2; $b=$b*2; print 'double, $a: '."$a\n"; print 'double, $b: '."$b\n\n"; } vioannid$ ./ref.pl $a before: 5 $b before: 10 double, $a: 10 double, $b: 20 $a after: 5 $b after: 10 vioannid$ Page 28
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my @list1 = ("Pamela","Monica","Sophie"); my @list2 = ("Natacha","Francesca","Magdalena"); print "@list1\n";print "@list2\n"; list(@list1,@list2); exit ; sub list { my(@firstArray, @secondArray) = @_ ; print("The first array is @firstArray.\n"); print("The second array is @secondArray.\n"); } vioannid$ ./ref3.pl Pamela Monica Sophie Natacha Francesca Magdalena The first array is Pamela Monica Sophie Natacha Francesca Magdalena. The second array is . vioannid$
Page 29
VI, October 2006
Page 30
VI, October 2006
Page 31
VI, October 2006
name value "address" @list_names john magdalena luc 0x180b324 \@list_names 0x180b324 0x180b318 @list_copy john magdalena luc 0x180b524
Page 32
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my @list1 = ("Pamela","Monica","Brad"); my @list2 = ("Natacha","Francesca","James"); print "@list1\n"; print "@list2\n"; list(\@list1,\@list2); print "@list1\n"; print "@list2\n"; exit ; sub list { my($ref1, $ref2) = @_; @{$ref1} = uc reverse @{$ref1}; print("First array sorted: @{$ref1}.\n"); print("Second array reversed: @{$ref2}.\n"); } vioannid$ ./ref4.pl Pamela Monica Sophie Natacha Francesca Magdalena First array sorted: EIHPOSACINOMALEMAP. Second array reversed: Natacha Francesca Magdalena. EIHPOSACINOMALEMAP Natacha Francesca Magdalena vioannid$
Page 33
VI, October 2006
${$ref}{"name"} $ref->{"job"} %{$ref} %$ref $ref = \%hash $hash = { "name" => "steve", "job" => "DJ" }; %hash = ( "name" => "steve", "job" => "DJ" ); %hash ${$ref}[1] $ref->[1] @$ref @{$ref} $ref = \@list $ref = [ "steve", "fred" ]; @list = ( "steve", "fred" ); @list NA $$ref ${$ref} $ref = \$scalar ${$ref} $scalar="steve"; $scalar Accessing an element Dereferencing it Referencing it Instantiating a reference to it Instantiating the scalar Variable
Page 34
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my %hash = ( "name" => "steve", "job" => "DJ" ); my $ref = \%hash; print ${$ref}{"name"}; print "\n"; print $ref->{'job'}; print "\n"; my %hash_copy = %$ref; print %hash_copy; print "\n"; exit; steve DJ namestevejobDJ
Page 35
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my @list = ( "bruce", "michael" ); my $ref2 = \@list; print ${$ref2}[0]; print "\n"; print $ref2->[1]; print "\n"; my @list_copy = @$ref2; print @list_copy; print "\n"; exit; bruce michael brucemichael Page 36
VI, October 2006
Page 37
VI, October 2006
Page 38
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my @gene1 = qw(AKT_signaling Erk Integrin_signaling); my @gene1_name = ('gene1', \@gene1); my @gene2 =qw(LCK_signaling BRCA-1_pathway TCF-1_pathway ); my @gene2_name = ('gene2', \@gene2); my @all_gene_names = (\@gene1_name,\@gene2_name,); print @all_gene_names ; print "\n"; my @array_gene_name_refs = @{$all_gene_names[0]}; print @array_gene_name_refs ; print "\n"; my $value_array_gene_name_refs1 = @{$all_gene_names[0]}[0]; print $value_array_gene_name_refs1 ; print "\n"; my $value_array2_field3 = ${${$all_gene_names[1]}[1]}[2]; print $value_array2_field3; print "\n"; exit; gene1ARRAY(0x180b324) gene1 ARRAY(0x180b318)ARRAY(0x180d888) TCF-1_pathway
Page 39
VI, October 2006
#!/usr/bin/perl use strict; use warnings; my @gene1 = qw(AKT_signaling Erk Integrin_signaling); my @gene1_name = ('gene1', \@gene1); my @gene2 =qw(LCK_signaling BRCA-1_pathway TCF-1_pathway ); my @gene2_name = ('gene2', \@gene2); my @all_gene_names = (\@gene1_name,\@gene2_name,); print @all_gene_names ; print "\n"; my @array_gene_name_refs = @{$all_gene_names[0]}; print @array_gene_name_refs ; print "\n"; my $value_array_gene_name_refs1 = $all_gene_names[0]->[0]; print $value_array_gene_name_refs1 ; print "\n"; my $value_array2_field3 = $all_gene_names[1]->[1]->[2]; print $value_array2_field3; print "\n"; exit; gene1ARRAY(0x180b324) gene1 ARRAY(0x180b318)ARRAY(0x180d888) TCF-1_pathway Page 40
VI, October 2006
Page 41
VI, October 2006
#!/usr/local/bin/perl use strict; use warnings; my $v1 = complex_operation (param1); . . . my $v2 = complex_operation (param2); exit ; sub complex_operation {
} #!/usr/local/bin/perl use strict; use warnings; my $v1 = complex_operation (param1); . . . my $v2 = complex_operation (param2); exit ; sub complex_operation {
}
Page 42
VI, October 2006
Page 43
VI, October 2006
#!/usr/local/bin/perl use strict; use warnings; use Mymod; my $v1 = Mymod::complex_operation (param1); . . . my $v2 = Mymod::complex_operation (param2); . . . sub complex_operation {
}
#!/usr/local/bin/perl use strict; use warnings; use Mymod; my $v1 = Mymod::complex_operation (param1); . . . my $v2 = Mymod::complex_operation (param2); . . .
Page 44
VI, October 2006
Page 45
VI, October 2006
Page 46
VI, October 2006
Page 47
VI, October 2006
Page 48
VI, October 2006
Page 49
VI, October 2006
Page 50
VI, October 2006