Dpuiu Perl: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 17: | Line 17: | ||
/fs/sz-user-supported/Linux-x86_64/lib | /fs/sz-user-supported/Linux-x86_64/lib | ||
* Translation: $_ =~ tr/abc/ABC/ | * Translation: $_ =~ tr/abc/ABC/ | ||
= Search directories = | = Search directories = | ||
Line 31: | Line 30: | ||
= Variables = | = Variables = | ||
* $. : input line number | * $. : input line number | ||
* $, : output 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 | |||
= Files= | = Files= | ||
* open(IN, '-') # Open standard input | * open(IN, '-') # Open standard input | ||
* open(IN, '>-') # Open standard output | * open(IN, '>-') # Open standard output | ||
* @lines = <IN> # read all lines in the file | * @lines = <IN> # read all lines in the file | ||
* $line = <IN> # read one line | * $line = <IN> # read one line | ||
= Hashes = | |||
* foreach $person (keys %ages) {} | |||
* foreach $age (values %ages) { } | |||
* while (($person, $age) = each(%ages)) {} |
Revision as of 02:20, 6 November 2008
POD
- POD; pod2html pod2latex pod2man pod2text pod2usage
In place editing
- Edit a file in place: -i
perl -i.bck script.pl xxx => xxx(new), xxx.bck(old)
BEGIN/END blocks
perl -ane 'BEGIN { print "sum\n"} END { print $sum,"\n" } $sum += $F[0]; '
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/
Search directories
- -Idir : same as "use lib dir_name"
default: /usr/include & /usr/lib/perl add dirs to PERLLIB
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"'
Variables
- $. : input line number
- $, : output 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
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)) {}