perl 3.0 patch #14 patch #13, continued
[p5sagit/p5-mst-13.2.git] / perl.man.1
CommitLineData
8d063cd8 1.rn '' }`
79a0689e 2''' $Header: perl.man.1,v 3.0.1.4 90/03/12 16:44:33 lwall Locked $
8d063cd8 3'''
4''' $Log: perl.man.1,v $
79a0689e 5''' Revision 3.0.1.4 90/03/12 16:44:33 lwall
6''' patch13: (LIST,) now legal
7''' patch13: improved LIST documentation
8''' patch13: example of if-elsif switch was wrong
9'''
ac58e20f 10''' Revision 3.0.1.3 90/02/28 17:54:32 lwall
11''' patch9: @array in scalar context now returns length of array
12''' patch9: in manual, example of open and ?: was backwards
13'''
ffed7fef 14''' Revision 3.0.1.2 89/11/17 15:30:03 lwall
15''' patch5: fixed some manual typos and indent problems
16'''
ae986130 17''' Revision 3.0.1.1 89/11/11 04:41:22 lwall
18''' patch2: explained about sh and ${1+"$@"}
19''' patch2: documented that space must separate word and '' string
20'''
a687059c 21''' Revision 3.0 89/10/18 15:21:29 lwall
22''' 3.0 baseline
8d063cd8 23'''
24'''
25.de Sh
26.br
27.ne 5
28.PP
29\fB\\$1\fR
30.PP
31..
32.de Sp
33.if t .sp .5v
34.if n .sp
35..
36.de Ip
37.br
38.ie \\n.$>=3 .ne \\$3
39.el .ne 3
40.IP "\\$1" \\$2
41..
42'''
43''' Set up \*(-- to give an unbreakable dash;
44''' string Tr holds user defined translation string.
45''' Bell System Logo is used as a dummy character.
46'''
378cc40b 47.tr \(*W-|\(bv\*(Tr
8d063cd8 48.ie n \{\
378cc40b 49.ds -- \(*W-
50.if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
51.if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
8d063cd8 52.ds L" ""
53.ds R" ""
54.ds L' '
55.ds R' '
56'br\}
57.el\{\
58.ds -- \(em\|
59.tr \*(Tr
60.ds L" ``
61.ds R" ''
62.ds L' `
63.ds R' '
64'br\}
a687059c 65.TH PERL 1 "\*(RP"
66.UC
8d063cd8 67.SH NAME
a687059c 68perl \- Practical Extraction and Report Language
8d063cd8 69.SH SYNOPSIS
a687059c 70.B perl
71[options] filename args
8d063cd8 72.SH DESCRIPTION
73.I Perl
a687059c 74is an interpreted language optimized for scanning arbitrary text files,
8d063cd8 75extracting information from those text files, and printing reports based
76on that information.
77It's also a good language for many system management tasks.
78The language is intended to be practical (easy to use, efficient, complete)
79rather than beautiful (tiny, elegant, minimal).
80It combines (in the author's opinion, anyway) some of the best features of C,
81\fIsed\fR, \fIawk\fR, and \fIsh\fR,
82so people familiar with those languages should have little difficulty with it.
83(Language historians will also note some vestiges of \fIcsh\fR, Pascal, and
84even BASIC-PLUS.)
85Expression syntax corresponds quite closely to C expression syntax.
a687059c 86Unlike most Unix utilities,
87.I perl
88does not arbitrarily limit the size of your data\*(--if you've got
89the memory,
90.I perl
91can slurp in your whole file as a single string.
92Recursion is of unlimited depth.
93And the hash tables used by associative arrays grow as necessary to prevent
94degraded performance.
95.I Perl
96uses sophisticated pattern matching techniques to scan large amounts of
97data very quickly.
98Although optimized for scanning text,
99.I perl
100can also deal with binary data, and can make dbm files look like associative
101arrays (where dbm is available).
102Setuid
103.I perl
104scripts are safer than C programs
105through a dataflow tracing mechanism which prevents many stupid security holes.
8d063cd8 106If you have a problem that would ordinarily use \fIsed\fR
107or \fIawk\fR or \fIsh\fR, but it
108exceeds their capabilities or must run a little faster,
109and you don't want to write the silly thing in C, then
110.I perl
111may be for you.
a687059c 112There are also translators to turn your
113.I sed
114and
115.I awk
116scripts into
117.I perl
118scripts.
8d063cd8 119OK, enough hype.
120.PP
121Upon startup,
122.I perl
123looks for your script in one of the following places:
124.Ip 1. 4 2
125Specified line by line via
126.B \-e
127switches on the command line.
128.Ip 2. 4 2
129Contained in the file specified by the first filename on the command line.
130(Note that systems supporting the #! notation invoke interpreters this way.)
131.Ip 3. 4 2
a687059c 132Passed in implicitly via standard input.
378cc40b 133This only works if there are no filename arguments\*(--to pass
a687059c 134arguments to a
135.I stdin
136script you must explicitly specify a \- for the script name.
8d063cd8 137.PP
138After locating your script,
139.I perl
140compiles it to an internal form.
141If the script is syntactically correct, it is executed.
142.Sh "Options"
83b4785a 143Note: on first reading this section may not make much sense to you. It's here
8d063cd8 144at the front for easy reference.
145.PP
146A single-character option may be combined with the following option, if any.
147This is particularly useful when invoking a script using the #! construct which
148only allows one argument. Example:
149.nf
150
151.ne 2
a687059c 152 #!/usr/bin/perl \-spi.bak # same as \-s \-p \-i.bak
8d063cd8 153 .\|.\|.
154
155.fi
156Options include:
157.TP 5
378cc40b 158.B \-a
a687059c 159turns on autosplit mode when used with a
160.B \-n
161or
162.BR \-p .
378cc40b 163An implicit split command to the @F array
164is done as the first thing inside the implicit while loop produced by
a687059c 165the
166.B \-n
167or
168.BR \-p .
378cc40b 169.nf
170
a687059c 171 perl \-ane \'print pop(@F), "\en";\'
378cc40b 172
173is equivalent to
174
175 while (<>) {
a687059c 176 @F = split(\' \');
177 print pop(@F), "\en";
378cc40b 178 }
179
180.fi
181.TP 5
a687059c 182.BI \-d
183runs the script under the perl debugger.
184See the section on Debugging.
185.TP 5
186.BI \-D number
8d063cd8 187sets debugging flags.
188To watch how it executes your script, use
a687059c 189.BR \-D14 .
8d063cd8 190(This only works if debugging is compiled into your
191.IR perl .)
a687059c 192Another nice value is \-D1024, which lists your compiled syntax tree.
193And \-D512 displays compiled regular expressions.
8d063cd8 194.TP 5
a687059c 195.BI \-e " commandline"
8d063cd8 196may be used to enter one line of script.
197Multiple
198.B \-e
199commands may be given to build up a multi-line script.
200If
201.B \-e
202is given,
203.I perl
204will not look for a script filename in the argument list.
205.TP 5
a687059c 206.BI \-i extension
8d063cd8 207specifies that files processed by the <> construct are to be edited
208in-place.
209It does this by renaming the input file, opening the output file by the
210same name, and selecting that output file as the default for print statements.
211The extension, if supplied, is added to the name of the
212old file to make a backup copy.
213If no extension is supplied, no backup is made.
a687059c 214Saying \*(L"perl \-p \-i.bak \-e "s/foo/bar/;" .\|.\|. \*(R" is the same as using
8d063cd8 215the script:
216.nf
217
218.ne 2
a687059c 219 #!/usr/bin/perl \-pi.bak
8d063cd8 220 s/foo/bar/;
221
222which is equivalent to
223
224.ne 14
378cc40b 225 #!/usr/bin/perl
8d063cd8 226 while (<>) {
227 if ($ARGV ne $oldargv) {
a687059c 228 rename($ARGV, $ARGV . \'.bak\');
229 open(ARGVOUT, ">$ARGV");
8d063cd8 230 select(ARGVOUT);
231 $oldargv = $ARGV;
232 }
233 s/foo/bar/;
234 }
235 continue {
236 print; # this prints to original filename
237 }
a687059c 238 select(STDOUT);
8d063cd8 239
240.fi
a687059c 241except that the
242.B \-i
243form doesn't need to compare $ARGV to $oldargv to know when
8d063cd8 244the filename has changed.
245It does, however, use ARGVOUT for the selected filehandle.
a687059c 246Note that
247.I STDOUT
248is restored as the default output filehandle after the loop.
378cc40b 249.Sp
250You can use eof to locate the end of each input file, in case you want
251to append to each file, or reset line numbering (see example under eof).
8d063cd8 252.TP 5
a687059c 253.BI \-I directory
8d063cd8 254may be used in conjunction with
255.B \-P
256to tell the C preprocessor where to look for include files.
257By default /usr/include and /usr/lib/perl are searched.
258.TP 5
259.B \-n
260causes
261.I perl
262to assume the following loop around your script, which makes it iterate
a687059c 263over filename arguments somewhat like \*(L"sed \-n\*(R" or \fIawk\fR:
8d063cd8 264.nf
265
266.ne 3
267 while (<>) {
378cc40b 268 .\|.\|. # your script goes here
8d063cd8 269 }
270
271.fi
272Note that the lines are not printed by default.
273See
274.B \-p
275to have lines printed.
378cc40b 276Here is an efficient way to delete all files older than a week:
277.nf
278
a687059c 279 find . \-mtime +7 \-print | perl \-ne \'chop;unlink;\'
378cc40b 280
281.fi
a687059c 282This is faster than using the \-exec switch of find because you don't have to
378cc40b 283start a process on every filename found.
8d063cd8 284.TP 5
285.B \-p
286causes
287.I perl
288to assume the following loop around your script, which makes it iterate
289over filename arguments somewhat like \fIsed\fR:
290.nf
291
292.ne 5
293 while (<>) {
378cc40b 294 .\|.\|. # your script goes here
8d063cd8 295 } continue {
296 print;
297 }
298
299.fi
300Note that the lines are printed automatically.
301To suppress printing use the
302.B \-n
303switch.
83b4785a 304A
305.B \-p
306overrides a
307.B \-n
308switch.
8d063cd8 309.TP 5
310.B \-P
311causes your script to be run through the C preprocessor before
312compilation by
a687059c 313.IR perl .
8d063cd8 314(Since both comments and cpp directives begin with the # character,
315you should avoid starting comments with any words recognized
316by the C preprocessor such as \*(L"if\*(R", \*(L"else\*(R" or \*(L"define\*(R".)
317.TP 5
318.B \-s
319enables some rudimentary switch parsing for switches on the command line
a687059c 320after the script name but before any filename arguments (or before a \-\|\-).
83b4785a 321Any switch found there is removed from @ARGV and sets the corresponding variable in the
8d063cd8 322.I perl
323script.
324The following script prints \*(L"true\*(R" if and only if the script is
a687059c 325invoked with a \-xyz switch.
8d063cd8 326.nf
327
328.ne 2
a687059c 329 #!/usr/bin/perl \-s
83b4785a 330 if ($xyz) { print "true\en"; }
8d063cd8 331
332.fi
378cc40b 333.TP 5
334.B \-S
a687059c 335makes
336.I perl
337use the PATH environment variable to search for the script
378cc40b 338(unless the name of the script starts with a slash).
339Typically this is used to emulate #! startup on machines that don't
340support #!, in the following manner:
341.nf
342
343 #!/usr/bin/perl
a687059c 344 eval "exec /usr/bin/perl \-S $0 $*"
378cc40b 345 if $running_under_some_shell;
346
347.fi
348The system ignores the first line and feeds the script to /bin/sh,
a687059c 349which proceeds to try to execute the
350.I perl
351script as a shell script.
378cc40b 352The shell executes the second line as a normal shell command, and thus
a687059c 353starts up the
354.I perl
355interpreter.
378cc40b 356On some systems $0 doesn't always contain the full pathname,
a687059c 357so the
358.B \-S
359tells
360.I perl
361to search for the script if necessary.
362After
363.I perl
364locates the script, it parses the lines and ignores them because
378cc40b 365the variable $running_under_some_shell is never true.
ae986130 366A better construct than $* would be ${1+"$@"}, which handles embedded spaces
367and such in the filenames, but doesn't work if the script is being interpreted
368by csh.
369In order to start up sh rather than csh, some systems may have to replace the
370#! line with a line containing just
371a colon, which will be politely ignored by perl.
378cc40b 372.TP 5
a687059c 373.B \-u
374causes
375.I perl
376to dump core after compiling your script.
377You can then take this core dump and turn it into an executable file
378by using the undump program (not supplied).
379This speeds startup at the expense of some disk space (which you can
380minimize by stripping the executable).
381(Still, a "hello world" executable comes out to about 200K on my machine.)
382If you are going to run your executable as a set-id program then you
383should probably compile it using taintperl rather than normal perl.
384If you want to execute a portion of your script before dumping, use the
385dump operator instead.
386.TP 5
378cc40b 387.B \-U
a687059c 388allows
389.I perl
390to do unsafe operations.
13281fa4 391Currently the only \*(L"unsafe\*(R" operation is the unlinking of directories while
378cc40b 392running as superuser.
393.TP 5
394.B \-v
a687059c 395prints the version and patchlevel of your
396.I perl
397executable.
378cc40b 398.TP 5
399.B \-w
400prints warnings about identifiers that are mentioned only once, and scalar
401variables that are used before being set.
402Also warns about redefined subroutines, and references to undefined
a687059c 403filehandles or filehandles opened readonly that you are attempting to
404write on.
405Also warns you if you use == on values that don't look like numbers, and if
406your subroutines recurse more than 100 deep.
8d063cd8 407.Sh "Data Types and Objects"
408.PP
a687059c 409.I Perl
410has three data types: scalars, arrays of scalars, and
411associative arrays of scalars.
412Normal arrays are indexed by number, and associative arrays by string.
8d063cd8 413.PP
a687059c 414The interpretation of operations and values in perl sometimes
415depends on the requirements
416of the context around the operation or value.
417There are three major contexts: string, numeric and array.
418Certain operations return array values
419in contexts wanting an array, and scalar values otherwise.
420(If this is true of an operation it will be mentioned in the documentation
421for that operation.)
422Operations which return scalars don't care whether the context is looking
423for a string or a number, but
424scalar variables and values are interpreted as strings or numbers
425as appropriate to the context.
378cc40b 426A scalar is interpreted as TRUE in the boolean sense if it is not the null
8d063cd8 427string or 0.
ffed7fef 428Booleans returned by operators are 1 for true and 0 or \'\' (the null
8d063cd8 429string) for false.
430.PP
a687059c 431There are actually two varieties of null string: defined and undefined.
432Undefined null strings are returned when there is no real value for something,
433such as when there was an error, or at end of file, or when you refer
434to an uninitialized variable or element of an array.
435An undefined null string may become defined the first time you access it, but
436prior to that you can use the defined() operator to determine whether the
437value is defined or not.
438.PP
378cc40b 439References to scalar variables always begin with \*(L'$\*(R', even when referring
440to a scalar that is part of an array.
8d063cd8 441Thus:
442.nf
443
444.ne 3
378cc40b 445 $days \h'|2i'# a simple scalar variable
8d063cd8 446 $days[28] \h'|2i'# 29th element of array @days
a687059c 447 $days{\'Feb\'}\h'|2i'# one value from an associative array
378cc40b 448 $#days \h'|2i'# last index of array @days
8d063cd8 449
a687059c 450but entire arrays or array slices are denoted by \*(L'@\*(R':
8d063cd8 451
452 @days \h'|2i'# ($days[0], $days[1],\|.\|.\|. $days[n])
a687059c 453 @days[3,4,5]\h'|2i'# same as @days[3.\|.5]
454 @days{'a','c'}\h'|2i'# same as ($days{'a'},$days{'c'})
455
456and entire associative arrays are denoted by \*(L'%\*(R':
8d063cd8 457
a687059c 458 %days \h'|2i'# (key1, val1, key2, val2 .\|.\|.)
8d063cd8 459.fi
460.PP
a687059c 461Any of these eight constructs may serve as an lvalue,
378cc40b 462that is, may be assigned to.
a687059c 463(It also turns out that an assignment is itself an lvalue in
464certain contexts\*(--see examples under s, tr and chop.)
465Assignment to a scalar evaluates the righthand side in a scalar context,
466while assignment to an array or array slice evaluates the righthand side
467in an array context.
468.PP
378cc40b 469You may find the length of array @days by evaluating
8d063cd8 470\*(L"$#days\*(R", as in
471.IR csh .
378cc40b 472(Actually, it's not the length of the array, it's the subscript of the last element, since there is (ordinarily) a 0th element.)
473Assigning to $#days changes the length of the array.
474Shortening an array by this method does not actually destroy any values.
475Lengthening an array that was previously shortened recovers the values that
476were in those elements.
477You can also gain some measure of efficiency by preextending an array that
478is going to get big.
479(You can also extend an array by assigning to an element that is off the
480end of the array.
481This differs from assigning to $#whatever in that intervening values
482are set to null rather than recovered.)
483You can truncate an array down to nothing by assigning the null list () to
484it.
485The following are exactly equivalent
486.nf
487
488 @whatever = ();
489 $#whatever = $[ \- 1;
490
491.fi
8d063cd8 492.PP
ac58e20f 493If you evaluate an array in a scalar context, it returns the length of
494the array.
495The following is always true:
496.nf
497
498 @whatever == $#whatever \- $[ + 1;
499
500.fi
501.PP
a687059c 502Multi-dimensional arrays are not directly supported, but see the discussion
503of the $; variable later for a means of emulating multiple subscripts with
504an associative array.
ac58e20f 505You could also write a subroutine to turn multiple subscripts into a single
506subscript.
a687059c 507.PP
8d063cd8 508Every data type has its own namespace.
378cc40b 509You can, without fear of conflict, use the same name for a scalar variable,
8d063cd8 510an array, an associative array, a filehandle, a subroutine name, and/or
511a label.
a687059c 512Since variable and array references always start with \*(L'$\*(R', \*(L'@\*(R',
513or \*(L'%\*(R', the \*(L"reserved\*(R" words aren't in fact reserved
8d063cd8 514with respect to variable names.
515(They ARE reserved with respect to labels and filehandles, however, which
378cc40b 516don't have an initial special character.
a687059c 517Hint: you could say open(LOG,\'logfile\') rather than open(log,\'logfile\').
518Using uppercase filehandles also improves readability and protects you
519from conflict with future reserved words.)
8d063cd8 520Case IS significant\*(--\*(L"FOO\*(R", \*(L"Foo\*(R" and \*(L"foo\*(R" are all
521different names.
522Names which start with a letter may also contain digits and underscores.
523Names which do not start with a letter are limited to one character,
524e.g. \*(L"$%\*(R" or \*(L"$$\*(R".
a687059c 525(Most of the one character names have a predefined significance to
526.IR perl .
8d063cd8 527More later.)
528.PP
a687059c 529Numeric literals are specified in any of the usual floating point or
530integer formats:
531.nf
532
533.ne 5
534 12345
535 12345.67
536 .23E-10
537 0xffff # hex
538 0377 # octal
539
540.fi
8d063cd8 541String literals are delimited by either single or double quotes.
542They work much like shell quotes:
543double-quoted string literals are subject to backslash and variable
a687059c 544substitution; single-quoted strings are not (except for \e\' and \e\e).
8d063cd8 545The usual backslash rules apply for making characters such as newline, tab, etc.
546You can also embed newlines directly in your strings, i.e. they can end on
547a different line than they begin.
548This is nice, but if you forget your trailing quote, the error will not be
a687059c 549reported until
550.I perl
551finds another line containing the quote character, which
8d063cd8 552may be much further on in the script.
a687059c 553Variable substitution inside strings is limited to scalar variables, normal
554array values, and array slices.
555(In other words, identifiers beginning with $ or @, followed by an optional
556bracketed expression as a subscript.)
8d063cd8 557The following code segment prints out \*(L"The price is $100.\*(R"
558.nf
559
560.ne 2
a687059c 561 $Price = \'$100\';\h'|3.5i'# not interpreted
8d063cd8 562 print "The price is $Price.\e\|n";\h'|3.5i'# interpreted
563
564.fi
83b4785a 565Note that you can put curly brackets around the identifier to delimit it
566from following alphanumerics.
ae986130 567Also note that a single quoted string must be separated from a preceding
568word by a space, since single quote is a valid character in an identifier
569(see Packages).
8d063cd8 570.PP
a687059c 571Array values are interpolated into double-quoted strings by joining all the
572elements of the array with the delimiter specified in the $" variable,
573space by default.
574(Since in versions of perl prior to 3.0 the @ character was not a metacharacter
575in double-quoted strings, the interpolation of @array, $array[EXPR],
576@array[LIST], $array{EXPR}, or @array{LIST} only happens if array is
577referenced elsewhere in the program or is predefined.)
578The following are equivalent:
579.nf
580
581.ne 4
582 $temp = join($",@ARGV);
583 system "echo $temp";
584
585 system "echo @ARGV";
586
587.fi
ae986130 588Within search patterns (which also undergo double-quotish substitution)
a687059c 589there is a bad ambiguity: Is /$foo[bar]/ to be
590interpreted as /${foo}[bar]/ (where [bar] is a character class for the
591regular expression) or as /${foo[bar]}/ (where [bar] is the subscript to
592array @foo)?
593If @foo doesn't otherwise exist, then it's obviously a character class.
594If @foo exists, perl takes a good guess about [bar], and is almost always right.
595If it does guess wrong, or if you're just plain paranoid,
596you can force the correct interpretation with curly brackets as above.
597.PP
598A line-oriented form of quoting is based on the shell here-is syntax.
599Following a << you specify a string to terminate the quoted material, and all lines
600following the current line down to the terminating string are the value
601of the item.
602The terminating string may be either an identifier (a word), or some
603quoted text.
604If quoted, the type of quotes you use determines the treatment of the text,
605just as in regular quoting.
606An unquoted identifier works like double quotes.
607There must be no space between the << and the identifier.
608(If you put a space it will be treated as a null identifier, which is
609valid, and matches the first blank line\*(--see Merry Christmas example below.)
610The terminating string must appear by itself (unquoted and with no surrounding
611whitespace) on the terminating line.
612.nf
613
614 print <<EOF; # same as above
615The price is $Price.
616EOF
617
618 print <<"EOF"; # same as above
619The price is $Price.
620EOF
621
622 print << x 10; # null identifier is delimiter
623Merry Christmas!
624
625 print <<`EOC`; # execute commands
626echo hi there
627echo lo there
628EOC
629
630 print <<foo, <<bar; # you can stack them
631I said foo.
632foo
633I said bar.
634bar
635
636.fi
8d063cd8 637Array literals are denoted by separating individual values by commas, and
79a0689e 638enclosing the list in parentheses:
639.nf
640
641 (LIST)
642
643.fi
8d063cd8 644In a context not requiring an array value, the value of the array literal
645is the value of the final element, as in the C comma operator.
646For example,
647.nf
648
83b4785a 649.ne 4
a687059c 650 @foo = (\'cc\', \'\-E\', $bar);
8d063cd8 651
652assigns the entire array value to array foo, but
653
a687059c 654 $foo = (\'cc\', \'\-E\', $bar);
8d063cd8 655
656.fi
657assigns the value of variable bar to variable foo.
79a0689e 658Note that the value of an actual array in a scalar context is the length
659of the array; the following assigns to $foo the value 3:
660.nf
661
662.ne 2
663 @foo = (\'cc\', \'\-E\', $bar);
664 $foo = @foo; # $foo gets 3
665
666.fi
667You may have an optional comma before the closing parenthesis of an
668array literal, so that you can say:
669.nf
670
671 @foo = (
672 1,
673 2,
674 3,
675 );
676
677.fi
678When a LIST is evaluated, each element of the list is evaluated in
679an array context, and the resulting array value is interpolated into LIST
680just as if each individual element were a member of LIST. Thus arrays
681lose their identity in a LIST\*(--the list
682
683 (@foo,@bar,&SomeSub)
684
685contains all the elements of @foo followed by all the elements of @bar,
686followed by all the elements returned by the subroutine named SomeSub.
687.PP
688A list value may also be subscripted like a normal array.
689Examples:
690.nf
691
692 $time = (stat($file))[8]; # stat returns array value
693 $digit = ('a','b','c','d','e','f')[$digit-10];
694 return (pop(@foo),pop(@foo))[0];
695
696.fi
697.PP
8d063cd8 698Array lists may be assigned to if and only if each element of the list
699is an lvalue:
700.nf
701
702 ($a, $b, $c) = (1, 2, 3);
703
a687059c 704 ($map{\'red\'}, $map{\'blue\'}, $map{\'green\'}) = (0x00f, 0x0f0, 0xf00);
705
706The final element may be an array or an associative array:
707
708 ($a, $b, @rest) = split;
709 local($a, $b, %rest) = @_;
8d063cd8 710
711.fi
a687059c 712You can actually put an array anywhere in the list, but the first array
713in the list will soak up all the values, and anything after it will get
714a null value.
715This may be useful in a local().
8d063cd8 716.PP
a687059c 717An associative array literal contains pairs of values to be interpreted
718as a key and a value:
719.nf
720
721.ne 2
722 # same as map assignment above
723 %map = ('red',0x00f,'blue',0x0f0,'green',0xf00);
724
725.fi
726Array assignment in a scalar context returns the number of elements
727produced by the expression on the right side of the assignment:
728.nf
729
730 $x = (($foo,$bar) = (3,2,1)); # set $x to 3, not 2
731
732.fi
8d063cd8 733.PP
734There are several other pseudo-literals that you should know about.
378cc40b 735If a string is enclosed by backticks (grave accents), it first undergoes
736variable substitution just like a double quoted string.
737It is then interpreted as a command, and the output of that command
738is the value of the pseudo-literal, like in a shell.
8d063cd8 739The command is executed each time the pseudo-literal is evaluated.
378cc40b 740The status value of the command is returned in $? (see Predefined Names
741for the interpretation of $?).
742Unlike in \f2csh\f1, no translation is done on the return
8d063cd8 743data\*(--newlines remain newlines.
378cc40b 744Unlike in any of the shells, single quotes do not hide variable names
745in the command from interpretation.
746To pass a $ through to the shell you need to hide it with a backslash.
8d063cd8 747.PP
748Evaluating a filehandle in angle brackets yields the next line
a687059c 749from that file (newline included, so it's never false until EOF, at
750which time an undefined value is returned).
8d063cd8 751Ordinarily you must assign that value to a variable,
ac58e20f 752but there is one situation where an automatic assignment happens.
8d063cd8 753If (and only if) the input symbol is the only thing inside the conditional of a
754.I while
755loop, the value is
756automatically assigned to the variable \*(L"$_\*(R".
757(This may seem like an odd thing to you, but you'll use the construct
758in almost every
759.I perl
760script you write.)
761Anyway, the following lines are equivalent to each other:
762.nf
763
a687059c 764.ne 5
765 while ($_ = <STDIN>) { print; }
766 while (<STDIN>) { print; }
767 for (\|;\|<STDIN>;\|) { print; }
768 print while $_ = <STDIN>;
769 print while <STDIN>;
8d063cd8 770
771.fi
772The filehandles
a687059c 773.IR STDIN ,
774.I STDOUT
775and
776.I STDERR
777are predefined.
778(The filehandles
8d063cd8 779.IR stdin ,
780.I stdout
781and
782.I stderr
a687059c 783will also work except in packages, where they would be interpreted as
784local identifiers rather than global.)
8d063cd8 785Additional filehandles may be created with the
786.I open
787function.
788.PP
378cc40b 789If a <FILEHANDLE> is used in a context that is looking for an array, an array
790consisting of all the input lines is returned, one line per array element.
791It's easy to make a LARGE data space this way, so use with care.
792.PP
8d063cd8 793The null filehandle <> is special and can be used to emulate the behavior of
794\fIsed\fR and \fIawk\fR.
795Input from <> comes either from standard input, or from each file listed on
796the command line.
797Here's how it works: the first time <> is evaluated, the ARGV array is checked,
a687059c 798and if it is null, $ARGV[0] is set to \'-\', which when opened gives you standard
8d063cd8 799input.
800The ARGV array is then processed as a list of filenames.
801The loop
802.nf
803
804.ne 3
805 while (<>) {
806 .\|.\|. # code for each line
807 }
808
809.ne 10
810is equivalent to
811
a687059c 812 unshift(@ARGV, \'\-\') \|if \|$#ARGV < $[;
8d063cd8 813 while ($ARGV = shift) {
814 open(ARGV, $ARGV);
815 while (<ARGV>) {
816 .\|.\|. # code for each line
817 }
818 }
819
820.fi
821except that it isn't as cumbersome to say.
822It really does shift array ARGV and put the current filename into
823variable ARGV.
824It also uses filehandle ARGV internally.
825You can modify @ARGV before the first <> as long as you leave the first
826filename at the beginning of the array.
83b4785a 827Line numbers ($.) continue as if the input was one big happy file.
378cc40b 828(But see example under eof for how to reset line numbers on each file.)
8d063cd8 829.PP
83b4785a 830.ne 5
378cc40b 831If you want to set @ARGV to your own list of files, go right ahead.
8d063cd8 832If you want to pass switches into your script, you can
833put a loop on the front like this:
834.nf
835
836.ne 10
837 while ($_ = $ARGV[0], /\|^\-/\|) {
838 shift;
839 last if /\|^\-\|\-$\|/\|;
840 /\|^\-D\|(.*\|)/ \|&& \|($debug = $1);
841 /\|^\-v\|/ \|&& \|$verbose++;
842 .\|.\|. # other switches
843 }
844 while (<>) {
845 .\|.\|. # code for each line
846 }
847
848.fi
849The <> symbol will return FALSE only once.
850If you call it again after this it will assume you are processing another
a687059c 851@ARGV list, and if you haven't set @ARGV, will input from
852.IR STDIN .
378cc40b 853.PP
854If the string inside the angle brackets is a reference to a scalar variable
855(e.g. <$foo>),
856then that variable contains the name of the filehandle to input from.
857.PP
858If the string inside angle brackets is not a filehandle, it is interpreted
859as a filename pattern to be globbed, and either an array of filenames or the
860next filename in the list is returned, depending on context.
861One level of $ interpretation is done first, but you can't say <$foo>
862because that's an indirect filehandle as explained in the previous
863paragraph.
864You could insert curly brackets to force interpretation as a
865filename glob: <${foo}>.
866Example:
867.nf
868
869.ne 3
870 while (<*.c>) {
a687059c 871 chmod 0644, $_;
378cc40b 872 }
873
874is equivalent to
875
876.ne 5
a687059c 877 open(foo, "echo *.c | tr \-s \' \et\er\ef\' \'\e\e012\e\e012\e\e012\e\e012\'|");
378cc40b 878 while (<foo>) {
879 chop;
a687059c 880 chmod 0644, $_;
378cc40b 881 }
882
883.fi
884In fact, it's currently implemented that way.
a687059c 885(Which means it will not work on filenames with spaces in them unless
886you have /bin/csh on your machine.)
378cc40b 887Of course, the shortest way to do the above is:
888.nf
889
a687059c 890 chmod 0644, <*.c>;
378cc40b 891
892.fi
8d063cd8 893.Sh "Syntax"
894.PP
895A
896.I perl
897script consists of a sequence of declarations and commands.
898The only things that need to be declared in
899.I perl
900are report formats and subroutines.
901See the sections below for more information on those declarations.
ffed7fef 902All uninitialized user-created objects are assumed to
a687059c 903start with a null or 0 value until they
904are defined by some explicit operation such as assignment.
8d063cd8 905The sequence of commands is executed just once, unlike in
906.I sed
907and
908.I awk
909scripts, where the sequence of commands is executed for each input line.
910While this means that you must explicitly loop over the lines of your input file
911(or files), it also means you have much more control over which files and which
912lines you look at.
913(Actually, I'm lying\*(--it is possible to do an implicit loop with either the
914.B \-n
915or
916.B \-p
917switch.)
918.PP
919A declaration can be put anywhere a command can, but has no effect on the
a687059c 920execution of the primary sequence of commands--declarations all take effect
921at compile time.
8d063cd8 922Typically all the declarations are put at the beginning or the end of the script.
923.PP
924.I Perl
925is, for the most part, a free-form language.
926(The only exception to this is format declarations, for fairly obvious reasons.)
927Comments are indicated by the # character, and extend to the end of the line.
928If you attempt to use /* */ C comments, it will be interpreted either as
929division or pattern matching, depending on the context.
930So don't do that.
931.Sh "Compound statements"
932In
933.IR perl ,
934a sequence of commands may be treated as one command by enclosing it
935in curly brackets.
936We will call this a BLOCK.
937.PP
938The following compound commands may be used to control flow:
939.nf
940
941.ne 4
942 if (EXPR) BLOCK
943 if (EXPR) BLOCK else BLOCK
378cc40b 944 if (EXPR) BLOCK elsif (EXPR) BLOCK .\|.\|. else BLOCK
8d063cd8 945 LABEL while (EXPR) BLOCK
946 LABEL while (EXPR) BLOCK continue BLOCK
947 LABEL for (EXPR; EXPR; EXPR) BLOCK
378cc40b 948 LABEL foreach VAR (ARRAY) BLOCK
8d063cd8 949 LABEL BLOCK continue BLOCK
950
951.fi
83b4785a 952Note that, unlike C and Pascal, these are defined in terms of BLOCKs, not
8d063cd8 953statements.
954This means that the curly brackets are \fIrequired\fR\*(--no dangling statements allowed.
955If you want to write conditionals without curly brackets there are several
956other ways to do it.
957The following all do the same thing:
958.nf
959
960.ne 5
a687059c 961 if (!open(foo)) { die "Can't open $foo: $!"; }
962 die "Can't open $foo: $!" unless open(foo);
963 open(foo) || die "Can't open $foo: $!"; # foo or bust!
ac58e20f 964 open(foo) ? \'hi mom\' : die "Can't open $foo: $!";
a687059c 965 # a bit exotic, that last one
8d063cd8 966
967.fi
8d063cd8 968.PP
969The
970.I if
971statement is straightforward.
972Since BLOCKs are always bounded by curly brackets, there is never any
973ambiguity about which
974.I if
975an
976.I else
977goes with.
978If you use
979.I unless
980in place of
981.IR if ,
982the sense of the test is reversed.
983.PP
984The
985.I while
986statement executes the block as long as the expression is true
987(does not evaluate to the null string or 0).
988The LABEL is optional, and if present, consists of an identifier followed by
989a colon.
990The LABEL identifies the loop for the loop control statements
991.IR next ,
a687059c 992.IR last ,
8d063cd8 993and
994.I redo
995(see below).
996If there is a
997.I continue
998BLOCK, it is always executed just before
999the conditional is about to be evaluated again, similarly to the third part
1000of a
1001.I for
1002loop in C.
1003Thus it can be used to increment a loop variable, even when the loop has
1004been continued via the
1005.I next
1006statement (similar to the C \*(L"continue\*(R" statement).
1007.PP
1008If the word
1009.I while
1010is replaced by the word
1011.IR until ,
1012the sense of the test is reversed, but the conditional is still tested before
1013the first iteration.
1014.PP
1015In either the
1016.I if
1017or the
1018.I while
1019statement, you may replace \*(L"(EXPR)\*(R" with a BLOCK, and the conditional
1020is true if the value of the last command in that block is true.
1021.PP
1022The
1023.I for
1024loop works exactly like the corresponding
1025.I while
1026loop:
1027.nf
1028
1029.ne 12
1030 for ($i = 1; $i < 10; $i++) {
1031 .\|.\|.
1032 }
1033
1034is the same as
1035
1036 $i = 1;
1037 while ($i < 10) {
1038 .\|.\|.
1039 } continue {
1040 $i++;
1041 }
1042.fi
1043.PP
378cc40b 1044The foreach loop iterates over a normal array value and sets the variable
1045VAR to be each element of the array in turn.
13281fa4 1046The \*(L"foreach\*(R" keyword is actually identical to the \*(L"for\*(R" keyword,
1047so you can use \*(L"foreach\*(R" for readability or \*(L"for\*(R" for brevity.
378cc40b 1048If VAR is omitted, $_ is set to each value.
1049If ARRAY is an actual array (as opposed to an expression returning an array
1050value), you can modify each element of the array
1051by modifying VAR inside the loop.
1052Examples:
1053.nf
1054
1055.ne 5
1056 for (@ary) { s/foo/bar/; }
1057
1058 foreach $elem (@elements) {
1059 $elem *= 2;
1060 }
1061
a687059c 1062.ne 3
1063 for ((10,9,8,7,6,5,4,3,2,1,\'BOOM\')) {
1064 print $_, "\en"; sleep(1);
378cc40b 1065 }
1066
a687059c 1067 for (1..15) { print "Merry Christmas\en"; }
1068
378cc40b 1069.ne 3
a687059c 1070 foreach $item (split(/:[\e\e\en:]*/, $ENV{\'TERMCAP\'}) {
378cc40b 1071 print "Item: $item\en";
1072 }
a687059c 1073
378cc40b 1074.fi
1075.PP
8d063cd8 1076The BLOCK by itself (labeled or not) is equivalent to a loop that executes
1077once.
1078Thus you can use any of the loop control statements in it to leave or
1079restart the block.
1080The
1081.I continue
1082block is optional.
1083This construct is particularly nice for doing case structures.
1084.nf
1085
1086.ne 6
1087 foo: {
a687059c 1088 if (/^abc/) { $abc = 1; last foo; }
1089 if (/^def/) { $def = 1; last foo; }
1090 if (/^xyz/) { $xyz = 1; last foo; }
8d063cd8 1091 $nothing = 1;
1092 }
1093
1094.fi
a687059c 1095There is no official switch statement in perl, because there
1096are already several ways to write the equivalent.
1097In addition to the above, you could write
378cc40b 1098.nf
1099
a687059c 1100.ne 6
1101 foo: {
ffed7fef 1102 $abc = 1, last foo if /^abc/;
1103 $def = 1, last foo if /^def/;
1104 $xyz = 1, last foo if /^xyz/;
a687059c 1105 $nothing = 1;
1106 }
1107
1108or
1109
1110.ne 6
1111 foo: {
1112 /^abc/ && do { $abc = 1; last foo; }
1113 /^def/ && do { $def = 1; last foo; }
1114 /^xyz/ && do { $xyz = 1; last foo; }
1115 $nothing = 1;
1116 }
1117
1118or
1119
1120.ne 6
1121 foo: {
1122 /^abc/ && ($abc = 1, last foo);
1123 /^def/ && ($def = 1, last foo);
1124 /^xyz/ && ($xyz = 1, last foo);
1125 $nothing = 1;
1126 }
1127
1128or even
1129
378cc40b 1130.ne 8
a687059c 1131 if (/^abc/)
79a0689e 1132 { $abc = 1; }
a687059c 1133 elsif (/^def/)
79a0689e 1134 { $def = 1; }
a687059c 1135 elsif (/^xyz/)
79a0689e 1136 { $xyz = 1; }
a687059c 1137 else
1138 {$nothing = 1;}
378cc40b 1139
1140.fi
a687059c 1141As it happens, these are all optimized internally to a switch structure,
1142so perl jumps directly to the desired statement, and you needn't worry
1143about perl executing a lot of unnecessary statements when you have a string
1144of 50 elsifs, as long as you are testing the same simple scalar variable
1145using ==, eq, or pattern matching as above.
1146(If you're curious as to whether the optimizer has done this for a particular
1147case statement, you can use the \-D1024 switch to list the syntax tree
1148before execution.)
8d063cd8 1149.Sh "Simple statements"
1150The only kind of simple statement is an expression evaluated for its side
1151effects.
1152Every expression (simple statement) must be terminated with a semicolon.
1153Note that this is like C, but unlike Pascal (and
1154.IR awk ).
1155.PP
1156Any simple statement may optionally be followed by a
1157single modifier, just before the terminating semicolon.
1158The possible modifiers are:
1159.nf
1160
1161.ne 4
1162 if EXPR
1163 unless EXPR
1164 while EXPR
1165 until EXPR
1166
1167.fi
1168The
1169.I if
1170and
1171.I unless
1172modifiers have the expected semantics.
1173The
1174.I while
1175and
378cc40b 1176.I until
8d063cd8 1177modifiers also have the expected semantics (conditional evaluated first),
1178except when applied to a do-BLOCK command,
1179in which case the block executes once before the conditional is evaluated.
1180This is so that you can write loops like:
1181.nf
1182
1183.ne 4
1184 do {
a687059c 1185 $_ = <STDIN>;
8d063cd8 1186 .\|.\|.
1187 } until $_ \|eq \|".\|\e\|n";
1188
1189.fi
1190(See the
1191.I do
1192operator below. Note also that the loop control commands described later will
83b4785a 1193NOT work in this construct, since modifiers don't take loop labels.
8d063cd8 1194Sorry.)
1195.Sh "Expressions"
1196Since
1197.I perl
1198expressions work almost exactly like C expressions, only the differences
1199will be mentioned here.
1200.PP
1201Here's what
1202.I perl
1203has that C doesn't:
a687059c 1204.Ip ** 8 2
1205The exponentiation operator.
1206.Ip **= 8
1207The exponentiation assignment operator.
8d063cd8 1208.Ip (\|) 8 3
1209The null list, used to initialize an array to null.
1210.Ip . 8
1211Concatenation of two strings.
1212.Ip .= 8
a687059c 1213The concatenation assignment operator.
8d063cd8 1214.Ip eq 8
1215String equality (== is numeric equality).
1216For a mnemonic just think of \*(L"eq\*(R" as a string.
1217(If you are used to the
1218.I awk
1219behavior of using == for either string or numeric equality
1220based on the current form of the comparands, beware!
1221You must be explicit here.)
1222.Ip ne 8
1223String inequality (!= is numeric inequality).
1224.Ip lt 8
1225String less than.
1226.Ip gt 8
1227String greater than.
1228.Ip le 8
1229String less than or equal.
1230.Ip ge 8
1231String greater than or equal.
1232.Ip =~ 8 2
1233Certain operations search or modify the string \*(L"$_\*(R" by default.
1234This operator makes that kind of operation work on some other string.
1235The right argument is a search pattern, substitution, or translation.
1236The left argument is what is supposed to be searched, substituted, or
1237translated instead of the default \*(L"$_\*(R".
1238The return value indicates the success of the operation.
1239(If the right argument is an expression other than a search pattern,
1240substitution, or translation, it is interpreted as a search pattern
1241at run time.
1242This is less efficient than an explicit search, since the pattern must
1243be compiled every time the expression is evaluated.)
1244The precedence of this operator is lower than unary minus and autoincrement/decrement, but higher than everything else.
1245.Ip !~ 8
1246Just like =~ except the return value is negated.
1247.Ip x 8
1248The repetition operator.
1249Returns a string consisting of the left operand repeated the
1250number of times specified by the right operand.
1251.nf
1252
a687059c 1253 print \'\-\' x 80; # print row of dashes
1254 print \'\-\' x80; # illegal, x80 is identifier
8d063cd8 1255
a687059c 1256 print "\et" x ($tab/8), \' \' x ($tab%8); # tab over
8d063cd8 1257
1258.fi
1259.Ip x= 8
a687059c 1260The repetition assignment operator.
1261.Ip .\|. 8
1262The range operator, which is really two different operators depending
1263on the context.
1264In an array context, returns an array of values counting (by ones)
1265from the left value to the right value.
1266This is useful for writing \*(L"for (1..10)\*(R" loops and for doing
1267slice operations on arrays.
1268.Sp
1269In a scalar context, .\|. returns a boolean value.
1270The operator is bistable, like a flip-flop..
1271Each .\|. operator maintains its own boolean state.
378cc40b 1272It is false as long as its left operand is false.
1273Once the left operand is true, the range operator stays true
1274until the right operand is true,
1275AFTER which the range operator becomes false again.
a687059c 1276(It doesn't become false till the next time the range operator is evaluated.
8d063cd8 1277It can become false on the same evaluation it became true, but it still returns
1278true once.)
13281fa4 1279The right operand is not evaluated while the operator is in the \*(L"false\*(R" state,
1280and the left operand is not evaluated while the operator is in the \*(L"true\*(R" state.
a687059c 1281The scalar .\|. operator is primarily intended for doing line number ranges
1282after
8d063cd8 1283the fashion of \fIsed\fR or \fIawk\fR.
1284The precedence is a little lower than || and &&.
1285The value returned is either the null string for false, or a sequence number
1286(beginning with 1) for true.
1287The sequence number is reset for each range encountered.
a687059c 1288The final sequence number in a range has the string \'E0\' appended to it, which
8d063cd8 1289doesn't affect its numeric value, but gives you something to search for if you
1290want to exclude the endpoint.
1291You can exclude the beginning point by waiting for the sequence number to be
1292greater than 1.
a687059c 1293If either operand of scalar .\|. is static, that operand is implicitly compared
1294to the $. variable, the current line number.
8d063cd8 1295Examples:
1296.nf
1297
a687059c 1298.ne 6
1299As a scalar operator:
1300 if (101 .\|. 200) { print; } # print 2nd hundred lines
8d063cd8 1301
a687059c 1302 next line if (1 .\|. /^$/); # skip header lines
8d063cd8 1303
a687059c 1304 s/^/> / if (/^$/ .\|. eof()); # quote body
1305
1306.ne 4
1307As an array operator:
1308 for (101 .\|. 200) { print; } # print $_ 100 times
1309
1310 @foo = @foo[$[ .\|. $#foo]; # an expensive no-op
1311 @foo = @foo[$#foo-4 .\|. $#foo]; # slice last 5 items
8d063cd8 1312
1313.fi
378cc40b 1314.Ip \-x 8
1315A file test.
1316This unary operator takes one argument, either a filename or a filehandle,
1317and tests the associated file to see if something is true about it.
a687059c 1318If the argument is omitted, tests $_, except for \-t, which tests
1319.IR STDIN .
1320It returns 1 for true and \'\' for false, or the undefined value if the
1321file doesn't exist.
378cc40b 1322Precedence is higher than logical and relational operators, but lower than
1323arithmetic operators.
1324The operator may be any of:
1325.nf
1326 \-r File is readable by effective uid.
a687059c 1327 \-w File is writable by effective uid.
378cc40b 1328 \-x File is executable by effective uid.
1329 \-o File is owned by effective uid.
1330 \-R File is readable by real uid.
a687059c 1331 \-W File is writable by real uid.
378cc40b 1332 \-X File is executable by real uid.
1333 \-O File is owned by real uid.
1334 \-e File exists.
1335 \-z File has zero size.
1336 \-s File has non-zero size.
1337 \-f File is a plain file.
1338 \-d File is a directory.
1339 \-l File is a symbolic link.
1340 \-p File is a named pipe (FIFO).
1341 \-S File is a socket.
1342 \-b File is a block special file.
1343 \-c File is a character special file.
1344 \-u File has setuid bit set.
1345 \-g File has setgid bit set.
1346 \-k File has sticky bit set.
1347 \-t Filehandle is opened to a tty.
1348 \-T File is a text file.
1349 \-B File is a binary file (opposite of \-T).
1350
1351.fi
1352The interpretation of the file permission operators \-r, \-R, \-w, \-W, \-x and \-X
1353is based solely on the mode of the file and the uids and gids of the user.
1354There may be other reasons you can't actually read, write or execute the file.
1355Also note that, for the superuser, \-r, \-R, \-w and \-W always return 1, and
1356\-x and \-X return 1 if any execute bit is set in the mode.
1357Scripts run by the superuser may thus need to do a stat() in order to determine
1358the actual mode of the file, or temporarily set the uid to something else.
1359.Sp
1360Example:
1361.nf
1362.ne 7
1363
1364 while (<>) {
1365 chop;
1366 next unless \-f $_; # ignore specials
1367 .\|.\|.
1368 }
1369
1370.fi
a687059c 1371Note that \-s/a/b/ does not do a negated substitution.
1372Saying \-exp($foo) still works as expected, however\*(--only single letters
378cc40b 1373following a minus are interpreted as file tests.
1374.Sp
1375The \-T and \-B switches work as follows.
1376The first block or so of the file is examined for odd characters such as
1377strange control codes or metacharacters.
1378If too many odd characters (>10%) are found, it's a \-B file, otherwise it's a \-T file.
1379Also, any file containing null in the first block is considered a binary file.
1380If \-T or \-B is used on a filehandle, the current stdio buffer is examined
1381rather than the first block.
378cc40b 1382Both \-T and \-B return TRUE on a null file, or a file at EOF when testing
1383a filehandle.
8d063cd8 1384.PP
a687059c 1385If any of the file tests (or either stat operator) are given the special
1386filehandle consisting of a solitary underline, then the stat structure
1387of the previous file test (or stat operator) is used, saving a system
1388call.
1389(This doesn't work with \-t, and you need to remember that lstat and -l
1390will leave values in the stat structure for the symbolic link, not the
1391real file.)
1392Example:
1393.nf
1394
1395 print "Can do.\en" if -r $a || -w _ || -x _;
1396
1397.ne 9
1398 stat($filename);
1399 print "Readable\en" if -r _;
1400 print "Writable\en" if -w _;
1401 print "Executable\en" if -x _;
1402 print "Setuid\en" if -u _;
1403 print "Setgid\en" if -g _;
1404 print "Sticky\en" if -k _;
1405 print "Text\en" if -T _;
1406 print "Binary\en" if -B _;
1407
1408.fi
1409.PP
8d063cd8 1410Here is what C has that
1411.I perl
1412doesn't:
1413.Ip "unary &" 12
1414Address-of operator.
1415.Ip "unary *" 12
1416Dereference-address operator.
378cc40b 1417.Ip "(TYPE)" 12
1418Type casting operator.
8d063cd8 1419.PP
1420Like C,
1421.I perl
1422does a certain amount of expression evaluation at compile time, whenever
1423it determines that all of the arguments to an operator are static and have
1424no side effects.
1425In particular, string concatenation happens at compile time between literals that don't do variable substitution.
1426Backslash interpretation also happens at compile time.
1427You can say
1428.nf
1429
1430.ne 2
a687059c 1431 \'Now is the time for all\' . "\|\e\|n" .
1432 \'good men to come to.\'
8d063cd8 1433
1434.fi
1435and this all reduces to one string internally.
1436.PP
378cc40b 1437The autoincrement operator has a little extra built-in magic to it.
1438If you increment a variable that is numeric, or that has ever been used in
1439a numeric context, you get a normal increment.
1440If, however, the variable has only been used in string contexts since it
1441was set, and has a value that is not null and matches the
a687059c 1442pattern /^[a\-zA\-Z]*[0\-9]*$/, the increment is done
378cc40b 1443as a string, preserving each character within its range, with carry:
1444.nf
1445
a687059c 1446 print ++($foo = \'99\'); # prints \*(L'100\*(R'
1447 print ++($foo = \'a0\'); # prints \*(L'a1\*(R'
1448 print ++($foo = \'Az\'); # prints \*(L'Ba\*(R'
1449 print ++($foo = \'zz\'); # prints \*(L'aaa\*(R'
378cc40b 1450
1451.fi
1452The autodecrement is not magical.