tchrist@convex.com (Tom Christiansen) (02/21/90)
Here is the troff source for the slides I presented at the perl mini-tutorial at USENIX in Washington last month. If you don't have troff, simple nroff versions of the macros are provided. Comments and suggestions are of course welcome. --tom #!/bin/sh # This is a shell archive. # Run the following text with /bin/sh to extract. sed -e 's/^X//' << \EOFMARK > README Xprint with 'troff macros.t slides' if you have troff, nitroff, ditroff. X (best copy) Xview with 'nroff macros.n slides | ul | {more,less} -f' if you have nroff. X (ok, not too hot) Xsee ascii otherwise. X (clear text) EOFMARK sed -e 's/^X//' << \EOFMARK > slides X.\" Copyright 1990 Tom Christiansen X.\" You are free to use these as you wish providing X.\" you give me credit for having originally written them. X.\" X.\" Start Quote macro -- place text in teletype font and indent X.de QS X.br X.in +3n X.ft TA X.nf X.. X.\" End Quote macro -- restore normal situation X.de QE X.in -3n X.fi X.ft R X.. X.\" quotes i might want to use X.ds qq \&"\" X.ds lq \&"\" X.ds rq \&"\" X.if t \ X. ds lq `` X.if t \ X. ds rq '' X.\" Begin Page macro -- go to next page, center emboldened args X.de BP X.bp X.if n \ X --------------------------------------- X.c "\\fB\\s20\\$1\\$2\\$3\\$4\\$5\\s0\\fR" X.sp X.. X.\" some sizes X.nr VS 21 X.vs 21 X.nr PS 16 X.ps 16 X.ta 8 16 24 32 40 48 56 64 72 X.\" =========================================================================== X.ce 7 X.au " \fIperl\fR tutorial -- \\\\n(% " X.sp 1 X\s36\fBPERL X.sp X\s18\fIa language by Larry Wall\fP X.sp 3 X.br X\s24Practical Extraction and Report Language X.sp 1 X\fIor\fB X.sp 1 X.br XPathologically Eclectic Rubbish Lister X.sp 3 X\s18\fITom Christiansen X\s-1CONVEX\s+1 Computer Corporation\fP X.\" =========================================================================== X.nr VS 21 X.vs 21 X.nr PS 16 X.ps 16 X.BP "Overview" X.2 XWhat is \fIPerl\fP: Xfeatures, Xwhere to get it, Xpreview X.sp X.2 XData Types: Xscalars and Xarrays X.sp X.2 XOperators X.sp X.2 XFlow Control X.sp X.2 XRegular Expressions X.sp X.2 XI/O: Xregular I/O, Xsystem functions, Xdirectory access, Xformatted I/O X.sp X.2 XFunctions and Subroutines: built-in array and string functions X.sp X.2 XEsoterica: Xsuid scripts, Xdebugging, Xpackages, Xcommand line options X.sp X.2 XExamples X.\" =========================================================================== X.BP "What is Perl?" X.2 XAn interpreted language that looks a lot like C with built-in X\fIsed\fP, \fIawk\fP, and \fIsh\fP, as well as bits of \fIcsh\fP, Pascal, \s-1FORTRAN\s+1, \s-1BASIC-PLUS\s0 Xthrown in. X.sp X.2 XHighly optimized for manipulating printable text, but also able to handle binary data. X.sp X.2 XEspecially suitable for system management tasks due to interfaces to Xmost common system calls. X.sp X.2 XRich enough for most general programming tasks. X.sp X.2 X\fI\*(lqA shell for C programmers.\*(rq\fR [Larry Wall] X.\" =========================================================================== X.BP "Features" X.2 XEasy to learn because much derives from existing tools. X.sp X.2 XMore rapid program development Xbecause it's an interpreter X.sp X.2 XFaster execution than shell script equivalents. X.sp X.2 XMore powerful than \fIsed\fP, \fIawk\fP, or \fIsh\fP; \fIa2p\fP and \fIs2p\fP Xtranslators supplied for your old scripts. X.sp X.2 XPortable across many different architectures. X.sp X.2 XAbsence of arbitrary limits like string length. X.sp X.2 XFits nicely into \s-1UNIX\s0 tool and filter philosophy. X.sp X.2 XIt's free! X.\" =========================================================================== X.BP "Where to get it" X.2 XAny \fIcomp.sources.unix\fP archive X.sp X.2 XFamous archive servers X.3 Xuunet.uu.net 192.48.96.2 X.3 Xtut.cis.ohio-state.edu 128.146.8.60 X.sp X.2 XIts author, Larry Wall <lwall@jpl-devvax.jpl.nasa.gov> X.3 Xjpl-devvax.jpl.nasa.gov 128.149.1.143 X.sp X.2 XPerl reference guide (in postscript form) also available from Ohio State, along Xwith some sample scripts and archives of the \fIperl-users\fP mailing list. X.sp X.2 X\s-1USENET\s0 newsgroup \fIcomp.lang.perl\fP good source for questions, Xcomments, examples. X.\" =========================================================================== X.BP "Preview" X.2 XIt's not for nothing that \fIperl\fP is sometimes called the \*(lqpathologically Xeclectic rubbish lister.\*(rq Before you drown in a deluge of features, Xhere's a simple example to whet your Xappetites that demonstrates the principal features of the language, all Xof which have been present since version 1. X.QS X Xwhile (<>) { X next if /^#/; X ($x, $y, $z) = /(\eS+)\es+(\ed\ed\ed)\es+(foo|bar)/; X $x =~ tr/a-z/A-Z/; X $seen{$x}++; X $z =~ s/foo/fear/ && $scared++; X printf "%s %08x %-10s\en", $z, $y, $x X if $seen{$x} > $y; X} X.QE X.\" =========================================================================== X.BP "Data Types" X.2 XBasic data types are scalars, Xindexed arrays of scalars, Xand associative arrays of scalars. X.sp X.2 XScalars themselves are either string, numeric, or boolean, depending on context. XValues of 0 (zero) and '' (null string) are false; all else is true. X.sp X.2 XType of variable determined by leading special character. X.3 "$ scalar" X.3 "@ indexed array (lists)" X.3 "% associative array" X.3 "& function" X.sp X.2 XAll data Xtypes have their own separate namespaces, as Xdo labels, functions, and file and directory handles. X.\" =========================================================================== X.BP "Data Types (scalars)" X.2 XUse a $ to indicate a scalar value X.sp X.QS X$foo = 3.14159; X.sp X$foo = 'red'; X.sp X$foo = "was $foo before"; # interpolate variable X.sp X$host = \s+2`\s0hostname\s+2`\s0; # note backticks X.sp X($foo, $bar, $glarch) = ('red', 'blue', 'green'); X.sp X($foo, $bar) = ($bar, $foo); # exchange X.QE X.\" =========================================================================== X.BP "Special Scalar Variables" X.2 XSpecial scalars are named with punctuation (except \fB$0\fP). Examples are X.sp X.3 X\fB$0\fP name of the currently executing script X.3 X\fB$_\fP default for pattern operators and implicit I/O X.3 X\fB$$\fP the current pid X.3 X\fB$!\fP the current system error message from \fIerrno\fP X.3 X\fB$?\fP status of last `backtick`, pipe, or system X.3 X\fB$|\fP whether output is buffered X.3 X\fB$.\fP the current line number of last input X.3 X\fB$[\fP array base, 0 by default; \fIawk\fP uses 1 X.3 X\fB$<\fP the real uid of the process X.3 X\fB$(\fP the real gid of the process X.3 X\fB$>\fP the effective uid of the process X.3 X\fB$)\fP the effective gid of the process X.\" =========================================================================== X.BP "Data types (arrays)" X.2 XIndexed arrays (lists); $ for one scalar element, @ for all X.QS X$foo[$i+2] = 3; # set one element to 3 X@foo = ( 1, 3, 5 );# init whole array X@foo = ( ) ; # initialize empty array X@foo = @bar; # copy whole @array X@foo = @bar[$i..$i+5]; # copy slice of @array X.QE X.sp X.2 X$#ARRAY is index of highest subscript, Xso the script's name is \fB$0\fP and its arguments run Xfrom $ARGV[0] through $ARGV[$#ARGV], inclusive. X.sp X.2 XAssociative (hashed) arrays; $ for one scalar element, % for all X.QS X$frogs{'green'} += 23; # 23 more green frogs X$location{$x, $y, $z} = 'troll'; # multi-dim array X%foo = %bar; # copy whole %array X@frogs{'green', 'blue', 'yellow'} = (3, 6, 9); X.QE X.\" =========================================================================== X.BP "Special Array Variables" X.2 X\fB@ARGV\fP command line arguments X.sp X.2 X\fB@INC\fP search path for files called with \fBdo\fP X.sp X.2 X\fB@_\fP default for \fBsplit\fP and subroutine parameters X.sp X.2 X\fB%ENV\fP the current environment; e.g. $ENV{'HOME'} X.sp X.2 X\fB%SIG\fP used to set signal handlers X.QS X Xsub trapped { X print STDERR "Interrupted\e007\en"; X exit 1; X} X$SIG{'INT'} = 'trapped'; X.QE X.\" =========================================================================== X.BP "Operators" X\fIPerl\fP uses all of C's operators except for type casting and `\fB\s+1&\fP\s0' and `\fB\s+1*\fP\s0' as address operators, plus these X.sp X.2 Xexponentiation: \fB**\fP, \fB**=\fP X.sp X.2 Xrange operator: \fB\s+1..\fP\s0 X.QS X$inheader = 1 if /^From / .. /^$/; Xif (1..10) { do foo(); } Xfor $i (60..75) { do foo($i); } X@new = @old[30..50]; X X.QE X.2 Xstring concatenation: \fB\s+1.\fP\s0, \fB\s+1.=\fP\s0 X.QS X X$x = $y \s+1.\s0 &frob(@list) \s+1.\s0 $z; X$x \s+1.\s0= "\en"; X.QE X.\" =========================================================================== X.BP "Operators (continued)" X.2 Xstring repetition: \fB\s+1x\fP\s0, \fB\s+1x=\fP\s0 X.QS X X$bar = '-' x 72; # row of 72 dashes X X.QE X.2 Xstring tests: \fBeq, ne, lt, gt, le, ge\fP X.QS X Xif ($x eq 'foo') { } Xif ($x ge 'red' ) { } X X.QE X.2 Xfile test operators like augmented \fI/bin/test\fP tests Xwork on strings or filehandles X.QS X Xif (-e $file) { } # file exists Xif (-z $file) { } # zero length Xif (-O LOG) { } # LOG owned by real uid Xdie "$file not a text file" unless -T $file; X.QE X.\" =========================================================================== X.BP "Flow Control" X.2 XUnlike C, blocks always require enclosing braces \s+2{}\s0 X.sp X.2 X\fBunless\fP and \fBuntil\fP are just \fBif\fP and \fBwhile\fP negated X.sp X.3 Xif (EXPR) BLOCK else BLOCK X.3 Xif (EXPR) BLOCK elsif (EXPR) BLOCK else BLOCK X.3 Xwhile (EXPR) BLOCK X.3 Xdo BLOCK while EXPR X.3 Xfor (EXPR; EXPR; EXPR) BLOCK X.3 Xforeach $VAR (LIST) BLOCK X.sp X.2 XFor readability, \fBif\fP, \fBunless\fP, \fBwhile\fP, and \fBuntil\fP may be used as trailing statement modifiers as in \s-1BASIC-PLUS\s0 X.QS X Xreturn -1 unless $x > 0; X.QE X.\" =========================================================================== X.BP "Flow Control (continued)" X.2 XUse \fBnext\fP and \fBlast\fP rather than C's \fBcontinue\fP and \fBbreak\fP X.sp X.2 X\fBredo\fP restarts the current iteration, ignoring the loop test X.sp X.2 XBlocks (and \fBnext\fP, \fBlast\fP, and \fBredo\fP) take optional labels for Xclearer loop control, avoiding the use of X\fBgoto\fP to exit nested loops. X.sp X.2 XNo \fBswitch\fP statement, but it's easy to roll your own X.sp X.2 X\fBdo\fP takes 3 forms X.3 Xexecute a block X.br X\f(TAdo { $x += $a[$i++] } until $i > $j;\fP X.3 Xexecute a subroutine X.br X\f(TAdo foo($x, $y);\fP X.3 Xexecute a file in current context X.br X\f(TAdo 'subroutines.pl';\fP X.\" =========================================================================== X.BP "Regular Expressions" X.2 XUnderstands \fIegrep\fP regexps, plus X.sp X.nf X.3 X\fB\ew\fP, \fB\eW\fP alphanumerics plus \s+2_\s0 (and negation) X.3 X\fB\ed\fP, \fB\eD\fP digits (and negation) X.3 X\fB\es\fP, \fB\eS\fP white space (and negation) X.3 X\fB\eb\fP, \fB\eB\fP word boundaries (and negation) X.fi X.sp X.2 XC-style escapes recognized, like \fB\et\fP, \fB\en\fP, \fB\e034\fP X.sp X.2 XDon't escape these characters for their special meaning:\ \ \fB(\ )\ |\ {\ }\ +\fP X.sp X.2 XCharacter classes may contain metas, e.g. \fB[\ew.$]\fP X.sp X.2 XSpecial variables: \fB$&\fP means all text matched, \fB$`\fP is text before match, \fB$'\fP is text after match. X.\" =========================================================================== X.BP "Regular Expressions (continued)" X.2 XUse \fB\e1\fP .. \fB\e9\fP within rexprs; \fB$1\fP .. \fB$9\fP outside X.QS X Xif (/^this (red|blue|green) (bat|ball) is \e1/) X { ($color, $object) = ($1, $2); } X($color, $object) = X /^this (red|blue|green) (bat|ball) is \e1/; X X.QE X.2 XSubstitute and translation operators are like \fIsed\fP's \fBs\fP and \fBy\fP. X.\" Substitutes recognize \fB/g\fP and \fB/i\fP switches. X.QS Xs/alpha/beta/; Xs/(.)\e1/$1/g; Xy/A-Z/a-z/; X X.QE X.2 XUse \fB=~\fP and \fB!~\fP to match against variables X.QS X Xif ($foo !~ /^\ew+$/) { exit 1; } X$foo =~ s/\ebtexas\eb/TX/i; X.QE X.\" =========================================================================== X.BP "I/O" X.2 XFilehandles have their own distinct namespaces, but are typically Xall upper case for clarity. Pre-defined filehandles are XSTDIN, STDOUT, STDERR. X.sp X.2 XMentioning a filehandle in angle brackets reads next line in scalar context, Xall lines in an array context; newlines are left intact. X.QS X X$line = <TEMP>; X@lines = <TEMP>; X X.QE X.2 X\fB<>\fR means all files supplied on command line (or STDIN if none). XWhen used this way, X$ARGV is the current filename. X.sp X.2 XWhen used in a \fBwhile\fP construct, input lines are Xautomatically assigned to the \fB$_\fP variable. X.BP "I/O (continued)" X.2 XUsually iterate over file a line at a time, assigning to \fB$_\fP Xeach time and using that as the default operand. X.QS X Xwhile ( <> ) { X next if /^#/; # skip comments X s/left/right/g; # global substitute X print; # print $_ X} X X.QE X.2 XIf not using the pseudo-file \fB<>\fP, open a filehandle: X.QS X Xopen (PWD, "/etc/passwd"); Xopen (TMP, ">/tmp/foobar.$$"); Xopen (LOG, ">>logfile"); Xopen (TOPIPE, "| lpr"); Xopen (FROMPIPE, "/usr/etc/netstat -a |"); X.QE X.\" .2 X.\" Filehandles may be used indirectly X.\" .QS X.\" $outfile = 'TEMP'; X.\" $line = <$outfile>; X.\" .QE X.\" =========================================================================== X.BP "I/O (continued)" X.2 XMay also use \fBgetc\fP for character I/O and \fBread\fP for raw I/O X.sp X.2 XAccess to \fBeof\fP, \fBseek\fP, \fBclose\fP, \fBflock\fP, \fBioctl\fP, \fBfcntl\fP, and \fBselect\fP calls for use with filehandles. X.sp X.2 XAccess to \fBmkdir\fP, \fPrmdir\fP, \fBchmod\fP, \fBchown\fP, \fBlink\fP, X\fBsymlink\fP (if supported), X\fBstat\fP, \fBrename\fP, \fBunlink\fP calls for use with filenames. X.sp X.2 XPass \fBprintf\fP a filehandle as its first argument unless printing to STDOUT X.QS X X\f(TAprintf LOG "%-8s %s: weird bits: %08x\en", X $program, &ctime, $bits;\fP X X.QE X.2 "Associative arrays may be bound to \fBdbm\fP files with \fBdbmopen()\fP" X.\" =========================================================================== X.BP "System Functions" XA plethora of functions from the C library are provided as built-ins, including Xmost system calls. These include X.sp X.2 X\fBchdir\fP, X\fBchroot\fP, X\fBexec\fP, X\fBexit\fP, X\fBfork\fP, X\fBgetlogin\fP, X\fBgetpgrp\fP, X\fBgetppid\fP, X\fBkill\fP, X\fBsetpgrp\fP, X\fBsetpriority\fP, X\fBsleep\fP, X\fBsyscall\fP, X\fBsystem\fP, X\fBtimes\fP, X\fBumask\fP, X\fBwait\fP. X.sp X.2 XIf your system has Berkeley-style networking, X\fBbind\fP, X\fBconnect\fP, X\fBsend\fP, X\fBgetsockname\fP, X\fBgetsockopt\fP, X\fBgetpeername\fP, X\fBrecv\fP, X\fBlisten\fP, X\fBsocket\fP, X\fBsocketpair\fP. X.sp X.2 X\fBgetpw*\fP, X\fBgetgr*\fP, X\fBgethost*\fP, X\fBgetnet*\fP, X\fBgetserv*\fP, and X\fBgetproto*\fP. X.sp X.2 X\fBpack\fP and \fBunpack\fP can be used Xfor manipulating binary data. X.QE X.\" =========================================================================== X.BP "Directory Access" X.1 XThree methods of accessing directories are provided. X.sp X.2 XYou may open a pipe from \fI/bin/ls\fP like this: X.QS Xopen(FILES,"/bin/ls *.c |"); Xwhile ($file = <FILES>) { chop($file); ... } X X.QE X.2 XThe directory-reading routines are provided as built-ins and operate Xon directory handles. Supported routines are X\fBopendir\fP, X\fBreaddir\fP, X\fBclosedir\fP, X\fBseekdir\fP, X\fBtelldir\fP, and X\fBrewinddir\fP. X.sp X.2 XThe easiest way is to use \fIperl\fP's file globbing notation. A string enclosed in Xangle brackets containing shell meta-characters evaluates to a list Xof matching filenames. X.QS X Xforeach $x ( <*.[ch]> ) { rename($x, "$x.old"); } Xchmod 0644, <*.c>; X.QE X.\" =========================================================================== X.BP "Subroutines" X.2 XSubroutines called either with `\fBdo\fP' operator or with `\fB&\fP'. XAny of the three principal data types may be passed as parameters Xor used as a return value. X.QS X.sp Xdo foo(1.43); X.sp Xdo foo(@list) X.sp X$x = &foo('red', 3, @others); X.sp X@list = &foo(@olist); X.sp X%foo = &foo($foo, @foo); X.QE X.\" =========================================================================== X.BP "Subroutines (continued)" X.2 XParameters are received by the subroutine in the special array \fB@_\fP. XIf desired, these can be copied to local variables. This is especially Xuseful for recursive subroutines. X.QS X X$result = &simple($alpha, $beta, @tutti); Xsub simple { X local($x, $y, @rest) = @_; X local($sum, %seen); X return $sum; X} X X.QE X.2 XSubroutines may also be called indirectly X.QS X X$foo = 'some_routine'; Xdo $foo(@list) X($x, $y, $z) = do $foo(%maps); X.QE X.\" =========================================================================== X.BP "Formatted I/O" X.2 XBesides \fBprintf\fP, formatted I/O can be done with \fBformat\fP and X\fBwrite\fP statements. X.sp X.2 XAutomatic pagination and printing of headers. X.sp X.2 XPicture description facilitates lining up multi-line output X.sp X.2 XFields in picture may be left or right-justified or centered X.sp X.2 XMulti-line text-block filling is provided, something like having a \fB%s\fP format string with a built-in pipe to \fBfmt\fP) X.sp X.2 XThese special scalar variables are useful: X.3 X\fB$%\fP for current page number, X.3 X\fB$=\fP for current page length (default 60) X.3 X\fB$-\fP for lines left on page X.\" =========================================================================== X.BP "Formatted I/O (example)" X.nf X.nr VS 12 X.nr PS 10 X.vs 12 X.ps 10 X\f(TA# a report from a bug report form; taken from perl man page Xformat top = X\& Bug Reports X@<<<<<<<<<<<<<<<<<<<<<<< @||| @>>>>>>>>>>>>>>>>>>>>>>> X$system, $%, $date X------------------------------------------------------------------ X\&. X X Xformat STDOUT = XSubject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< X\& $subject XIndex: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< X\& $index, $description XPriority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< X\& $priority, $date, $description XFrom: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< X\& $from, $description XAssigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< X\& $programmer, $description X\&~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< X\& $description X\&~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< X\& $description X\&~ ^<<<<<<<<<<<<<<<<<<<<<<<... X\& $description X\&.\s0\fP X.nr VS 21 X.nr PS 16 X.vs 21 X.ps 16 X.fi X.\" =========================================================================== X.BP "Built-in Array Functions" X.2 XIndexed arrays function as lists; you can add items to or remove them from Xeither end using these functions: X.3 X\fBpop\fP remove last value from end of array X.3 X\fBpush\fP add values to end of array X.3 X\fBshift\fP remove first value from front of array X.3 X\fBunshift\fP add values to front of array X.sp X.1 "For example" X.QS X Xpush(@list, $bar); Xpush(@list, @rest); X$tos = pop(@list); Xwhile ( $arg = shift(@ARGV) ) { } Xunshift( @ARGV, 'zeroth arg', 'first arg'); X.QE X.\" =========================================================================== X.BP "Built-in Array Functions (split and join)" X.2 X\fBsplit\fP breaks up a string into an array of new strings. You can X\fBsplit\fP on arbitrary regular expressions, limit the number of fields you X\fBsplit\fP into, and save the delimiters if you want. X.QS X X@list = split(/[, \et]+/, $expr); Xwhile (<PASSWD>) { X ($login, $passwd, $uid, $gid, $gcos, X $home, $shell) = split(/:/); X} X X.QE X.2 XThe inverse of \fBsplit\fP is \fBjoin\fP. X.QS X X$line = join(':', $login, $passwd, $uid, X $gid, $gcos, $home, $shell); X.QE X.\" =========================================================================== X.BP "Built-in Array Functions (sort, grep, reverse)" X.2 X\fBreverse\fP inverts a list. X.QS X Xforeach $tick (reverse 0..10) { } X.QE X.sp X.2 X\fBsort\fP returns a new array with the elements ordered according Xto their \s-1ASCII\s0 values. Use your own routine for different collating. X.QS X Xprint sort @list; Xsub numerically { $a - $b; } Xprint sort numerically @list; X X.QE X.2 X\fBgrep\fP returns a new list consisting of all the elements for which Xa given expression is true. For example, this Xwill delete all lines with leading pound signs: X.QS X X@lines = grep(!/^#/, @lines); X.QE X.\" =========================================================================== X.BP "Built-in Array Functions (%arrays)" XFor manipulating associative arrays, the \fBkeys\fP and \fBvalues\fP Xfunctions return indexed arrays of the indices and data values respectively. X\fPeach\fP is used to iterate through an associative array to retrieve Xone \fB($key,$value)\fP pair at a time. X.QS X Xwhile (($key,$value) = each %array) { X printf "%s is %s\en", $key, $value; X} X Xforeach $key (keys %array) { X printf "%s is %s\en", $key, $array{$key}; X} X Xprint reverse sort values %array; X.QE X.\" =========================================================================== X.BP "String functions" X.2 XBesides the powerful regular expression features, several well-known XC string manipulation functions are provided, including \fBcrypt\fP, X\fBindex\fP, \fBrindex\fP, \fBlength\fP, \fBsubstr\fP, and \fBsprintf\fP. X.sp X.2 XThe \fBchop\fP function efficiently removes the last character from a string. XIt's usually used to delete the trailing newline on input lines. XLike many \fIperl\fP operators, it works on \fB$_\fP Xif no operand is given. X.QS X Xchop($line); Xchop ($host = `hostname`); Xwhile (<STDIN>) { X chop; ... X} X.QE X.\" =========================================================================== X.BP "String functions (continued)" X.2 XThe \fBeval\fP operator lets you execute dynamically generated code. For Xexample, to process any command line arguments of the form \fBvariable=value\fP, Xplace this at the top of your script: X.QS X Xeval '$'.$1."'$2';" X while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift; X X.QE XThe \fBeval\fP operator is also useful for run-time testing of system-dependent features which would Xotherwise trigger fatal errors. For example, not all systems support the X\fBsymlink\fP or \fBdbmopen\fP; you could test for their existence by executing Xthe statements within an \fBeval\fP and testing the special variable \fB$@\fP, Xwhich contains the text of the run-time error message if anything went wrong. X.\" =========================================================================== X.BP "Suid Scripts" X.2 X\fIPerl\fP programs can be made to run setuid, Xand can actually be more secure than the corresponding \fIC\fP program. X.sp X.2 XBecause interpreters have no guarantee that the filename they get as the first Xargument is the same file that was \fBexec\fP'ed, \fIperl\fP Xwon't let your run a setuid script on a system where setuid scripts Xare not disabled. X.sp X.2 XUsing a dataflow tracing mechanism Xtriggered by setuid execution, perl can tell what data is safe to use and Xwhat data comes from an external source and thus is \*(lqtainted.\*(rq X.sp X.2 XTainted data may not be used directly or Xindirectly in any Xcommand that modifies files, directories or processes Xor else a fatal run-time error will result. X.\" =========================================================================== X.BP "Debugging and Packages" X.2 XWhen invoked with the \fB-d\fP switch, \fIperl\fP runs your program Xunder a symbolic debugger (written in \fIperl\fP) somewhat similar to \fIsdb\fP in syntax. XAmongst other things, Xbreakpoints may be set, variables examined or changed, and Xcall tracebacks printed out. Because it uses \fBeval\fP on your code, Xyou can execute any arbitrary perl code you want Xfrom the debugger. X.sp X.2 XUsing \fBpackage\fPs you can write modules with separate namespaces to Xavoid naming conflicts in library routines. The debugger uses this Xto keep its variables separate from yours. Variable are accessed by Xthe \fBpackage'name\fP notation, as in this line from the debugger: X.sp X.QS X$DB'stop[$DB'line] =~ s/;9$//; X.QE X.\" =========================================================================== X.BP "Command Line Options" XThe following are the more important command line switches recognized by \fIperl\fP: X.sp X.2 X\fB\-v\fP print out version string X.2 X\fB\-w\fP issue warnings about error-prone constructs X.2 X\fB\-d\fP run script under the debugger X.2 X\fB\-e\fP like \fIsed\fP: used to enter single command lines X.2 X\fB\-n\fP loop around input like \fIsed -n\fP X.2 X\fB\-p\fP as with \fB-n\fP but print out each line X.2 X\fB\-i\fP edit files in place X.2 X\fB\-a\fP turn on autosplit mode (like \fIawk\fP) into \fB@F\fP array X.2 X\fB\-P\fP call C pre-processor on script X.\" =========================================================================== X.BP "Examples: Command Line" X.QS X# output current version Xperl -v X X# simplest perl program Xperl -e 'print "hello, world.\en";' X X# useful at end of "find foo -print" Xperl -n -e 'chop;unlink;' X X# add first and last columns (filter) Xperl -a -n -e 'print $F[0] + $F[$#F], "\en";' X X# in-place edit of *.c files changing all foo to bar Xperl -p -i -e 's/\ebfoo\eb/bar/g;' *.c X X# run a script under the debugger Xperl -d myscript X.QE EOFMARK sed -e 's/^X//' << \EOFMARK > macros.n X.de c X.in 0 X.nr l 0 X.if \\nl .sp 1 X.ce X.if !\\$1 \\$1 X.. X.de 1 X.in \w' 'u X.ti -\w' 'u X.if !\\$1 \\$1 X.. X.de 2 X.in 0.5i X.ti -\w'\(bu 'u X\(bu\ \c X.if !\\$1 \\$1 X.. X.de 3 X.in 1.0i X.ti -\w'o 'u Xo\ \c X.if !\\$1 \\$1 X.. X.de 4 X.in 1.5i X.ti -\w'* 'u X*\ \c X.if !\\$1 \\$1 X.. EOFMARK sed -e 's/^X//' << \EOFMARK > macros.t X'\" macro package for making foils X'if \n(mo-0 .ds mo Jan X'if \n(mo-1 .ds mo Feb X'if \n(mo-2 .ds mo Mar X'if \n(mo-3 .ds mo Apr X'if \n(mo-4 .ds mo May X'if \n(mo-5 .ds mo Jun X'if \n(mo-6 .ds mo Jul X'if \n(mo-7 .ds mo Aug X'if \n(mo-8 .ds mo Sept X'if \n(mo-9 .ds mo Oct X'if \n(mo-10 .ds mo Nov X'if \n(mo-11 .ds mo Dec X'ds DA \\*(mo \n(dy, 19\n(yr X'\"fp 4 vS X'na X'ft B X'ps 24 X'vs 26 X'\" Parameters: X'nr PW 11i \" PW=paper width X'nr PH 8.5i \" PH=paper height X'nr MB 1i \" MB=margin border X'nr MT 0.5i \" MT=margin text X'nr BW \n(PWu-\n(MBu-\n(MBu \" BW=border width X'nr TW \n(BWu-\n(MTu-\n(MTu \" TW=text width X.nr C 0.5i \" C =corner radius X'nr PT \n(MBu+\n(MTu \" PT=page offset for text X'nr BB \n(PHu-\n(MBu \" BB=bottom of box X'\" 0.5i is the amount the convex C sticks up... X'nr CH 0.5i \" CH=height of convex logo (above base) X'nr BT \n(BBu-\n(CHu \" BT=bottom of text X'nr CW 2.6i \" CW=width of convex logo X' \" MW=width of middle title X' \" DW=width of date X'pl \n(PHu X'll \n(TWu X'lt \n(Twu X'ev 1 X'll \n(TWu X'lt \n(TWu X'ev X'po \n(PTu X'wh \n(BTu fa X'wh 0 hd X.de hd \" header X'sp \\n(MBu X'sp \\n(MTu X.. X'ds ff "''' \\*(DA '" X.de Ft \" footer X'ds ff \\$1 X.. X.de au X'ds ff "'' \\$1 ' \\*(DA '" X'ds RF \\$1 X.. X.de fa X.ch fa X.ev 1 X.sp |\\n(MBu X.po \\n(MBu X.nr W \\n(BWu-\\nCu-\\nCu \" actual width for box drawing X.nr H \\n(PHu-\\n(MBu-\\n(MBu-\\nCu-\\nCu \" actual height for box X'ie !\\*(RF 'nr MW \w'\s9 \\*(RF \s0'u X'el 'nr MW 0 X'nr DW \w'\s9 \\*(DA \s0'u \" DW=width of date X.nr a1 ((\\n(BWu-\\n(MWu)/2u)-\\nCu \" width of half of bottom straight line X.nr a2 \\n(a1u-\\n(CWu X.nr a3 \\n(a1u-\\n(DWu X.ps 36 X\v'\\nCu'\D'l 0 \\nHu'\D'a \\nCu 0 0 \\nCu'\h'\\n(CWu'\D'l \\n(a2u 0'\h'\\n(MWu'\D'l \\n(a3u 0'\h'\\n(DWu'\D'a 0 -\\nCu \\nCu 0'\D'l 0 -\\nHu'\D'a -\\nCu 0 0 -\\nCu'\D'l -\\nWu 0'\D'a 0 \\nCu -\\nCu 0' X.br X.ps X.po X.sp |\\n(BBu X'ps 9 X.sp 0.2 \" to center on the line instead of have baseline on line X'tl \\*(ff X'ps X.sp |\\n(BBu X.sp -0.46i X.bc b X\h'140u'\v'0u'\D'f 120u 0u -36u 6u -120u 0u' X.sp -1 X\h'79u'\v'16u'\D'f 120u 0u -14u 7u -120u 0u' X.sp -1 X\h'47u'\v'37u'\D'f 120u 0u -10u 10u -120u 0u' X.sp -1 X\h'25u'\v'61u'\D'f 120u 0u -9u 15u -120u 0u' X.sp -1 X\h'9u'\v'90u'\D'f 120u 0u -6u 20u -120u 0u' X.sp -1 X\h'121u'\v'121u'\D'f -1u 12u 0u 10u -120u 0u 0u -10u 1u -12u' X.sp -1 X\h'0u'\v'149u'\D'f 1u 14u 4u 17u 120u 0u -4u -17u -1u -14u' X.sp -1 X\h'125u'\v'181u'\D'f 4u 12u 5u 10u 5u 10u -120u 0u -5u -10u -5u -10u -4u -12u' X.sp -1 X\h'21u'\v'216u'\D'f 7u 10u 9u 10u 6u 7u 8u 7u 120u 0u -8u -7u -6u -7u -9u -10u -7u -10u' X.sp -1 X\h'53u'\v'252u'\D'f 17u 11u 13u 7u 18u 6u 19u 4u 20u 4u 120u 0u -20u -4u -19u -4u -18u -6u -13u -7u -17u -11u' X.sp -1 X\h'209u'\v'265u'\D'f 12u 5u 10u 3u 13u 2u 7u 1u 8u 0u 8u 0u 7u -1u 10u -2u 10u -3u 13u -5u' X.sp -1 X\h'217u'\v'239u'\D'f -6u 6u -9u 6u 130u 0u 8u -5u 7u -7u' X.sp -1 X\h'241u'\v'212u'\D'f -4u 5u -6u 9u 129u 0u 4u -5u 6u -9u' X.sp -1 X\h'256u'\v'177u'\D'f -3u 9u -4u 11u 129u 0u 4u -11u 3u -9u' X.sp -1 X\h'244u'\v'76u'\D'f 5u 10u 4u 10u 3u 11u 129u 0u -3u -11u -4u -10u -6u -10u' X.sp -1 X\h'212u'\v'38u'\D'f 13u 12u 8u 10u 8u 13u 129u 0u -9u -13u -9u -10u -12u -12u' X.sp -1 X\h'198u'\v'27u'\D'f 9u 7u 128u 0u -11u -7u -13u -7u -14u -5u -13u -4u -13u -3u -7u 0u -7u -1u -6u 1u -7u 0u -10u 2u -10u 3u -10u 4u -7u 4u' X.sp -1 X\h'1.30i'\v'0.58i'\f(ss\s24CONVEX\s0\fP X.sp -1 X'bp X.ev X.wh \\n(BTu fa X.. X'nr l 0 X.de c X.in 0 X.nr l 0 X.if \\nl .sp 1 X.ce X.if !\\$1 \\$1 X.. X.de 1 X.in \w' 'u X.ti -\w' 'u X.if !\\$1 \\$1 X.. X.de 2 X.in 0.5i X.ti -\w'\(bu 'u X\(bu\ \c X.if !\\$1 \\$1 X.. X.de 3 X.in 1.0i X.ti -\w'o 'u Xo\ \c X.if !\\$1 \\$1 X.. X.de 4 X.in 1.5i X.ti -\w'* 'u X*\ \c X.if !\\$1 \\$1 X.. X.de n X.in \\$1 X.ti -\w\\$2 X\\$2 X.sp -1 X.if !\\$3 \\$3 X.. X.ss 21 X.de b X.ft TA X.ps 20 X.vs 22 X.nf X.. X.de e X.ft B X.ps 20 X.vs 22 X.fi X.. X.b X.nr X 8*\w' 'u X.ta +\nXu +\nXu +\nXu +\nXu +\nXu +\nXu +\nXu +\nXu X.e X.de TS X.ch fa \\n(BTu+1v X.. EOFMARK sed -e 's/^X//' << \EOFMARK > ascii X X X PERL X X X a language by Larry Wall X X X Practical Extraction and Report Language X X X or X X X Pathologically Eclectic Rubbish Lister X X X Tom Christiansen X CONVEX Computer Corporation X X X --------------------------------------- X X Overview X X X o What is Perl: features, where to get it, preview X X X o Data Types: scalars and arrays X X X o Operators X X X o Flow Control X X X o Regular Expressions X X X o I/O: regular I/O, system functions, directory access, for- X matted I/O X X X o Functions and Subroutines: built-in array and string func- X X tions X X X o Esoterica: suid scripts, debugging, packages, command line X options X X X o Examples X X X --------------------------------------- X X What is Perl? X X X o An interpreted language that looks a lot like C with built- X in sed, awk, and sh, as well as bits of csh, Pascal, FOR- X X TRAN, BASIC-PLUS thrown in. X X X o Highly optimized for manipulating printable text, but also X able to handle binary data. X X X o Especially suitable for system management tasks due to in- X X terfaces to most common system calls. X X X o Rich enough for most general programming tasks. X X X o "A shell for C programmers." [Larry Wall] X X --------------------------------------- X X Features X X X o Easy to learn because much derives from existing tools. X X X o More rapid program development because it's an interpreter X X X o Faster execution than shell script equivalents. X X X o More powerful than sed, awk, or sh; a2p and s2p translators X supplied for your old scripts. X X X o Portable across many different architectures. X X X o Absence of arbitrary limits like string length. X X X o Fits nicely into UNIX tool and filter philosophy. X X X o It's free! X X X --------------------------------------- X X Where to get it X X X o Any comp.sources.unix archive X X X o Famous archive servers X o uunet.uu.net 192.48.96.2 X X o tut.cis.ohio-state.edu 128.146.8.60 X X X o Its author, Larry Wall <lwall@jpl-devvax.jpl.nasa.gov> X o jpl-devvax.jpl.nasa.gov 128.149.1.143 X X X o Perl reference guide (in postscript form) also available X X from Ohio State, along with some sample scripts and archives X of the perl-users mailing list. X X X o USENET newsgroup comp.lang.perl good source for questions, X X comments, examples. X X --------------------------------------- X X Preview X X X o It's not for nothing that perl is sometimes called the X "pathologically eclectic rubbish lister." Before you drown X X in a deluge of features, here's a simple example to whet X your appetites that demonstrates the principal features of X X the language, all of which have been present since version X 1. X X X while (<>) { X X next if /^#/; X ($x, $y, $z) = /(\S+)\s+(\d\d\d)\s+(foo|bar)/; X X $x =~ tr/a-z/A-Z/; X $seen{$x}++; X X $z =~ s/foo/fear/ && $scared++; X printf "%s %08x %-10s\n", $z, $y, $x X X if $seen{$x} > $y; X } X X X --------------------------------------- X X Data Types X X X o Basic data types are scalars, indexed arrays of scalars, and X associative arrays of scalars. X X X o Scalars themselves are either string, numeric, or boolean, X X depending on context. Values of 0 (zero) and '' (null X string) are false; all else is true. X X X o Type of variable determined by leading special character. X X o $ scalar X o @ indexed array (lists) X X o % associative array X o & function X X X o All data types have their own separate namespaces, as do la- X X bels, functions, and file and directory handles. X X --------------------------------------- X X Data Types (scalars) X X X o Use a $ to indicate a scalar value X X X $foo = 3.14159; X X X $foo = 'red'; X X X $foo = "was $foo before"; # interpolate variable X X X $host = `hostname`; # note backticks X X X ($foo, $bar, $glarch) = ('red', 'blue', 'green'); X X X ($foo, $bar) = ($bar, $foo); # exchange X X --------------------------------------- X X Special Scalar Variables X X X o Special scalars are named with punctuation (except $0). Ex- X amples are X X X o $0 name of the currently executing script X X o $_ default for pattern operators and implicit I/O X o $$ the current pid X X o $! the current system error message from errno X o $? status of last `backtick`, pipe, or system X X o $| whether output is buffered X o $. the current line number of last input X X o $[ array base, 0 by default; awk uses 1 X o $< the real uid of the process X X o $( the real gid of the process X o $> the effective uid of the process X X o $) the effective gid of the process X X --------------------------------------- X X Data types (arrays) X X X o Indexed arrays (lists); $ for one scalar element, @ for all X $foo[$i+2] = 3; # set one element to 3 X X @foo = ( 1, 3, 5 );# init whole array X @foo = ( ) ; # initialize empty array X X @foo = @bar; # copy whole @array X @foo = @bar[$i..$i+5]; # copy slice of @array X X X o $#ARRAY is index of highest subscript, so the script's name X X is $0 and its arguments run from $ARGV[0] through X $ARGV[$#ARGV], inclusive. X X X o Associative (hashed) arrays; $ for one scalar element, % for X X all X $frogs{'green'} += 23; # 23 more green frogs X X $location{$x, $y, $z} = 'troll'; # multi-dim array X %foo = %bar; # copy whole %array X X @frogs{'green', 'blue', 'yellow'} = (3, 6, 9); X X --------------------------------------- X X Special Array Variables X X X o @ARGV command line arguments X X X o @INC search path for files called with do X X X o @_ default for split and subroutine parameters X X X o %ENV the current enviroment; e.g. $ENV{'HOME'} X X X o %SIG used to set signal handlers X X X sub trapped { X print STDERR "Interrupted\007\n"; X X exit 1; X } X X $SIG{'INT'} = 'trapped'; X X --------------------------------------- X X Operators X X XPerl uses all of C's operators except for type casting and `&' Xand `*' as address operators, plus these X X X o exponentiation: **, **= X X X o range operator: .. X X $inheader = 1 if /^From / .. /^$/; X if (1..10) { do foo(); } X X for $i (60..75) { do foo($i); } X @new = @old[30..50]; X X X o string concatenation: ., .= X X X $x = $y . &frob(@list) . $z; X X $x .= "\n"; X X --------------------------------------- X X Operators (continued) X X X o string repetition: x, x= X X X $bar = '-' x 72; # row of 72 dashes X X X o string tests: eq, ne, lt, gt, le, ge X X X if ($x eq 'foo') { } X if ($x ge 'red' ) { } X X X o file test operators like augmented /bin/test tests work on X X strings or filehandles X X X if (-e $file) { } # file exists X if (-z $file) { } # zero length X X if (-O LOG) { } # LOG owned by real uid X die "$file not a text file" unless -T $file; X X X --------------------------------------- X X Flow Control X X X o Unlike C, blocks always require enclosing braces {} X X X o unless and until are just if and while negated X X X o if (EXPR) BLOCK else BLOCK X o if (EXPR) BLOCK elsif (EXPR) BLOCK else BLOCK X X o while (EXPR) BLOCK X o do BLOCK while EXPR X X o for (EXPR; EXPR; EXPR) BLOCK X o foreach $VAR (LIST) BLOCK X X X o For readability, if, unless, while, and until may be used as X X trailing statement modifiers as in BASIC-PLUS X X X return -1 unless $x > 0; X X --------------------------------------- X X Flow Control (continued) X X X o Use next and last rather than C's continue and break X X X o redo restarts the current iteration, ignoring the loop test X X X o Blocks (and next, last, and redo) take optional labels for X clearer loop control, avoiding the use of goto to exit nest- X X ed loops. X X X o No switch statement, but it's easy to roll your own X X X o do takes 3 forms X o execute a block X X do { $x += $a[$i++] } until $i > $j; X o execute a subroutine X X do foo($x, $y); X o execute a file in current context X X do 'subroutines.pl'; X X --------------------------------------- X X Regular Expressions X X X o Understands egrep regexps, plus X X X o \w, \W alphanumerics plus _ (and negation) X o \d, \D digits (and negation) X X o \s, \S white space (and negation) X o \b, \B word boundaries (and negation) X X X o C-style escapes recognized, like \t, \n, \034 X X X o Don't escape these characters for their special mean- X X ing: ( ) | { } + X X X o Character classes may contain metas, e.g. [\w.$] X X X o Special variables: $& means all text matched, $` is text be- X fore match, $' is text after match. X X X --------------------------------------- X X Regular Expressions (continued) X X X o Use \1 .. \9 within rexprs; $1 .. $9 outside X X X if (/^this (red|blue|green) (bat|ball) is \1/) X { ($color, $object) = ($1, $2); } X X ($color, $object) = X /^this (red|blue|green) (bat|ball) is \1/; X X X o Substitute and translation operators are like sed's s and y. X X s/alpha/beta/; X s/(.)\1/$1/g; X X y/A-Z/a-z/; X X X o Use =~ and !~ to match against variables X X X if ($foo !~ /^\w+$/) { exit 1; } X $foo =~ s/\btexas\b/TX/i; X X X --------------------------------------- X X I/O X X X o Filehandles have their own distinct namespaces, but are typ- X ically all upper case for clarity. Pre-defined filehandles X X are STDIN, STDOUT, STDERR. X X X o Mentioning a filehandle in angle brackets reads next line in X scalar context, all lines in an array context; newlines are X X left intact. X X X $line = <TEMP>; X @lines = <TEMP>; X X X o <> means all files supplied on command line (or STDIN if X X none). When used this way, $ARGV is the current filename. X X X o When used in a while construct, input lines are automatical- X ly assigned to the $_ variable. X X X --------------------------------------- X X I/O (continued) X X X o Usually iterate over file a line at a time, assigning to $_ X each time and using that as the default operand. X X X while ( <> ) { X X next if /^#/; # skip comments X s/left/right/g; # global substitute X X print; # print $_ X } X X X o If not using the pseudo-file <>, open a filehandle: X X X open (PWD, "/etc/passwd"); X X open (TMP, ">/tmp/foobar.$$"); X open (LOG, ">>logfile"); X X open (TOPIPE, "| lpr"); X open (FROMPIPE, "/usr/etc/netstat -a |"); X X X --------------------------------------- X X I/O (continued) X X X o May also use getc for character I/O and read for raw I/O X X X o Access to eof, seek, close, flock, ioctl, fcntl, and select X calls for use with filehandles. X X X o Access to mkdir, rmdir, chmod, chown, link, symlink (if sup- X X ported), stat, rename, unlink calls for use with filenames. X X X o Pass printf a filehandle as its first argument unless print- X ing to STDOUT X X X printf LOG "%-8s %s: weird bits: %08x\n", X X $program, &ctime, $bits; X X X o Associative arrays may be bound to dbm files with dbmopen() X X --------------------------------------- X X System Functions X X XA plethora of functions from the C library are provided as Xbuilt-ins, including most system calls. These include X X X o chdir, chroot, exec, exit, fork, getlogin, getpgrp, getppid, X X kill, setpgrp, setpriority, sleep, syscall, system, times, X umask, wait. X X X o If your system has Berkeley-style networking, bind, connect, X X send, getsockname, getsockopt, getpeername, recv, listen, X socket, socketpair. X X X o getpw*, getgr*, gethost*, getnet*, getserv*, and getproto*. X X X o pack and unpack can be used for manipulating binary data. X X X --------------------------------------- X X Directory Access X X XThree methods of accessing directories are provided. X X X o You may open a pipe from /bin/ls like this: X open(FILES,"/bin/ls *.c |"); X X while ($file = <FILES>) { chop($file); ... } X X X o The directory-reading routines are provided as built-ins and X operate on directory handles. Supported routines are open- X X dir, readdir, closedir, seekdir, telldir, and rewinddir. X X X o The easiest way is to use perl's file globbing notation. A X string enclosed in angle brackets containing shell meta- X X characters evaluates to a list of matching filenames. X X X foreach $x ( <*.[ch]> ) { rename($x, "$x.old"); } X chmod 0644, <*.c>; X X X --------------------------------------- X X Subroutines X X X o Subroutines called either with `do' operator or with `&'. X Any of the three principal data types may be passed as X X parameters or used as a return value. X X X do foo(1.43); X X X do foo(@list) X X X $x = &foo('red', 3, @others); X X X @list = &foo(@olist); X X X %foo = &foo($foo, @foo); X X --------------------------------------- X X Subroutines (continued) X X X o Parameters are received by the subroutine in the special ar- X ray @_. If desired, these can be copied to local variables. X X This is especially useful for recursive subroutines. X X X $result = &simple($alpha, $beta, @tutti); X sub simple { X X local($x, $y, @rest) = @_; X local($sum, %seen); X X return $sum; X } X X X o Subroutines may also be called indirectly X X X $foo = 'some_routine'; X X do $foo(@list) X ($x, $y, $z) = do $foo(%maps); X X X --------------------------------------- X X Formatted I/O X X X o Besides printf, formatted I/O can be done with format and X write statements. X X X o Automatic pagination and printing of headers. X X X o Picture description facilitates lining up multi-line output X X X o Fields in picture may be left or right-justified or centered X X X o Multi-line text-block filling is provided, something like X X having a %s format string with a built-in pipe to fmt) X X X o These special scalar variables are useful: X o $% for current page number, X X o $= for current page length (default 60) X o $- for lines left on page X X X --------------------------------------- X X Formatted I/O (example) X X# a report from a bug report form; taken from perl man page Xformat top = X Bug Reports X@<<<<<<<<<<<<<<<<<<<<<<< @||| @>>>>>>>>>>>>>>>>>>>>>>> X$system, $%, $date X------------------------------------------------------------------ X. X Xformat STDOUT = XSubject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< X $subject XIndex: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< X $index, $description XPriority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< X $priority, $date, $description XFrom: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< X $from, $description XAssigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< X $programmer, $description X~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< X $description X~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< X $description X~ ^<<<<<<<<<<<<<<<<<<<<<<<... X $description X. X X X --------------------------------------- X X Built-in Array Functions X X X o Indexed arrays function as lists; you can add items to or X remove them from either end using these functions: X X o pop remove last value from end of array X o push add values to end of array X X o shift remove first value from front of array X o unshift add values to front of array X X XFor example X X X push(@list, $bar); X X push(@list, @rest); X $tos = pop(@list); X X while ( $arg = shift(@ARGV) ) { } X unshift( @ARGV, 'zeroth arg', 'first arg'); X X X --------------------------------------- X X Built-in Array Functions (split and join) X X X o split breaks up a string into an array of new strings. You X can split on arbitrary regular expressions, limit the number X X of fields you split into, and save the delimiters if you X want. X X X @list = split(/[, \t]+/, $expr); X X while (<PASSWD>) { X ($login, $passwd, $uid, $gid, $gcos, X X $home, $shell) = split(/:/); X } X X X o The inverse of split is join. X X X $line = join(':', $login, $passwd, $uid, X X $gid, $gcos, $home, $shell); X X --------------------------------------- X X Built-in Array Functions (sort, grep, reverse) X X X o reverse inverts a list. X X X foreach $tick (reverse 0..10) { } X X X o sort returns a new array with the elements ordered according X to their ASCII values. Use your own routine for different X X collating. X X X print sort @list; X sub numerically { $a - $b; } X X print sort numerically @list; X X X o grep returns a new list consisting of all the elements for X which a given expression is true. For example, this will X X delete all lines with leading pound signs: X X X @lines = grep(!/^#/, @lines); X X --------------------------------------- X X Built-in Array Functions (%arrays) X X XFor manipulating associative arrays, the keys and values func- Xtions return indexed arrays of the indices and data values X Xrespectively. each is used to iterate through an associative ar- Xray to retrieve one ($key,$value) pair at a time. X X X while (($key,$value) = each %array) { X X printf "%s is %s\n", $key, $value; X } X X X foreach $key (keys %array) { X X printf "%s is %s\n", $key, $array{$key}; X } X X X print reverse sort values %array; X X X --------------------------------------- X X String functions X X X o Besides the powerful regular expression features, several X well-known C string manipulation functions are provided, in- X X cluding crypt, index, rindex, length, substr, and sprintf. X X X o The chop function efficiently removes the last character X from a string. It's usually used to delete the trailing X X newline on input lines. Like many perl operators, it works X on $_ if no operand is given. X X X chop($line); X X chop ($host = `hostname`); X while (<STDIN>) { X X chop; ... X } X X X --------------------------------------- X X String functions (continued) X X X o The eval operator lets you execute dynamically generated X code. For example, to process any command line arguments of X X the form variable=value, place this at the top of your X script: X X X eval '$'.$1."'$2';" X X while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift; X X X The eval operator is also useful for run-time testing of X system-dependent features which would otherwise trigger fa- X X tal errors. For example, not all systems support the sym- X link or dbmopen; you could test for their existence by exe- X X cuting the statements within an eval and testing the special X variable $@, which contains the text of the run-time error X X message if anything went wrong. X X --------------------------------------- X X Suid Scripts X X X o Perl programs can be made to run setuid, and can actually be X more secure than the corresponding C program. X X X o Because interpreters have no guarantee that the filename X X they get as the first argument is the same file that was X exec'ed, perl won't let your run a setuid script on a system X X where setuid scripts are not disabled. X X X o Using a dataflow tracing mechanism triggered by setuid exe- X cution, perl can tell what data is safe to use and what data X X comes from an external source and thus is "tainted." X X X o Tainted data may not be used directly or indirectly in any X command that modifies files, directories or processes or X X else a fatal run-time error will result. X X --------------------------------------- X X Debugging and Packages X X X o When invoked with the -d switch, perl runs your program X under a symbolic debugger (written in perl) somewhat similar X X to sdb in syntax. Amongst other things, breakpoints may be X set, variables examined or changed, and call tracebacks X X printed out. Because it uses eval on your code, you can ex- X ecute any arbitrary perl code you want from the debugger. X X X o Using packages you can write modules with separate X X namespaces to avoid naming conflicts in library routines. X The debugger uses this to keep its variables separate from X X yours. Variable are accessed by the package'name notation, X as in this line from the debugger: X X X $DB'stop[$DB'line] =~ s/;9$//; X X X --------------------------------------- X X Command Line Options X X XThe following are the more important command line switches recog- Xnized by perl: X X X o -v print out version string X X o -w issue warnings about error-prone constructs X o -d run script under the debugger X X o -e like sed: used to enter single command lines X o -n loop around input like sed -n X X o -p as with -n but print out each line X o -i edit files in place X X o -a turn on autosplit mode (like awk) into @F array X o -P call C pre-processor on script X X X --------------------------------------- X X Examples: Command Line X X X # output current version X perl -v X X X # simplest perl program X X perl -e 'print "hello, world.\n";' X X X # useful at end of "find foo -print" X perl -n -e 'chop;unlink;' X X X # add first and last columns (filter) X X perl -a -n -e 'print $F[0] + $F[$#F], "\n";' X X X # in-place edit of *.c files changing all foo to bar X perl -p -i -e 's/\bfoo\b/bar/g;' *.c X X X # run a script under the debugger X X perl -d myscript X EOFMARK exit 0 -- Tom Christiansen {uunet,uiucdcs,sun}!convex!tchrist Convex Computer Corporation tchrist@convex.COM "EMACS belongs in <sys/errno.h>: Editor too big!"