MAN page from Old RedHat 5.X perl-5.004-4.i386.rpm
PERLSYN
Section: Perl Programmers Reference Guide (1)
Updated: perl 5.004, patch 04
Index NAME
perlsyn - Perl syntax
DESCRIPTION
A Perl script consists of a sequence of declarations and statements.The only things that need to be declared in Perl are report formatsand subroutines. See the sections below for more information on thosedeclarations. All uninitialized user-created objects are assumed tostart with a null or 0 value until they are defined by some explicitoperation such as assignment. (Though you can get warnings about theuse of undefined values if you like.) The sequence of statements isexecuted just once, unlike in
sed and
awk scripts, where thesequence of statements is executed for each input line. While this meansthat you must explicitly loop over the lines of your input file (orfiles), it also means you have much more control over which files andwhich lines you look at. (Actually, I'm lying---it is possible to do animplicit loop with either the
-n or
-p switch. It's just not themandatory default like it is in
sed and
awk.)
Declarations
Perl is, for the most part, a free-form language. (The onlyexception to this is format declarations, for obvious reasons.) Commentsare indicated by the ``#'' character, and extend to the end of the line. Ifyou attempt to use
/* */ C-style comments, it will be interpretedeither as division or pattern matching, depending on the context, and C
++// comments just look like a null regular expression, so don't dothat.
A declaration can be put anywhere a statement can, but has no effect onthe execution of the primary sequence of statements---declarations alltake effect at compile time. Typically all the declarations are put atthe beginning or the end of the script. However, if you're usinglexically-scoped private variables created with my(), you'll have to make sureyour format or subroutine definition is within the same block scopeas the my if you expect to be able to access those private variables.
Declaring a subroutine allows a subroutine name to be used as if it were alist operator from that point forward in the program. You can declare asubroutine without defining it by saying sub name, thus:
sub myname; $me = myname $0 or die "can't get myname";
Note that it functions as a list operator, not as a unary operator; sobe careful to use
or instead of
|| in this case. However, ifyou were to declare the subroutine as
sub myname ($), then
myname would functonion as a unary operator, so either
or or
|| would work.
Subroutines declarations can also be loaded up with the require statementor both loaded and imported into your namespace with a use statement.See the perlmod manpage for details on this.
A statement sequence may contain declarations of lexically-scopedvariables, but apart from declaring a variable name, the declaration actslike an ordinary statement, and is elaborated within the sequence ofstatements as if it were an ordinary statement. That means it actuallyhas both compile-time and run-time effects.
Simple statements
The only kind of simple statement is an expression evaluated for itsside effects. Every simple statement must be terminated with asemicolon, unless it is the final statement in a block, in which casethe semicolon is optional. (A semicolon is still encouraged there if theblock takes up more than one line, because you may eventually add another line.)Note that there are some operators like
eval {} and
do {} that looklike compound statements, but aren't (they're just TERMs in an expression),and thus need an explicit termination if used as the last item in a statement.
Any simple statement may optionally be followed by a SINGLE modifier,just before the terminating semicolon (or block ending). The possiblemodifiers are:
if EXPR unless EXPR while EXPR until EXPR
The
if and
unless modifiers have the expected semantics,presuming you're a speaker of English. The
while and
untilmodifiers also have the usual ``while loop'' semantics (conditionalevaluated first), except when applied to a do-
BLOCK (or to thenow-deprecated do-
SUBROUTINE statement), in which case the blockexecutes once before the conditional is evaluated. This is so that youcan write loops like:
do { $line = <STDIN>; ... } until $line eq ".\n";See the
do entry in the
perlfunc manpage. Note also that the loop controlstatements described later will
NOT work in this construct, becausemodifiers don't take loop labels. Sorry. You can always wrapanother block around it to do that sort of thing.
Compound statements
In Perl, a sequence of statements that defines a scope is called a block.Sometimes a block is delimited by the file containing it (in the caseof a required file, or the program as a whole), and sometimes a blockis delimited by the extent of a string (in the case of an eval).
But generally, a block is delimited by curly brackets, also known as braces.We will call this syntactic construct a BLOCK.
The following compound statements may be used to control flow:
if (EXPR) BLOCK if (EXPR) BLOCK else BLOCK if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK LABEL while (EXPR) BLOCK LABEL while (EXPR) BLOCK continue BLOCK LABEL for (EXPR; EXPR; EXPR) BLOCK LABEL foreach VAR (LIST) BLOCK LABEL BLOCK continue BLOCK
Note that, unlike C and Pascal, these are defined in terms of BLOCKs,not statements. This means that the curly brackets are
required--nodangling statements allowed. If you want to write conditionals withoutcurly brackets there are several other ways to do it. The followingall do the same thing:
if (!open(FOO)) { die "Can't open $FOO: $!"; } die "Can't open $FOO: $!" unless open(FOO); open(FOO) or die "Can't open $FOO: $!"; # FOO or bust! open(FOO) ? 'hi mom' : die "Can't open $FOO: $!"; # a bit exotic, that last oneThe
if statement is straightforward. Because BLOCKs are alwaysbounded by curly brackets, there is never any ambiguity about which
if an
else goes with. If you use
unless in place of
if,the sense of the test is reversed.
The while statement executes the block as long as the expression istrue (does not evaluate to the null string or 0 or ``0"). The LABEL isoptional, and if present, consists of an identifier followed by a colon.The LABEL identifies the loop for the loop control statements next,last, and redo. If the LABEL is omitted, the loop control statementrefers to the innermost enclosing loop. This may include dynamicallylooking back your call-stack at run time to find the LABEL. Suchdesperate behavior triggers a warning if you use the -w flag.
If there is a continue BLOCK, it is always executed just before theconditional is about to be evaluated again, just like the third part of afor loop in C. Thus it can be used to increment a loop variable, evenwhen the loop has been continued via the next statement (which issimilar to the C continue statement).
Loop Control
The
next command is like the
continue statement in C; it startsthe next iteration of the loop:
LINE: while (<STDIN>) { next LINE if /^#/; # discard comments ... }The
last command is like the
break statement in C (as used inloops); it immediately exits the loop in question. The
continue block, if any, is not executed:
LINE: while (<STDIN>) { last LINE if /^$/; # exit when done with header ... }The
redo command restarts the loop block without evaluating theconditional again. The
continue block, if any, is
not executed.This command is normally used by programs that want to lie to themselvesabout what was just input.
For example, when processing a file like /etc/termcap.If your input lines might end in backslashes to indicate continuation, youwant to skip ahead and get the next record.
while (<>) { chomp; if (s/\\$//) { $_ .= <>; redo unless eof(); } # now process $_ }which is Perl short-hand for the more explicitly written version:
LINE: while (defined($line = <ARGV>)) { chomp($line); if ($line =~ s/\\$//) { $line .= <ARGV>; redo LINE unless eof(); # not eof(ARGV)! } # now process $line }Or here's a simpleminded Pascal comment stripper (warning: assumes no{ or } in strings).
LINE: while (<STDIN>) { while (s|({.*}.*){.*}|$1 |) {} s|{.*}| |; if (s|{.*| |) { $front = $_; while (<STDIN>) { if (/}/) { # end of comment? s|^|$front{|; redo LINE; } } } print; }Note that if there were a
continue block on the above code, it would getexecuted even on discarded lines.
If the word while is replaced by the word until, the sense of thetest is reversed, but the conditional is still tested before the firstiteration.
The form while/if BLOCK BLOCK, available in Perl 4, is no longeravailable. Replace any occurrence of if BLOCK by if (do BLOCK).
For Loops
Perl's C-style
for loop works exactly like the corresponding
while loop;that means that this:
for ($i = 1; $i < 10; $i++) { ... }is the same as this:
$i = 1; while ($i < 10) { ... } continue { $i++; }(There is one minor difference: The first form implies a lexical scopefor variables declared with
my in the initialization expression.)
Besides the normal array index looping, for can lend itselfto many other interesting applications. Here's one that avoids theproblem you get into if you explicitly test for end-of-file onan interactive file descriptor causing your program to appear tohang.
$on_a_tty = -t STDIN && -t STDOUT; sub prompt { print "yes? " if $on_a_tty } for ( prompt(); <STDIN>; prompt() ) { # do something } Foreach Loops
The
foreach loop iterates over a normal list value and sets thevariable
VAR to be each element of the list in turn. If the variableis preceded with the keyword
my, then it is lexically scoped, andis therefore visible only within the loop. Otherwise, the variable isimplicitly local to the loop and regains its former value upon exitingthe loop. If the variable was previously declared with
my, it usesthat variable instead of the global one, but it's still localized tothe loop. (Note that a lexically scoped variable can cause problemswith you have subroutine or format declarations.)
The foreach keyword is actually a synonym for the for keyword, soyou can use foreach for readability or for for brevity. If VAR isomitted, $_ is set to each value. If LIST is an actual array (as opposedto an expression returning a list value), you can modify each element ofthe array by modifying VAR inside the loop. That's because the foreachloop index variable is an implicit alias for each item in the list thatyou're looping over.
Examples:
for (@ary) { s/foo/bar/ } foreach my $elem (@elements) { $elem *= 2; } for $count (10,9,8,7,6,5,4,3,2,1,'BOOM') { print $count, "\n"; sleep(1); } for (1..15) { print "Merry Christmas\n"; } foreach $item (split(/:[\\\n:]*/, $ENV{TERMCAP})) { print "Item: $item\n"; }Here's how a C programmer might code up a particular algorithm in Perl:
for (my $i = 0; $i < @ary1; $i++) { for (my $j = 0; $j < @ary2; $j++) { if ($ary1[$i] > $ary2[$j]) { last; # can't go to outer :-( } $ary1[$i] += $ary2[$j]; } # this is where that last takes me }Whereas here's how a Perl programmer more comfortable with the idiom mightdo it:
OUTER: foreach my $wid (@ary1) { INNER: foreach my $jet (@ary2) { next OUTER if $wid > $jet; $wid += $jet; } }See how much easier this is? It's cleaner, safer, and faster. It'scleaner because it's less noisy. It's safer because if code gets addedbetween the inner and outer loops later on, the new code won't beaccidentally executed. The
next explicitly iterates the other looprather than merely terminating the inner one. And it's faster becausePerl executes a
foreach statement more rapidly than it would theequivalent
for loop.
Basic BLOCKs and Switch Statements
A
BLOCK by itself (labeled or not) is semantically equivalent to aloop that executes once. Thus you can use any of the loop controlstatements in it to leave or restart the block. (Note that this is
NOT true in
eval{},
sub{}, or contrary to popular belief
do{} blocks, which do
NOT count as loops.) The
continueblock is optional.
The BLOCK construct is particularly nice for doing casestructures.
SWITCH: { if (/^abc/) { $abc = 1; last SWITCH; } if (/^def/) { $def = 1; last SWITCH; } if (/^xyz/) { $xyz = 1; last SWITCH; } $nothing = 1; }There is no official switch statement in Perl, because there arealready several ways to write the equivalent. In addition to theabove, you could write
SWITCH: { $abc = 1, last SWITCH if /^abc/; $def = 1, last SWITCH if /^def/; $xyz = 1, last SWITCH if /^xyz/; $nothing = 1; }(That's actually not as strange as it looks once you realize that you canuse loop control ``operators'' within an expression, That's just the normalC comma operator.)
or
SWITCH: { /^abc/ && do { $abc = 1; last SWITCH; }; /^def/ && do { $def = 1; last SWITCH; }; /^xyz/ && do { $xyz = 1; last SWITCH; }; $nothing = 1; }or formatted so it stands out more as a ``proper'' switch statement:
SWITCH: { /^abc/ && do { $abc = 1; last SWITCH; }; /^def/ && do { $def = 1; last SWITCH; }; /^xyz/ && do { $xyz = 1; last SWITCH; }; $nothing = 1; }or
SWITCH: { /^abc/ and $abc = 1, last SWITCH; /^def/ and $def = 1, last SWITCH; /^xyz/ and $xyz = 1, last SWITCH; $nothing = 1; }or even, horrors,
if (/^abc/) { $abc = 1 } elsif (/^def/) { $def = 1 } elsif (/^xyz/) { $xyz = 1 } else { $nothing = 1 }A common idiom for a switch statement is to use
foreach's aliasing to makea temporary assignment to
$_ for convenient matching:
SWITCH: for ($where) { /In Card Names/ && do { push @flags, '-e'; last; }; /Anywhere/ && do { push @flags, '-h'; last; }; /In Rulings/ && do { last; }; die "unknown value for form variable where: `$where'"; }Another interesting approach to a switch statement is arrangefor a
do block to return the proper value:
$amode = do { if ($flag & O_RDONLY) { "r" } elsif ($flag & O_WRONLY) { ($flag & O_APPEND) ? "a" : "w" } elsif ($flag & O_RDWR) { if ($flag & O_CREAT) { "w+" } else { ($flag & O_APPEND) ? "a+" : "r+" } } }; Goto
Although not for the faint of heart, Perl does support a
goto statement.A loop's
LABEL is not actually a valid target for a
goto;it's just the name of the loop. There are three forms: goto-
LABEL,goto-
EXPR, and goto-&
NAME.
The goto-LABEL form finds the statement labeled with LABEL and resumesexecution there. It may not be used to go into any construct thatrequires initialization, such as a subroutine or a foreach loop. Italso can't be used to go into a construct that is optimized away. Itcan be used to go almost anywhere else within the dynamic scope,including out of subroutines, but it's usually better to use some otherconstruct such as last or die. The author of Perl has never felt theneed to use this form of goto (in Perl, that is---C is another matter).
The goto-EXPR form expects a label name, whose scope will be resolveddynamically. This allows for computed gotos per FORTRAN, but isn'tnecessarily recommended if you're optimizing for maintainability:
goto ("FOO", "BAR", "GLARCH")[$i];The goto-&
NAME form is highly magical, and substitutes a call to thenamed subroutine for the currently running subroutine. This is used by
AUTOLOAD() subroutines that wish to load another subroutine and thenpretend that the other subroutine had been called in the first place(except that any modifications to
@_ in the current subroutine arepropagated to the other subroutine.) After the
goto, not even
caller()will be able to tell that this routine was called first.
In almost all cases like this, it's usually a far, far better idea to use thestructured control flow mechanisms of next, last, or redo instead ofresorting to a goto. For certain applications, the catch and throw pair ofeval{} and die() for exception processing can also be a prudent approach.
PODs: Embedded Documentation
Perl has a mechanism for intermixing documentation with source code.While it's expecting the beginning of a new statement, if the compilerencounters a line that begins with an equal sign and a word, like this
=head1 Here There Be Pods!
Then that text and all remaining text up through and including a linebeginning with
=cut will be ignored. The format of the interveningtext is described in the
perlpod manpage.
This allows you to intermix your source codeand your documentation text freely, as in
=item snazzle($)
The snazzle() function will behave in the most spectacular form that you can possibly imagine, not even excepting cybernetic pyrotechnics.
=cut back to the compiler, nuff of this pod stuff!
sub snazzle($) { my $thingie = shift; ......... }Note that pod translators should look at only paragraphs beginningwith a pod directive (it makes parsing easier), whereas the compileractually knows to look for pod escapes even in the middle of aparagraph. This means that the following secret stuff will beignored by both the compiler and the translators.
$a=3; =secret stuff warn "Neither POD nor CODE!?" =cut back print "got $a\n";
You probably shouldn't rely upon the
warn() being podded out forever.Not all pod translators are well-behaved in this regard, and perhapsthe compiler will become pickier.
One may also use pod directives to quickly comment out a sectionof code.
Plain Old Comments (Not!)
Much like the C preprocessor, perl can process line directives. Usingthis, one can control perl's idea of filenames and line numbers inerror or warning messages (especially for strings that are processedwith
eval()). The syntax for this mechanism is the same as for mostC preprocessors: it matches the regular expression
/^#\s*line\s+(\d+)\s*(?:\s"([^"]*)")?/ with
$1 being the linenumber for the next line, and
$2 being the optional filename(specified within quotes).
Here are some examples that you should be able to type into your commandshell:
% perl # line 200 "bzzzt" # the `#' on the previous line must be the first char on line die 'foo'; __END__ foo at bzzzt line 201.
% perl # line 200 "bzzzt" eval qq[\n#line 2001 ""\ndie 'foo']; print $@; __END__ foo at - line 2001.
% perl eval qq[\n#line 200 "foo bar"\ndie 'foo']; print $@; __END__ foo at foo bar line 200.
% perl # line 345 "goop" eval "\n#line " . __LINE__ . ' "' . __FILE__ ."\"\ndie 'foo'"; print $@; __END__ foo at goop line 345.
Index
- NAME
- DESCRIPTION
- Declarations
- Simple statements
- Compound statements
- Loop Control
- For Loops
- Foreach Loops
- Basic BLOCKs and Switch Statements
- Goto
- PODs: Embedded Documentation
- Plain Old Comments (Not!)
This document was created byman2html,using the manual pages.