=head1 NAME
X
perlsyn - Perl syntax
=head1 DESCRIPTION
A Perl program consists of a sequence of declarations and statements
which run from the top to the bottom. Loops, subroutines and other
control structures allow you to jump around within the code.
Perl is a B language, you can format and indent it however
you like. Whitespace mostly serves to separate tokens, unlike
languages like Python where it is an important part of the syntax.
Many of Perl's syntactic elements are B. Rather than
requiring you to put parentheses around every function call and
declare every variable, you can often leave such explicit elements off
and Perl will figure out what you meant. This is known as B, abbreviated B. It allows programmers to be B and to
code in a style with which they are comfortable.
Perl B and concepts from many languages: awk, sed, C,
Bourne Shell, Smalltalk, Lisp and even English. Other
languages have borrowed syntax from Perl, particularly its regular
expression extensions. So if you have programmed in another language
you will see familiar pieces in Perl. They often work the same, but
see L for information about how they differ.
=head2 Declarations
X X X X
The only things you need to declare in Perl are report formats and
subroutines (and sometimes not even subroutines). 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<0>; when used as a string, it is treated as
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 contexts,
such as:
my $a;
if ($a) {}
are exempt from warnings (because they care about truth rather than
definedness). Operators such as C<++>, C<-->, C<+=>,
C<-=>, and C<.=>, that operate on undefined left values such as:
my $a;
$a++;
are also 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:
X
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