home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
SLAKWARE
/
D13
/
PERL2.TGZ
/
perl2.tar
/
usr
/
lib
/
perl5
/
pod
/
perlsyn.pod
< prev
next >
Wrap
Text File
|
1996-06-28
|
18KB
|
509 lines
=head1 NAME
perlsyn - Perl syntax
=head1 DESCRIPTION
A Perl script consists of a sequence of declarations and statements.
The only things that need to be declared in Perl are report formats
and subroutines. See the sections below for more information on those
declarations. All uninitialized user-created objects are assumed to
start with a null or 0 value until they are defined by some explicit
operation such as assignment. (Though you can get warnings about the
use of undefined values if you like.) The sequence of statements is
executed just once, unlike in B<sed> and B<awk> scripts, where the
sequence of statements is executed for each input line. While this means
that you must explicitly loop over the lines of your input file (or
files), it also means you have much more control over which files and
which lines you look at. (Actually, I'm lying--it is possible to do an
implicit loop with either the B<-n> or B<-p> switch. It's just not the
mandatory default like it is in B<sed> and B<awk>.)
=head2 Declarations
Perl is, for the most part, a free-form language. (The only
exception to this is format declarations, for obvious reasons.) Comments
are indicated by the "#" character, and extend to the end of the line. If
you attempt to use C</* */> C-style comments, it will be interpreted
either as division or pattern matching, depending on the context, and C++
C<//> comments just look like a null regular expression, so don't do
that.
A declaration can be put anywhere a statement can, but has no effect on
the execution of the primary sequence of statements--declarations all
take effect at compile time. Typically all the declarations are put at
the beginning or the end of the script. However, if you're using
lexically-scoped private variables created with my(), you'll have to make sure
your format or subroutine definition is within the same block scope
as the my if you expect to to be able to access those private variables.
Declaring a subroutine allows a subroutine name to be used as if it were a
list operator from that point forward in the program. You can declare a
subroutine (prototyped to take one scalar parameter) without defining it by saying just:
sub myname ($);
$me = myname $0 or die "can't get myname";
Note that it functions as a list operator though, not as a unary
operator, so be careful to use C<or> instead of C<||> there.
Subroutines declarations can also be loaded up with the C<require> statement
or both loaded and imported into your namespace with a C<use> statement.
See L<perlmod> for details on this.
A statement sequence may contain declarations of lexically-scoped
variables, but apart from declaring a variable name, the declaration acts
like an ordinary statement, and is elaborated within the sequence of
statements as if it were an ordinary statement. That means it actually
has both compile-time and run-time effects.
=head2 Simple statements
The only kind of simple statement is an expression evaluated for its
side effects. Every simple statement must be terminated with a
semicolon, unless it is the final statement in a block, in which case
the semicolon is optional. (A semicolon is still encouraged there if the
block takes up more than one line, since you may eventually add another line.)
Note that there are some operators like C<eval {}> and C<do {}> that look
like 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 I<SINGLE> modifier,
just before the terminating semicolon (or block ending). The possible
modifiers are:
if EXPR
unless EXPR
while EXPR
until EXPR
The C<if> and C<unless> modifiers have the expected semantics,
presuming you're a speaker of English. The C<while> and C<until>
modifiers also have the usual "while loop" semantics (conditional
evaluated first), except when applied to a do-BLOCK (or to the
now-deprecated do-SUBROUTINE statement), in which case the block
executes once before the conditional is evaluated. This is so that you
can write loops like:
do {
$line = <STDIN>;
...
} until $line eq ".\n";
See L<perlfunc/do>. Note also that the loop control
statements described later will I<NOT> work in this construct, since
modifiers don't take loop labels. Sorry. You can always wrap
another block around it to do that sort of thing.
=head2 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 case
of a required file, or the program as a whole), and sometimes a block
is 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 I<required>--no
dangling statements allowed. If you want to write conditionals without
curly brackets there are several other ways to do it. The following
all 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 one
The C<if> statement is straightforward. Since BLOCKs are always
bounded by curly brackets, there is never any ambiguity about which
C<if> an C<else> goes with. If you use C<unless> in place of C<if>,
the sense of the test is reversed.
The C<while> statement executes the block as long as the expression is
true (does not evaluate to the null string or 0 or "0"). The LABEL is
optional, and if present, consists of an identifier followed by a colon.
The LABEL identifies the loop for the loop control statements C<next>,
C<last>, and C<redo>. If the LABEL is omitted, the loop control statement
refers to the innermost enclosing loop. This may include dynamically
looking back your call-stack at run time to find the LABEL. Such
desperate behavior triggers a warning if you use the B<-w> flag.
If there is a C<continue> BLOCK, it is always executed just before the
conditional is about to be evaluated again, just like the third part of a
C<for> loop in C. Thus it can be used to increment a loop variable, even
when the loop has been continued via the C<next> statement (which is
similar to the C C<continue> statement).
=head2 Loop Control
The C<next> command is like the C<continue> statement in C; it starts
the next iteration of the loop:
LINE: while (<STDIN>) {
next LINE if /^#/; # discard comments
...
}
The C<last> command is like the C<break> statement in C (as used in
loops); it immediately exits the loop in question. The
C<continue> block, if any, is not executed:
LINE: while (<STDIN>) {
last LINE if /^$/; # exit when done with header
...
}
The C<redo> command restarts the loop block without evaluating the
conditional again. The C<continue> block, if any, is I<not> executed.
This command is normally used by programs that want to lie to themselves
about what was just input.
For example, when processing a file like F</etc/termcap>.
If your input lines might end in backslashes to indicate continuation, you
want 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 ($line = <ARGV>) {
chomp($line);
if ($line =~ s/\\$//) {
$line .= <ARGV>;
redo LINE unless eof(); # not eof(ARGV)!
}
# now process $line
}
Or here's a a simpleminded Pas