Perl: A Quick Tour
Presented by William Huston to STNYLUG Mar 25 2003 email: bhuston@vegdot.org phone: 607-724-1755 |
# method 1: open F, "/etc/passwd" or die "can't open /etc/passwd"; while ($line=<F>) { print $line; } close F; # method 2: open F, "/etc/passwd" or die "can't open /etc/passwd"; while (<F>) { # assigns to default scalar print; # prints default scalar } close F; |
# Note: this is the first use of $a : if (defined $a) { print "defined\n" } else { print "not defined\n" } if ($a) { print "true\n" } else { print "false\n" } # prints not defined, false $a=0; if (defined $a) { print "defined\n"} else {print "not defined\n"} if ($a) { print "true\n" } else {print "false\n" } # prints defined false $a=1; if (defined $a) { print "defined\n"} else {print "not defined\n"} if ($a) { print "true\n"} else {print "false\n"} # prints defined true |
left |
terms and list operators (leftward)
|
|
left |
-> |
object|class -> method , $hashref->{KEY},
$arrayref->[index] |
nonassoc |
++ -- |
pre/post inc/dec |
right
|
** |
exponentiation |
right |
! ~ \ and unary + and - |
logical not, bitwise negation, reference
op, etc |
left
|
=~ !~ |
String match/subst. binding |
left
|
* / % x |
mult, divide, modulus, string replicator |
left
|
+ - . |
|
left
|
<< >> |
bitshift |
nonassoc |
named unary operators |
|
nonassoc |
< > <= >= lt gt le ge
|
comparisons for numbers and strings |
nonassoc |
== != <=> eq ne cmp |
comparisons for numbers and strings |
left
|
& |
bitwise and |
left
|
| ^ |
bitwise or, xor |
left |
&& |
logical and |
left
|
|| |
logical or |
nonassoc |
.. ...
|
.. is range operator in list context .. and ... are bi-stable |
right |
?:
|
same as in C: condition ? statement-if-true : statement-if-false |
right
|
= += -= *= etc. |
|
left |
, => |
=> is similar to the comma operator,
but left operand is automatically placed in double quoted context. Used to
make hash constructors more readable. Also, used in "call by named parameter"
semantics. |
nonassoc |
list operators (rightward) |
|
right |
not |
|
left
|
and |
|
left
|
or xor |
#!/usr/local/bin/perl # trivial hex file dumper sub print_saved { print "\n$saved\n"; $saved=" "; } while (<>) { print "Dump for $ARGV" unless $count; foreach $c (split //, $_) { if (int($count/32) == ($count/32)) { print_saved; printf "%06x ", $count; } elsif (int($count/2) == ($count/2)) { print " "; $saved .= " "; } printf "%02x", ord($c); $saved .= ($c =~ /[ -~]/) ? "$c " : "**" ; $count ++; } if (eof) { print_saved; print "\n"; $count=0; } } |
#!/usr/bin/perl # "count" read items from stdin (each line is considered an item), then report how many of each we find: while (<>) { $foo{$_}++; } for $key (sort {$foo{$a} <=> $foo{$b}} keys %foo) { print "$foo{$key} $key"; } |
One - Liners from the command line: Prints userid and actual names from /etc/passwd: perl -ne '@a=split /:/; print "userid: $a[0], Name : $a[4]\n"' /etc/passwd |
#!/usr/bin/perl # multi_tail # just like tail -f, but for multiple files use FileHandle; # so we can have an array of filehandles if (!$ARGV[0]) { print "\n\nUsage: $0 file1 [file2 ... filen]\ntail -f on all files\n\n"; exit; } for $i (0..$#ARGV) { # build array of filehandles, open files for read, seek to the end. $fh = new FileHandle; if ($fh->open("< $ARGV[$i]")) { push @fh, $fh; $filename{$fh}=$ARGV[$i]; seek($fh, 0, 2); } else { warn "can't open $ARGV[$i] for read: $!"; } } if (!@fh) { print "$0: No valid filenames!\n"; exit; } while (1) { foreach $fh (@fh) { while (<$fh>) { print "$filename{$fh}: $_"; } seek($fh, 0, 1); } sleep 1; } |