=head1 NAME
perlsyn - Perl syntax
=head1 DESCRIPTION
A Perl script consists of a sequence of declarations and statements.
The sequence of statements is executed just once, unlike in B
and B 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 or B switch. It's just not the mandatory
default like it is in B and B.)
Perl is, for the most part, a free-form language. (The only exception
to this is format declarations, for obvious reasons.) Text from a
C character until the end of the line is a comment, and is
ignored. 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.
=head2 Declarations
The only things you need to declare in Perl are report formats
and subroutines--and even undefined subroutines can be handled
through AUTOLOAD. A variable holds the undefined value (C)
until it has been assigned a defined value, which is anything
other than C. When used as a number, C is treated
as C; when used as a string, it is treated the empty string,
C; and when used as a reference that isn't being assigned
to, it is treated as an error. If you enable warnings, you'll
be notified of an uninitialized value whenever you treat C
as a string or a number. Well, usually. Boolean ("don't-care")
contexts and operators such as C, C, C, C, and
C<.> are always exempt from such warnings.
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 C, you'll
have to make sure
your format or subroutine definition is within the same block scope
as 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 a
list operator from that point forward in the program. You can declare a
subroutine without defining it by saying C, thus:
sub myname;
$me = myname $0 or die "can't get myname";
Note that myname() functions as a list operator, not as a unary operator;
so be careful to use C instead of C in this case. However, if
you were to declare the subroutine as C, then
C would function as a unary operator, so either C or
C would work.
Subroutines declarations can also be loaded up with the C statement
or both loaded and imported into your namespace with a C