Dpuiu Perl: Difference between revisions

From Cbcb
Jump to navigation Jump to search
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Links =
= Links =
* [http://perldoc.perl.org/]
* [http://perldoc.perl.org/]
* [http://perltraining.com.au/notes/sysadmin.pdf SysAdmin Perl]
* [http://perltraining.com.au/notes/sysadmin.pdf SysAdmin Perl]
Line 13: Line 14:


= POD =
= POD =
* [http://en.wikipedia.org/wiki/Plain_Old_Documentation POD]; pod2html  pod2latex pod2man  pod2text  pod2usage
* [http://en.wikipedia.org/wiki/Plain_Old_Documentation POD]; pod2html  pod2latex pod2man  pod2text  pod2usage
* make sure to leave 1 empty line before/after cut
* make sure to leave 1 empty line before/after cut
Line 48: Line 50:
   sub max(number,number) Returns max of 2 numbers
   sub max(number,number) Returns max of 2 numbers
   sub min(number,number) Returns min of 2 numbers
   sub min(number,number) Returns min of 2 numbers
= Functions =
== Map ==
  sub addString { return @_[0].$_[1]; }
  @myNames = ('jacob', 'alexander', 'ethan', 'andrew');
  @ucNames = map(addString($_,"."), @myNames);
  @a = (1,3,5);


= In place editing =
= In place editing =
* Edit a file in place: -i
* Edit a file in place: -i
   perl -i.bck script.pl xxx
   perl -i.bck script.pl xxx
Line 77: Line 90:


= Search directories =
= Search directories =
* -Idir : same as "use lib dir_name
  perl -Idir_name  file.pl
  <=>
  cat file.pl
    use lib dir_name ;
 
   default: /usr/include & /usr/lib/perl
   default: /usr/include & /usr/lib/perl
   add dirs to PERLLIB env variable <=>  @INC  
   add dirs to PERLLIB env variable <=>  @INC  
Line 95: Line 112:
* '''caller function : shows the context'''
* '''caller function : shows the context'''
   print STDERR "DEBUG: context: ",(caller(0))[3],"\n";
   print STDERR "DEBUG: context: ",(caller(0))[3],"\n";
*  
 
  sub myDie { print STDERR "DEBUG: context: ",(caller(1))[3],"\n"; }
  sub b { myDie(); }
 
* Example
   use Data::Dumper;
   use Data::Dumper;
   ...
   ...
   Dumper(\@list);
   Dumper(\@list);
   Dumper(\%hash);
   Dumper(\%hash);
* perldebug
!    : re-exectutes prev command
!<n> : re-exectutes command <n>
H    : show command history
save  debug.history : saves debug history
source debug.history : runs debug history
= Serialization =
* Writes object to a file
* Example
  $ cat dump.pl
  use Data::Dumper;
  require "delta.pm";
  my $result=delta2result();
  print Data::Dumper->Dump([$result],['result']);
  $cat eval.pl
  use Data::Dumper;
  require "delta.pm";
 
  undef $/;
  eval <>;
 
  print Data::Dumper->Dump([$result],['result']);
= Redefine commands =
* Example:
  use subs 'cmp';


= Libraries =
= Libraries =
Line 135: Line 189:
   $ ./Build install
   $ ./Build install


Useful ones:
== Useful modules ==
* [http://perldoc.perl.org/Getopt/Long.html Getopt::Long]
* [http://perldoc.perl.org/Getopt/Long.html Getopt::Long]
* File::Find
* File::Find
* [http://bonsai.hgc.jp/~mdehoon/software/cluster/cluster.pdf Algorithm::Cluster]
* Bioperl
  git clone git://github.com/bioperl/bioperl-live.git
  cd bioperl-live/
  ./Build.PL --prefix /fs/szdevel/core-cbcb-software/Linux-x86_64
  ./Build test
  ./Build install
  cd ..
  git clone git://github.com/bioperl/bioperl-run.git
  cd bioperl-run/
  ./Build.PL --prefix /fs/szdevel/core-cbcb-software/Linux-x86_64
  ./Build test
  ./Build install
  cd ..
  git clone git://github.com/bioperl/bioperl-ext.git
  perl Makefile.PL PREFIX=/fs/szdevel/core-cbcb-software/Linux-x86_64  "DIR=Bio/Ext Bio/SeqIO/staden"
  make
  make test
  make install
  mv Makefile Makefile.old
  mv ./Bio/Ext/Align/Makefile ./Bio/Ext/Align/Makefile.old
  mv ./Bio/SeqIO/staden/Makefile ./Bio/SeqIO/staden/Makefile.old
  #if error ...
  cat Makefile.old                    | p 'if(/^(\w+FLAGS\s+=)(.+)/) { print $1," -fPIC ",$2,"\n" } else { print $_ }' > Makefile
  cat ./Bio/Ext/Align/Makefile.old    | p 'if(/^(\w+FLAGS\s+=)(.+)/) { print $1," -fPIC ",$2,"\n" } else { print $_ }' > ./Bio/Ext/Align/Makefile
  cat ./Bio/SeqIO/staden/Makefile.old | p 'if(/^(\w+FLAGS\s+=)(.+)/) { print $1," -fPIC ",$2,"\n" } else { print $_ }' > ./Bio/SeqIO/staden/Makefile
TO_INST_PM


= Variables =
= Variables =

Latest revision as of 20:32, 21 April 2011

Links

Style

POD

  • POD; pod2html pod2latex pod2man pod2text pod2usage
  • make sure to leave 1 empty line before/after cut

Example:

 $ cat test.pl
 =pod

 =head1 sub max(number,number)
 Returns max of 2 numbers

 =cut

 sub max
 {
       my ($a,$b)=@_;
       return ($a>$b)?$a:$b;
 }

 =head1 sub min(number,number)
 Returns min of 2 numbers

 =cut

 sub min
 {
       my ($a,$b)=@_;
       return ($a<$b)?$a:$b;
 }

 print max(1,2),"\n";
 print min(1,2),"\n";
 $ perdoc test.pl
 sub max(number,number) Returns max of 2 numbers
 sub min(number,number) Returns min of 2 numbers

Functions

Map

 sub addString { return @_[0].$_[1]; }
 @myNames = ('jacob', 'alexander', 'ethan', 'andrew');
 @ucNames = map(addString($_,"."), @myNames);

 @a = (1,3,5);

In place editing

  • Edit a file in place: -i
 perl -i.bck script.pl xxx
 => xxx(new), xxx.bck(old)
  • dos2unix
 perl -i -pe 's/\r//g' file
  • unix2dos
 perl -i -pe 's/\n/\r\n/' file

Cmd line options

  • BEGIN/END blocks
 perl -ane 'BEGIN { print "sum\n"}  END { print $sum,"\n" } $sum += $F[0]; '
  • Check all files
 ls *pl | perl -pe 'print "perl -c "; '

Regular Expressions

  • Maximal match (default)
 echo $PERLLIB | perl -ane '/^(.+):/; print $1,"\n"'
  • Minimal match
 echo $PERLLIB | perl -ane '/^(.+?):/; print $1,"\n"'
 /fs/sz-user-supported/Linux-x86_64/lib
  • Translation:
 $_ =~ tr/abc/ABC/
  • All matches
 while($text=~m/($pattern)/g) { print "$` $& $'\n"; }

Search directories

  perl -Idir_name  file.pl
  <=>
 cat file.pl
   use lib dir_name ;
 default: /usr/include & /usr/lib/perl
 add dirs to PERLLIB env variable <=>  @INC 
 perl -e '$,="\n"; print  @INC ; '
  • Find installed modules
 perl -e 'foreach(@INC) { if(-d $_) {system "find $_ -type d -name Bio\n";}} '
 perl -e 'foreach(@INC) { if(-f $_) {system "find $_ -type f -name SeqIO.pm\n";}} '
 perl -e 'foreach(@INC) { if(-d $_) {system "find $_ -type f -path Bio/SeqIO.pm\n";}} ' # not working though it should!
 ~/bin/findPerlModule.pl Bio::SeqIO

Command line arguments

  • PERL5OPT : -[DIMUdmtw] switches

Debug

  • caller function : shows the context
 print STDERR "DEBUG: context: ",(caller(0))[3],"\n";
 sub myDie { print STDERR "DEBUG: context: ",(caller(1))[3],"\n"; }
 sub b { myDie(); }
  • Example
 use Data::Dumper;
 ...
 Dumper(\@list);
 Dumper(\%hash);
  • perldebug
!    : re-exectutes prev command
!<n> : re-exectutes command <n>
H    : show command history

save   debug.history : saves debug history
source debug.history : runs debug history

Serialization

  • Writes object to a file
  • Example
 $ cat dump.pl

 use Data::Dumper;
 require "delta.pm";

 my $result=delta2result();
 print Data::Dumper->Dump([$result],['result']);
 $cat eval.pl

 use Data::Dumper;
 require "delta.pm";
 
 undef $/;
 eval <>;
  
 print Data::Dumper->Dump([$result],['result']);

Redefine commands

  • Example:
 use subs 'cmp';

Libraries

  • prefix.pm
 ...
 1;
  • prefix.pl
 require "prefix.pm":
 ...


Modules

  • -M moldule: same as "use module_name"
 perl -MLWP::Simple -e' print head "http://www.example.com"'
 perl -e 'use LWP::Simple; print head "http://www.example.com"'
  • Installation(Makefile)
 $ perl Makefile.PL PREFIX=~/szdevel/CPAN
 $ make
 $ make test
 $ make install
   
 $ find ~/szdevel/CPAN -type f
 bin/xmltidy
 lib/perl5/site_perl/5.8.5/XML/Tidy.pm
 lib64/perl5/site_perl/5.8.5/x86_64-linux-thread-multi/auto/XML/Tidy/.packlist
 lib64/perl5/5.8.5/x86_64-linux-thread-multi/perllocal.pod
 share/man/man3/XML::Tidy.3pm
  • Installation(Build)
 $ perl Build.PL --prefix ~/szdevel/CPAN
 $ ./Build test
 $ ./Build install

Useful modules

 git clone git://github.com/bioperl/bioperl-live.git
 cd bioperl-live/
 ./Build.PL --prefix /fs/szdevel/core-cbcb-software/Linux-x86_64
 ./Build test
 ./Build install
 cd ..
 git clone git://github.com/bioperl/bioperl-run.git
 cd bioperl-run/
 ./Build.PL --prefix /fs/szdevel/core-cbcb-software/Linux-x86_64
 ./Build test
 ./Build install
 cd ..
 git clone git://github.com/bioperl/bioperl-ext.git
 perl Makefile.PL PREFIX=/fs/szdevel/core-cbcb-software/Linux-x86_64  "DIR=Bio/Ext Bio/SeqIO/staden"
 make
 make test
 make install
 mv Makefile Makefile.old
 mv ./Bio/Ext/Align/Makefile ./Bio/Ext/Align/Makefile.old
 mv ./Bio/SeqIO/staden/Makefile ./Bio/SeqIO/staden/Makefile.old
 #if error ...
 cat Makefile.old                    | p 'if(/^(\w+FLAGS\s+=)(.+)/) { print $1," -fPIC ",$2,"\n" } else { print $_ }' > Makefile
 cat ./Bio/Ext/Align/Makefile.old    | p 'if(/^(\w+FLAGS\s+=)(.+)/) { print $1," -fPIC ",$2,"\n" } else { print $_ }' > ./Bio/Ext/Align/Makefile
 cat ./Bio/SeqIO/staden/Makefile.old | p 'if(/^(\w+FLAGS\s+=)(.+)/) { print $1," -fPIC ",$2,"\n" } else { print $_ }' > ./Bio/SeqIO/staden/Makefile

TO_INST_PM

Variables

  • spec_variables
  • $0 : program name
  • $. : input line number
  • $, : output field separator (default="")
 $ perl -e '$, = "\n";print (1,2,3,"");'
 1
 2
 3
 $ perl -e '$, = " "; @l=("a","b","c"); print map {uc($_)} @l; '
 A B C
  • $/ : the input record separators (default="\n")
  • $\ : output record separators (default="")


Argument passing

Example:

 sub test
 {
       my ($options)=@_;
       my %options=%{$options};
       print $options{a},"\n";
 }

 my %options;
 $options{a}="a";
 test(\%options);

Files

  • open(IN, '-') # Open standard input
  • open(IN, '>-') # Open standard output
  • @lines = <IN> # read all lines in the file
  • $line = <IN> # read one line

Hashes

  • foreach $person (keys %ages) {}
  • foreach $age (values %ages) { }
  • while (($person, $age) = each(%ages)) {}
  • cmd hash
 my %cmd_hash = (scarf2std=>\&scarf2std, fqint2std=>\&fqint2std);  
 &{$cmd_hash{$cmd}};  
 sub scarf2std {...} 
 sub fqint2std {...}


Security

File names

  • use open(IN,"<" ,filename) instead of open(IN,filename)
 filename could be "rm -fr * |" 
  • check file names for illegal characters
 $filename =~ /^([\w.-]+)$/)

Taint mode

 #!/usr/bin/perl -wT
 use strict;
 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
 $ENV{PATH} = "/usr/bin/:/usr/local/bin";

Profiling

 perl -d:DProf test.pl        => tmon.out
 dprofpp


CPAN

  • FAQ
  • Check if a module is installed
 perl -MModule -e 1
 perl -MXML::Merge -e 1
  • Edit CPAN configuration
 ~/.cpan/CPAN/MyConfig.pm
 'makepl_arg' => q[LIB=~/szdevel/CPAN/lib \
                   INSTALLBIN=~/szdevel/CPAN/bin \
                   INSTALLSCRIPT=~/szdevel/CPAN/bin \
                   INSTALLMAN1DIR=~/szdevel/CPAN/share/man/man1/ 
                   INSTALLMAN3DIR=~/szdevel/CPAN/share/man/man3/],

Bioperl