| 1 | =head1 NAME
|
|---|
| 2 | X<syntax>
|
|---|
| 3 |
|
|---|
| 4 | perlsyn - Perl syntax
|
|---|
| 5 |
|
|---|
| 6 | =head1 DESCRIPTION
|
|---|
| 7 |
|
|---|
| 8 | A Perl program consists of a sequence of declarations and statements
|
|---|
| 9 | which run from the top to the bottom. Loops, subroutines and other
|
|---|
| 10 | control structures allow you to jump around within the code.
|
|---|
| 11 |
|
|---|
| 12 | Perl is a B<free-form> language, you can format and indent it however
|
|---|
| 13 | you like. Whitespace mostly serves to separate tokens, unlike
|
|---|
| 14 | languages like Python where it is an important part of the syntax.
|
|---|
| 15 |
|
|---|
| 16 | Many of Perl's syntactic elements are B<optional>. Rather than
|
|---|
| 17 | requiring you to put parentheses around every function call and
|
|---|
| 18 | declare every variable, you can often leave such explicit elements off
|
|---|
| 19 | and Perl will figure out what you meant. This is known as B<Do What I
|
|---|
| 20 | Mean>, abbreviated B<DWIM>. It allows programmers to be B<lazy> and to
|
|---|
| 21 | code in a style with which they are comfortable.
|
|---|
| 22 |
|
|---|
| 23 | Perl B<borrows syntax> and concepts from many languages: awk, sed, C,
|
|---|
| 24 | Bourne Shell, Smalltalk, Lisp and even English. Other
|
|---|
| 25 | languages have borrowed syntax from Perl, particularly its regular
|
|---|
| 26 | expression extensions. So if you have programmed in another language
|
|---|
| 27 | you will see familiar pieces in Perl. They often work the same, but
|
|---|
| 28 | see L<perltrap> for information about how they differ.
|
|---|
| 29 |
|
|---|
| 30 | =head2 Declarations
|
|---|
| 31 | X<declaration> X<undef> X<undefined> X<uninitialized>
|
|---|
| 32 |
|
|---|
| 33 | The only things you need to declare in Perl are report formats and
|
|---|
| 34 | subroutines (and sometimes not even subroutines). A variable holds
|
|---|
| 35 | the undefined value (C<undef>) until it has been assigned a defined
|
|---|
| 36 | value, which is anything other than C<undef>. When used as a number,
|
|---|
| 37 | C<undef> is treated as C<0>; when used as a string, it is treated as
|
|---|
| 38 | the empty string, C<"">; and when used as a reference that isn't being
|
|---|
| 39 | assigned to, it is treated as an error. If you enable warnings,
|
|---|
| 40 | you'll be notified of an uninitialized value whenever you treat
|
|---|
| 41 | C<undef> as a string or a number. Well, usually. Boolean contexts,
|
|---|
| 42 | such as:
|
|---|
|
|---|