home *** CD-ROM | disk | FTP | other *** search
- .\" Copyright 1990 Tom Christiansen
- .\" You are free to use these as you wish providing
- .\" you give me credit for having originally written them.
- .\"
- .\" Start Quote macro -- place text in teletype font and indent
- .de QS
- .br
- .in +3n
- .ft TA
- .nf
- ..
- .\" End Quote macro -- restore normal situation
- .de QE
- .in -3n
- .fi
- .ft R
- ..
- .\" quotes i might want to use
- .ds qq \&"\"
- .ds lq \&"\"
- .ds rq \&"\"
- .if t \
- . ds lq ``
- .if t \
- . ds rq ''
- .\" Begin Page macro -- go to next page, center emboldened args
- .de BP
- .bp
- .if n \
- ---------------------------------------
- .c "\\fB\\s20\\$1\\$2\\$3\\$4\\$5\\s0\\fR"
- .sp
- ..
- .\" some sizes
- .nr VS 21
- .vs 21
- .nr PS 16
- .ps 16
- .ta 8 16 24 32 40 48 56 64 72
- .\" ===========================================================================
- .ce 7
- .au " \fIperl\fR tutorial -- \\\\n(% "
- .sp 1
- \s36\fBPERL
- .sp
- \s18\fIa language by Larry Wall\fP
- .sp 3
- .br
- \s24Practical Extraction and Report Language
- .sp 1
- \fIor\fB
- .sp 1
- .br
- Pathologically Eclectic Rubbish Lister
- .sp 3
- \s18\fITom Christiansen
- \s-1CONVEX\s+1 Computer Corporation\fP
- .\" ===========================================================================
- .nr VS 21
- .vs 21
- .nr PS 16
- .ps 16
- .BP "Overview"
- .2
- What is \fIPerl\fP:
- features,
- where to get it,
- preview
- .sp
- .2
- Data Types:
- scalars and
- arrays
- .sp
- .2
- Operators
- .sp
- .2
- Flow Control
- .sp
- .2
- Regular Expressions
- .sp
- .2
- I/O:
- regular I/O,
- system functions,
- directory access,
- formatted I/O
- .sp
- .2
- Functions and Subroutines: built-in array and string functions
- .sp
- .2
- Esoterica:
- suid scripts,
- debugging,
- packages,
- command line options
- .sp
- .2
- Examples
- .\" ===========================================================================
- .BP "What is Perl?"
- .2
- An interpreted language that looks a lot like C with built-in
- \fIsed\fP, \fIawk\fP, and \fIsh\fP, as well as bits of \fIcsh\fP, Pascal, \s-1FORTRAN\s+1, \s-1BASIC-PLUS\s0
- thrown in.
- .sp
- .2
- Highly optimized for manipulating printable text, but also able to handle binary data.
- .sp
- .2
- Especially suitable for system management tasks due to interfaces to
- most common system calls.
- .sp
- .2
- Rich enough for most general programming tasks.
- .sp
- .2
- \fI\*(lqA shell for C programmers.\*(rq\fR [Larry Wall]
- .\" ===========================================================================
- .BP "Features"
- .2
- Easy to learn because much derives from existing tools.
- .sp
- .2
- More rapid program development
- because it's an interpreter
- .sp
- .2
- Faster execution than shell script equivalents.
- .sp
- .2
- More powerful than \fIsed\fP, \fIawk\fP, or \fIsh\fP; \fIa2p\fP and \fIs2p\fP
- translators supplied for your old scripts.
- .sp
- .2
- Portable across many different architectures.
- .sp
- .2
- Absence of arbitrary limits like string length.
- .sp
- .2
- Fits nicely into \s-1UNIX\s0 tool and filter philosophy.
- .sp
- .2
- It's free!
- .\" ===========================================================================
- .BP "Where to get it"
- .2
- Any \fIcomp.sources.unix\fP archive
- .sp
- .2
- Famous archive servers
- .3
- uunet.uu.net 192.48.96.2
- .3
- tut.cis.ohio-state.edu 128.146.8.60
- .sp
- .2
- Its author, Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
- .3
- jpl-devvax.jpl.nasa.gov 128.149.1.143
- .sp
- .2
- Perl reference guide (in postscript form) also available from Ohio State, along
- with some sample scripts and archives of the \fIperl-users\fP mailing list.
- .sp
- .2
- \s-1USENET\s0 newsgroup \fIcomp.lang.perl\fP good source for questions,
- comments, examples.
- .\" ===========================================================================
- .BP "Preview"
- .2
- It's not for nothing that \fIperl\fP is sometimes called the \*(lqpathologically
- eclectic rubbish lister.\*(rq Before you drown in a deluge of features,
- here's a simple example to whet your
- appetites that demonstrates the principal features of the language, all
- of which have been present since version 1.
- .QS
-
- while (<>) {
- next if /^#/;
- ($x, $y, $z) = /(\eS+)\es+(\ed\ed\ed)\es+(foo|bar)/;
- $x =~ tr/a-z/A-Z/;
- $seen{$x}++;
- $z =~ s/foo/fear/ && $scared++;
- printf "%s %08x %-10s\en", $z, $y, $x
- if $seen{$x} > $y;
- }
- .QE
- .\" ===========================================================================
- .BP "Data Types"
- .2
- Basic data types are scalars,
- indexed arrays of scalars,
- and associative arrays of scalars.
- .sp
- .2
- Scalars themselves are either string, numeric, or boolean, depending on context.
- Values of 0 (zero) and '' (null string) are false; all else is true.
- .sp
- .2
- Type of variable determined by leading special character.
- .3 "$ scalar"
- .3 "@ indexed array (lists)"
- .3 "% associative array"
- .3 "& function"
- .sp
- .2
- All data
- types have their own separate namespaces, as
- do labels, functions, and file and directory handles.
- .\" ===========================================================================
- .BP "Data Types (scalars)"
- .2
- Use a $ to indicate a scalar value
- .sp
- .QS
- $foo = 3.14159;
- .sp
- $foo = 'red';
- .sp
- $foo = "was $foo before"; # interpolate variable
- .sp
- $host = \s+2`\s0hostname\s+2`\s0; # note backticks
- .sp
- ($foo, $bar, $glarch) = ('red', 'blue', 'green');
- .sp
- ($foo, $bar) = ($bar, $foo); # exchange
- .QE
- .\" ===========================================================================
- .BP "Special Scalar Variables"
- .2
- Special scalars are named with punctuation (except \fB$0\fP). Examples are
- .sp
- .3
- \fB$0\fP name of the currently executing script
- .3
- \fB$_\fP default for pattern operators and implicit I/O
- .3
- \fB$$\fP the current pid
- .3
- \fB$!\fP the current system error message from \fIerrno\fP
- .3
- \fB$?\fP status of last `backtick`, pipe, or system
- .3
- \fB$|\fP whether output is buffered
- .3
- \fB$.\fP the current line number of last input
- .3
- \fB$[\fP array base, 0 by default; \fIawk\fP uses 1
- .3
- \fB$<\fP the real uid of the process
- .3
- \fB$(\fP the real gid of the process
- .3
- \fB$>\fP the effective uid of the process
- .3
- \fB$)\fP the effective gid of the process
- .\" ===========================================================================
- .BP "Data types (arrays)"
- .2
- Indexed arrays (lists); $ for one scalar element, @ for all
- .QS
- $foo[$i+2] = 3; # set one element to 3
- @foo = ( 1, 3, 5 );# init whole array
- @foo = ( ) ; # initialize empty array
- @foo = @bar; # copy whole @array
- @foo = @bar[$i..$i+5]; # copy slice of @array
- .QE
- .sp
- .2
- $#ARRAY is index of highest subscript,
- so the script's name is \fB$0\fP and its arguments run
- from $ARGV[0] through $ARGV[$#ARGV], inclusive.
- .sp
- .2
- Associative (hashed) arrays; $ for one scalar element, % for all
- .QS
- $frogs{'green'} += 23; # 23 more green frogs
- $location{$x, $y, $z} = 'troll'; # multi-dim array
- %foo = %bar; # copy whole %array
- @frogs{'green', 'blue', 'yellow'} = (3, 6, 9);
- .QE
- .\" ===========================================================================
- .BP "Special Array Variables"
- .2
- \fB@ARGV\fP command line arguments
- .sp
- .2
- \fB@INC\fP search path for files called with \fBdo\fP
- .sp
- .2
- \fB@_\fP default for \fBsplit\fP and subroutine parameters
- .sp
- .2
- \fB%ENV\fP the current enviroment; e.g. $ENV{'HOME'}
- .sp
- .2
- \fB%SIG\fP used to set signal handlers
- .QS
-
- sub trapped {
- print STDERR "Interrupted\e007\en";
- exit 1;
- }
- $SIG{'INT'} = 'trapped';
- .QE
- .\" ===========================================================================
- .BP "Operators"
- \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
- .sp
- .2
- exponentiation: \fB**\fP, \fB**=\fP
- .sp
- .2
- range operator: \fB\s+1..\fP\s0
- .QS
- $inheader = 1 if /^From / .. /^$/;
- if (1..10) { do foo(); }
- for $i (60..75) { do foo($i); }
- @new = @old[30..50];
-
- .QE
- .2
- string concatenation: \fB\s+1.\fP\s0, \fB\s+1.=\fP\s0
- .QS
-
- $x = $y \s+1.\s0 &frob(@list) \s+1.\s0 $z;
- $x \s+1.\s0= "\en";
- .QE
- .\" ===========================================================================
- .BP "Operators (continued)"
- .2
- string repetition: \fB\s+1x\fP\s0, \fB\s+1x=\fP\s0
- .QS
-
- $bar = '-' x 72; # row of 72 dashes
-
- .QE
- .2
- string tests: \fBeq, ne, lt, gt, le, ge\fP
- .QS
-
- if ($x eq 'foo') { }
- if ($x ge 'red' ) { }
-
- .QE
- .2
- file test operators like augmented \fI/bin/test\fP tests
- work on strings or filehandles
- .QS
-
- if (-e $file) { } # file exists
- if (-z $file) { } # zero length
- if (-O LOG) { } # LOG owned by real uid
- die "$file not a text file" unless -T $file;
- .QE
- .\" ===========================================================================
- .BP "Flow Control"
- .2
- Unlike C, blocks always require enclosing braces \s+2{}\s0
- .sp
- .2
- \fBunless\fP and \fBuntil\fP are just \fBif\fP and \fBwhile\fP negated
- .sp
- .3
- if (EXPR) BLOCK else BLOCK
- .3
- if (EXPR) BLOCK elsif (EXPR) BLOCK else BLOCK
- .3
- while (EXPR) BLOCK
- .3
- do BLOCK while EXPR
- .3
- for (EXPR; EXPR; EXPR) BLOCK
- .3
- foreach $VAR (LIST) BLOCK
- .sp
- .2
- For readability, \fBif\fP, \fBunless\fP, \fBwhile\fP, and \fBuntil\fP may be used as trailing statement modifiers as in \s-1BASIC-PLUS\s0
- .QS
-
- return -1 unless $x > 0;
- .QE
- .\" ===========================================================================
- .BP "Flow Control (continued)"
- .2
- Use \fBnext\fP and \fBlast\fP rather than C's \fBcontinue\fP and \fBbreak\fP
- .sp
- .2
- \fBredo\fP restarts the current iteration, ignoring the loop test
- .sp
- .2
- Blocks (and \fBnext\fP, \fBlast\fP, and \fBredo\fP) take optional labels for
- clearer loop control, avoiding the use of
- \fBgoto\fP to exit nested loops.
- .sp
- .2
- No \fBswitch\fP statement, but it's easy to roll your own
- .sp
- .2
- \fBdo\fP takes 3 forms
- .3
- execute a block
- .br
- \f(TAdo { $x += $a[$i++] } until $i > $j;\fP
- .3
- execute a subroutine
- .br
- \f(TAdo foo($x, $y);\fP
- .3
- execute a file in current context
- .br
- \f(TAdo 'subroutines.pl';\fP
- .\" ===========================================================================
- .BP "Regular Expressions"
- .2
- Understands \fIegrep\fP regexps, plus
- .sp
- .nf
- .3
- \fB\ew\fP, \fB\eW\fP alphanumerics plus \s+2_\s0 (and negation)
- .3
- \fB\ed\fP, \fB\eD\fP digits (and negation)
- .3
- \fB\es\fP, \fB\eS\fP white space (and negation)
- .3
- \fB\eb\fP, \fB\eB\fP word boundaries (and negation)
- .fi
- .sp
- .2
- C-style escapes recognized, like \fB\et\fP, \fB\en\fP, \fB\e034\fP
- .sp
- .2
- Don't escape these characters for their special meaning:\ \ \fB(\ )\ |\ {\ }\ +\fP
- .sp
- .2
- Character classes may contain metas, e.g. \fB[\ew.$]\fP
- .sp
- .2
- Special variables: \fB$&\fP means all text matched, \fB$`\fP is text before match, \fB$'\fP is text after match.
- .\" ===========================================================================
- .BP "Regular Expressions (continued)"
- .2
- Use \fB\e1\fP .. \fB\e9\fP within rexprs; \fB$1\fP .. \fB$9\fP outside
- .QS
-
- if (/^this (red|blue|green) (bat|ball) is \e1/)
- { ($color, $object) = ($1, $2); }
- ($color, $object) =
- /^this (red|blue|green) (bat|ball) is \e1/;
-
- .QE
- .2
- Substitute and translation operators are like \fIsed\fP's \fBs\fP and \fBy\fP.
- .\" Substitutes recognize \fB/g\fP and \fB/i\fP switches.
- .QS
- s/alpha/beta/;
- s/(.)\e1/$1/g;
- y/A-Z/a-z/;
-
- .QE
- .2
- Use \fB=~\fP and \fB!~\fP to match against variables
- .QS
-
- if ($foo !~ /^\ew+$/) { exit 1; }
- $foo =~ s/\ebtexas\eb/TX/i;
- .QE
- .\" ===========================================================================
- .BP "I/O"
- .2
- Filehandles have their own distinct namespaces, but are typically
- all upper case for clarity. Pre-defined filehandles are
- STDIN, STDOUT, STDERR.
- .sp
- .2
- Mentioning a filehandle in angle brackets reads next line in scalar context,
- all lines in an array context; newlines are left intact.
- .QS
-
- $line = <TEMP>;
- @lines = <TEMP>;
-
- .QE
- .2
- \fB<>\fR means all files supplied on command line (or STDIN if none).
- When used this way,
- $ARGV is the current filename.
- .sp
- .2
- When used in a \fBwhile\fP construct, input lines are
- automatically assigned to the \fB$_\fP variable.
- .BP "I/O (continued)"
- .2
- Usually iterate over file a line at a time, assigning to \fB$_\fP
- each time and using that as the default operand.
- .QS
-
- while ( <> ) {
- next if /^#/; # skip comments
- s/left/right/g; # global substitute
- print; # print $_
- }
-
- .QE
- .2
- If not using the pseudo-file \fB<>\fP, open a filehandle:
- .QS
-
- open (PWD, "/etc/passwd");
- open (TMP, ">/tmp/foobar.$$");
- open (LOG, ">>logfile");
- open (TOPIPE, "| lpr");
- open (FROMPIPE, "/usr/etc/netstat -a |");
- .QE
- .\" .2
- .\" Filehandles may be used indirectly
- .\" .QS
- .\" $outfile = 'TEMP';
- .\" $line = <$outfile>;
- .\" .QE
- .\" ===========================================================================
- .BP "I/O (continued)"
- .2
- May also use \fBgetc\fP for character I/O and \fBread\fP for raw I/O
- .sp
- .2
- Access to \fBeof\fP, \fBseek\fP, \fBclose\fP, \fBflock\fP, \fBioctl\fP, \fBfcntl\fP, and \fBselect\fP calls for use with filehandles.
- .sp
- .2
- Access to \fBmkdir\fP, \fPrmdir\fP, \fBchmod\fP, \fBchown\fP, \fBlink\fP,
- \fBsymlink\fP (if supported),
- \fBstat\fP, \fBrename\fP, \fBunlink\fP calls for use with filenames.
- .sp
- .2
- Pass \fBprintf\fP a filehandle as its first argument unless printing to STDOUT
- .QS
-
- \f(TAprintf LOG "%-8s %s: weird bits: %08x\en",
- $program, &ctime, $bits;\fP
-
- .QE
- .2 "Associative arrays may be bound to \fBdbm\fP files with \fBdbmopen()\fP"
- .\" ===========================================================================
- .BP "System Functions"
- A plethora of functions from the C library are provided as built-ins, including
- most system calls. These include
- .sp
- .2
- \fBchdir\fP,
- \fBchroot\fP,
- \fBexec\fP,
- \fBexit\fP,
- \fBfork\fP,
- \fBgetlogin\fP,
- \fBgetpgrp\fP,
- \fBgetppid\fP,
- \fBkill\fP,
- \fBsetpgrp\fP,
- \fBsetpriority\fP,
- \fBsleep\fP,
- \fBsyscall\fP,
- \fBsystem\fP,
- \fBtimes\fP,
- \fBumask\fP,
- \fBwait\fP.
- .sp
- .2
- If your system has Berkeley-style networking,
- \fBbind\fP,
- \fBconnect\fP,
- \fBsend\fP,
- \fBgetsockname\fP,
- \fBgetsockopt\fP,
- \fBgetpeername\fP,
- \fBrecv\fP,
- \fBlisten\fP,
- \fBsocket\fP,
- \fBsocketpair\fP.
- .sp
- .2
- \fBgetpw*\fP,
- \fBgetgr*\fP,
- \fBgethost*\fP,
- \fBgetnet*\fP,
- \fBgetserv*\fP, and
- \fBgetproto*\fP.
- .sp
- .2
- \fBpack\fP and \fBunpack\fP can be used
- for manipulating binary data.
- .QE
- .\" ===========================================================================
- .BP "Directory Access"
- .1
- Three methods of accessing directories are provided.
- .sp
- .2
- You may open a pipe from \fI/bin/ls\fP like this:
- .QS
- open(FILES,"/bin/ls *.c |");
- while ($file = <FILES>) { chop($file); ... }
-
- .QE
- .2
- The directory-reading routines are provided as built-ins and operate
- on directory handles. Supported routines are
- \fBopendir\fP,
- \fBreaddir\fP,
- \fBclosedir\fP,
- \fBseekdir\fP,
- \fBtelldir\fP, and
- \fBrewinddir\fP.
- .sp
- .2
- The easiest way is to use \fIperl\fP's file globbing notation. A string enclosed in
- angle brackets containing shell meta-characters evaluates to a list
- of matching filenames.
- .QS
-
- foreach $x ( <*.[ch]> ) { rename($x, "$x.old"); }
- chmod 0644, <*.c>;
- .QE
- .\" ===========================================================================
- .BP "Subroutines"
- .2
- Subroutines called either with `\fBdo\fP' operator or with `\fB&\fP'.
- Any of the three principal data types may be passed as parameters
- or used as a return value.
- .QS
- .sp
- do foo(1.43);
- .sp
- do foo(@list)
- .sp
- $x = &foo('red', 3, @others);
- .sp
- @list = &foo(@olist);
- .sp
- %foo = &foo($foo, @foo);
- .QE
- .\" ===========================================================================
- .BP "Subroutines (continued)"
- .2
- Parameters are received by the subroutine in the special array \fB@_\fP.
- If desired, these can be copied to local variables. This is especially
- useful for recursive subroutines.
- .QS
-
- $result = &simple($alpha, $beta, @tutti);
- sub simple {
- local($x, $y, @rest) = @_;
- local($sum, %seen);
- return $sum;
- }
-
- .QE
- .2
- Subroutines may also be called indirectly
- .QS
-
- $foo = 'some_routine';
- do $foo(@list)
- ($x, $y, $z) = do $foo(%maps);
- .QE
- .\" ===========================================================================
- .BP "Formatted I/O"
- .2
- Besides \fBprintf\fP, formatted I/O can be done with \fBformat\fP and
- \fBwrite\fP statements.
- .sp
- .2
- Automatic pagination and printing of headers.
- .sp
- .2
- Picture description facilitates lining up multi-line output
- .sp
- .2
- Fields in picture may be left or right-justified or centered
- .sp
- .2
- Multi-line text-block filling is provided, something like having a \fB%s\fP format string with a built-in pipe to \fBfmt\fP)
- .sp
- .2
- These special scalar variables are useful:
- .3
- \fB$%\fP for current page number,
- .3
- \fB$=\fP for current page length (default 60)
- .3
- \fB$-\fP for lines left on page
- .\" ===========================================================================
- .BP "Formatted I/O (example)"
- .nf
- .nr VS 12
- .nr PS 10
- .vs 12
- .ps 10
- \f(TA# a report from a bug report form; taken from perl man page
- format top =
- \& Bug Reports
- @<<<<<<<<<<<<<<<<<<<<<<< @||| @>>>>>>>>>>>>>>>>>>>>>>>
- $system, $%, $date
- ------------------------------------------------------------------
- \&.
-
-
- format STDOUT =
- Subject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- \& $subject
- Index: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- \& $index, $description
- Priority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- \& $priority, $date, $description
- From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- \& $from, $description
- Assigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- \& $programmer, $description
- \&~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- \& $description
- \&~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- \& $description
- \&~ ^<<<<<<<<<<<<<<<<<<<<<<<...
- \& $description
- \&.\s0\fP
- .nr VS 21
- .nr PS 16
- .vs 21
- .ps 16
- .fi
- .\" ===========================================================================
- .BP "Built-in Array Functions"
- .2
- Indexed arrays function as lists; you can add items to or remove them from
- either end using these functions:
- .3
- \fBpop\fP remove last value from end of array
- .3
- \fBpush\fP add values to end of array
- .3
- \fBshift\fP remove first value from front of array
- .3
- \fBunshift\fP add values to front of array
- .sp
- .1 "For example"
- .QS
-
- push(@list, $bar);
- push(@list, @rest);
- $tos = pop(@list);
- while ( $arg = shift(@ARGV) ) { }
- unshift( @ARGV, 'zeroth arg', 'first arg');
- .QE
- .\" ===========================================================================
- .BP "Built-in Array Functions (split and join)"
- .2
- \fBsplit\fP breaks up a string into an array of new strings. You can
- \fBsplit\fP on arbitrary regular expressions, limit the number of fields you
- \fBsplit\fP into, and save the delimiters if you want.
- .QS
-
- @list = split(/[, \et]+/, $expr);
- while (<PASSWD>) {
- ($login, $passwd, $uid, $gid, $gcos,
- $home, $shell) = split(/:/);
- }
-
- .QE
- .2
- The inverse of \fBsplit\fP is \fBjoin\fP.
- .QS
-
- $line = join(':', $login, $passwd, $uid,
- $gid, $gcos, $home, $shell);
- .QE
- .\" ===========================================================================
- .BP "Built-in Array Functions (sort, grep, reverse)"
- .2
- \fBreverse\fP inverts a list.
- .QS
-
- foreach $tick (reverse 0..10) { }
- .QE
- .sp
- .2
- \fBsort\fP returns a new array with the elements ordered according
- to their \s-1ASCII\s0 values. Use your own routine for different collating.
- .QS
-
- print sort @list;
- sub numerically { $a - $b; }
- print sort numerically @list;
-
- .QE
- .2
- \fBgrep\fP returns a new list consisting of all the elements for which
- a given expression is true. For example, this
- will delete all lines with leading pound signs:
- .QS
-
- @lines = grep(!/^#/, @lines);
- .QE
- .\" ===========================================================================
- .BP "Built-in Array Functions (%arrays)"
- For manipulating associative arrays, the \fBkeys\fP and \fBvalues\fP
- functions return indexed arrays of the indices and data values respectively.
- \fPeach\fP is used to iterate through an associative array to retrieve
- one \fB($key,$value)\fP pair at a time.
- .QS
-
- while (($key,$value) = each %array) {
- printf "%s is %s\en", $key, $value;
- }
-
- foreach $key (keys %array) {
- printf "%s is %s\en", $key, $array{$key};
- }
-
- print reverse sort values %array;
- .QE
- .\" ===========================================================================
- .BP "String functions"
- .2
- Besides the powerful regular expression features, several well-known
- C string manipulation functions are provided, including \fBcrypt\fP,
- \fBindex\fP, \fBrindex\fP, \fBlength\fP, \fBsubstr\fP, and \fBsprintf\fP.
- .sp
- .2
- The \fBchop\fP function efficiently removes the last character from a string.
- It's usually used to delete the trailing newline on input lines.
- Like many \fIperl\fP operators, it works on \fB$_\fP
- if no operand is given.
- .QS
-
- chop($line);
- chop ($host = `hostname`);
- while (<STDIN>) {
- chop; ...
- }
- .QE
- .\" ===========================================================================
- .BP "String functions (continued)"
- .2
- The \fBeval\fP operator lets you execute dynamically generated code. For
- example, to process any command line arguments of the form \fBvariable=value\fP,
- place this at the top of your script:
- .QS
-
- eval '$'.$1."'$2';"
- while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
-
- .QE
- The \fBeval\fP operator is also useful for run-time testing of system-dependent features which would
- otherwise trigger fatal errors. For example, not all systems support the
- \fBsymlink\fP or \fBdbmopen\fP; you could test for their existence by executing
- the statements within an \fBeval\fP and testing the special variable \fB$@\fP,
- which contains the text of the run-time error message if anything went wrong.
- .\" ===========================================================================
- .BP "Suid Scripts"
- .2
- \fIPerl\fP programs can be made to run setuid,
- and can actually be more secure than the corresponding \fIC\fP program.
- .sp
- .2
- Because interpreters have no guarantee that the filename they get as the first
- argument is the same file that was \fBexec\fP'ed, \fIperl\fP
- won't let your run a setuid script on a system where setuid scripts
- are not disabled.
- .sp
- .2
- Using a dataflow tracing mechanism
- triggered by setuid execution, perl can tell what data is safe to use and
- what data comes from an external source and thus is \*(lqtainted.\*(rq
- .sp
- .2
- Tainted data may not be used directly or
- indirectly in any
- command that modifies files, directories or processes
- or else a fatal run-time error will result.
- .\" ===========================================================================
- .BP "Debugging and Packages"
- .2
- When invoked with the \fB-d\fP switch, \fIperl\fP runs your program
- under a symbolic debugger (written in \fIperl\fP) somewhat similar to \fIsdb\fP in syntax.
- Amongst other things,
- breakpoints may be set, variables examined or changed, and
- call tracebacks printed out. Because it uses \fBeval\fP on your code,
- you can execute any arbitrary perl code you want
- from the debugger.
- .sp
- .2
- Using \fBpackage\fPs you can write modules with separate namespaces to
- avoid naming conflicts in library routines. The debugger uses this
- to keep its variables separate from yours. Variable are accessed by
- the \fBpackage'name\fP notation, as in this line from the debugger:
- .sp
- .QS
- $DB'stop[$DB'line] =~ s/;9$//;
- .QE
- .\" ===========================================================================
- .BP "Command Line Options"
- The following are the more important command line switches recognized by \fIperl\fP:
- .sp
- .2
- \fB\-v\fP print out version string
- .2
- \fB\-w\fP issue warnings about error-prone constructs
- .2
- \fB\-d\fP run script under the debugger
- .2
- \fB\-e\fP like \fIsed\fP: used to enter single command lines
- .2
- \fB\-n\fP loop around input like \fIsed -n\fP
- .2
- \fB\-p\fP as with \fB-n\fP but print out each line
- .2
- \fB\-i\fP edit files in place
- .2
- \fB\-a\fP turn on autosplit mode (like \fIawk\fP) into \fB@F\fP array
- .2
- \fB\-P\fP call C pre-processor on script
- .\" ===========================================================================
- .BP "Examples: Command Line"
- .QS
- # output current version
- perl -v
-
- # simplest perl program
- perl -e 'print "hello, world.\en";'
-
- # useful at end of "find foo -print"
- perl -n -e 'chop;unlink;'
-
- # add first and last columns (filter)
- perl -a -n -e 'print $F[0] + $F[$#F], "\en";'
-
- # in-place edit of *.c files changing all foo to bar
- perl -p -i -e 's/\ebfoo\eb/bar/g;' *.c
-
- # run a script under the debugger
- perl -d myscript
- .QE
-