Links
Style
POD
- POD; pod2html pod2latex pod2man pod2text pod2usage
In place editing
perl -i.bck script.pl xxx
=> xxx(new), xxx.bck(old)
perl -i -pe 's/\r//g' file
perl -i -pe 's/\n/\r\n/' file
Cmd line options
perl -ane 'BEGIN { print "sum\n"} END { print $sum,"\n" } $sum += $F[0]; '
ls *pl | perl -pe 'print "perl -c "; '
Regular Expressions
echo $PERLLIB | perl -ane '/^(.+):/; print $1,"\n"'
echo $PERLLIB | perl -ane '/^(.+?):/; print $1,"\n"'
/fs/sz-user-supported/Linux-x86_64/lib
$_ =~ tr/abc/ABC/
while($text=~m/($pattern)/g) { print "$` $& $'\n"; }
Search directories
- -Idir : same as "use lib dir_name"
default: /usr/include & /usr/lib/perl
add dirs to PERLLIB env variable <=> @INC
perl -e '$,="\n"; print @INC ; '
perl -e 'foreach(@INC) { if(-d $_) {system "find $_ -type d -name Bio\n";}} '
perl -e 'foreach(@INC) { if(-d $_) {system "find $_ -type f -name SeqIO.pm\n";}} '
Command line arguments
- PERL5OPT : -[DIMUdmtw] switches
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"'
$ 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
$ perl Build.PL --prefix ~/szdevel/CPAN
$ ./Build test
$ ./Build install
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="")
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 {...}
Modules
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";
CPAN
- FAQ
- Check if a module is installed
perl -MModule -e 1
perl -MXML::Merge -e 1
~/.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