-
Chap 2 - Scalar Var
-
Comparison Operators
- string
- numeric
- $input=<STDIN>;
- chmop and chop
- defined and undef
-
Chap 3 - Lists and Arrays
- $#arrayname - last element entry
- (1,'a',1..4,2.5,qw(kasd sdf rr),@arr)
-
list assignment or parallel assignment
- ($a, $b) = ($b, $a+$b)
- pop, push, shift, unshift
- foreach my $e (@arr) - !remember ()
- sort and reverse
-
scalar and list CONTEXT
- $count=@arr
- use fake function scalar to force
using a scalar context!
- chomp(@lines=<STDIN>);
#read till ctrl+D and remove all linefeeds.
-
Chap 4 - Subroutines
- sub my_add
{
my ($a, $b) = @_;
if ($a>$b) {$a;} else {$b;}
}
&my_add(1,2);
- ALWAYS use ampersand (&)
-
Chap 5 - Input and Output
- while(defined($line=<STDIO>)){print $line;}
===
while(<>){print;}
- print @a; vs print "@a";
- printf for formatted printing
- open FILE, ">>", "log" or die $!;
print FILE "haha, err again!";
-
Chap 6 - Hashes
- %h=("hello" => "salut", "thanks" => "merci");
- %h=('hello','salut',thanks','merci');
- while (($k,$v)=each %h){print $k.'=>'.$v;}
- modify, add, delete, exists, keys, values
- %ENV
-
Chap 7 - Regex
- metacharacters
- quantifiers
- grouping
- alternatives
- character classes
- character classes shortcuts
- shortcuts negating
-
Chap 8 - Matching with Regex
- //, m##,
- /i - case insensitive
- /s - let . match everything including \n
- /x - ignore \s
- anchors and word anchors - ^, $, \b, \B
- match vars - $1, $2, $3...
- noncapturing parentheses (?:hello)
- $`, $&, $'
- specific quantifiers
- perlrequick, perlretut, perlre
-
Chap 9 - Text Processing
- s#pattern#string#gisx
- case shifting with \U, \L, \E, \u\L
also available in double-quoted strings
- @a=split /pattern/, $s;
- $s=join $s, @a;
-
non-greedy quantifiers
vs.
greedy quantifiers
- diff performance
- diff result in some cases
-
/m for multi-line matching
- open F, $fn or die $!;
$s=join '',<F>;
$s=~s/^/$fn: /gm;
-
Chap 10 - More Control Structures
- unless, untill
- expression modifiers
- a naked block provides a lexical var context
- elsif
- for==foreach
- last, next, redo
- ?:, and, or, &&, ||
-
Chap 11 - Modules
- CPAN=Comprehensive Perl Archive Network
- perl -MCPAN -e shell
-
import a module
- use File::BaseName;
- use File::BaseName ();
- use File::BaseName qw/ basename /;
- databases and DBI
-
Chap 12 - File Tests
- -d, -e, -M
- if -r $file and -w _
is more efficient than
if -r $file and -w $file
- my($dev, $ino, $mode, $nlink,
$uid, $gid, $rdev, $size, $atime,
$mtime, $ctime, $blksize, $blocks)
= stat($filename);
-
Chap 13 - Dir Operations
- chdir
- glob "*.pm"; === <"*.pm">;
- opendir, readdir, closedir
- unlink to remove files
- rename "old" "new"
- link and symlink
- mkdir and rmdir
- chown, chmod
-
Chap 14 - Strings and Sorting
- find a sub string with index
- substr
- sprintf
-
advanced sorting with sort subroutine @arr;
- %score={"R",100,"Z",90,"P",100};
@winners=sort by_score_and_name keys %score;
sub by_score_and_name {
$score{$b}<=>$score{$a} or $a cmp $b}
- Chap 15 - Smart Matching and given-when
-
Chap 16 - Process Mgmt
- system - creates a new proc
- exec - replace the perl proc
-
`` - enable capturing the output
- $result = `who`;
- @lines = `who`;
- %ENV
-
piped open - open a concurrent child process
- open PS, 'ps aux|' or die $!;
chomp(my @lines) = <PS>;
- open MAIL, '|mail yawen' or die "cannot pipe to mail: $!";
print MAIL "hi, i am you!";
close MAIL;
die "non-zero return value: $?" if $?;
-
defined($pid=fork) or die $!;
unless($pid){
#Child process
exec 'date';
die $!;
}
#Parent process
waitpid($pid, 0);
- perlipc
-
sending and receiving signals
- kill 2, $pid or die $!;
- warn "$pid is gone!" unless (kill 0, $pid);
- $$ for a random value
-
Chap 17 - Advanced Techniques
-
trapping errors with eval
- eval {EXPRESSION}
- $@ for the err msg
- eval $str; is dangeous
-
advanced grep
- @odd_nu=grep{$_%2} 1..1000;
- @arr=grep{/$pattern/} <FILE>;
or
@arr=grep /$pattern/, <FILE>;
- map {&subr($_)} @arr;
-
parentheses required in list slices
- $mtime=(stat $file)[9];
-
array slices
- @new_arr=@arr[0,4,2,1]; - to pick and rearrange items in @arr to form @new_arr
-
hash slices
- @three_scores=@hash_name{qw/ ralph ruby stef /}
- http://ishare.iask.sina.com.cn/f/5139414.html
- PERL=Practical Extraction and Report Language
-
Perl is good for
- text processing
- sys admin
- ...as a glue language
- is a general-purpose language
- CPAN
-
resources
- perldoc
- perlmonks
-
orielly series
- learning perl
- intermediate perl
- mastering perl
- programming perl
- perl best practices
-
tools
- vim, Komodo