Commit | Line | Data |
a0d0e21e |
1 | =head1 NAME |
2 | |
3 | perlsyn - Perl syntax |
4 | |
5 | =head1 DESCRIPTION |
6 | |
7 | A Perl script consists of a sequence of declarations and statements. |
8 | The only things that need to be declared in Perl are report formats |
9 | and subroutines. See the sections below for more information on those |
10 | declarations. All uninitialized user-created objects are assumed to |
11 | start with a null or 0 value until they are defined by some explicit |
12 | operation such as assignment. (Though you can get warnings about the |
13 | use of undefined values if you like.) The sequence of statements is |
14 | executed just once, unlike in B<sed> and B<awk> scripts, where the |
15 | sequence of statements is executed for each input line. While this means |
16 | that you must explicitly loop over the lines of your input file (or |
17 | files), it also means you have much more control over which files and |
18 | which lines you look at. (Actually, I'm lying--it is possible to do an |
19 | implicit loop with either the B<-n> or B<-p> switch. It's just not the |
20 | mandatory default like it is in B<sed> and B<awk>.) |
21 | |
22 | Perl is, for the most part, a free-form language. (The only |
23 | exception to this is format declarations, for obvious reasons.) Comments |
24 | are indicated by the "#" character, and extend to the end of the line. If |
25 | you attempt to use C</* */> C-style comments, it will be interpreted |
26 | either as division or pattern matching, depending on the context, and C++ |
27 | C<//> comments just look like a null regular expression, So don't do |
28 | that. |
29 | |
30 | A declaration can be put anywhere a statement can, but has no effect on |
31 | the execution of the primary sequence of statements--declarations all |
32 | take effect at compile time. Typically all the declarations are put at |
33 | the beginning or the end of the script. |
34 | |
35 | As of Perl 5, declaring a subroutine allows a subroutine name to be used |
36 | as if it were a list operator from that point forward in the program. You |
37 | can declare a subroutine without defining it by saying just |
38 | |
39 | sub myname; |
40 | $me = myname $0 or die "can't get myname"; |
41 | |
42 | Note that it functions as a list operator though, not a unary |
43 | operator, so be careful to use C<or> instead of C<||> there. |
44 | |
45 | Subroutines declarations can also be imported by a C<use> statement. |
46 | |
47 | Also as of Perl 5, a statement sequence may contain declarations of |
48 | lexically scoped variables, but apart from declaring a variable name, |
49 | the declaration acts like an ordinary statement, and is elaborated within |
50 | the sequence of statements as if it were an ordinary statement. |
51 | |
52 | =head2 Simple statements |
53 | |
54 | The only kind of simple statement is an expression evaluated for its |
55 | side effects. Every simple statement must be terminated with a |
56 | semicolon, unless it is the final statement in a block, in which case |
57 | the semicolon is optional. (A semicolon is still encouraged there if the |
748a9306 |
58 | block takes up more than one line, since you may eventually add another line.) |
a0d0e21e |
59 | Note that there are some operators like C<eval {}> and C<do {}> that look |
60 | like compound statements, but aren't (they're just TERMs in an expression), |
61 | and thus need an explicit termination |
62 | if used as the last item in a statement. |
63 | |
64 | Any simple statement may optionally be followed by a I<SINGLE> modifier, |
65 | just before the terminating semicolon (or block ending). The possible |
66 | modifiers are: |
67 | |
68 | if EXPR |
69 | unless EXPR |
70 | while EXPR |
71 | until EXPR |
72 | |
73 | The C<if> and C<unless> modifiers have the expected semantics, |
74 | presuming you're a speaker of English. The C<while> and C<until> |
75 | modifiers also have the usual "while loop" semantics (conditional |
76 | evaluated first), except when applied to a do-BLOCK (or to the |
77 | now-deprecated do-SUBROUTINE statement), in which case the block |
78 | executes once before the conditional is evaluated. This is so that you |
79 | can write loops like: |
80 | |
81 | do { |
82 | $_ = <STDIN>; |
83 | ... |
84 | } until $_ eq ".\n"; |
85 | |
86 | See L<perlfunc/do>. Note also that the loop control |
87 | statements described later will I<NOT> work in this construct, since |
88 | modifiers don't take loop labels. Sorry. You can always wrap |
89 | another block around it to do that sort of thing.) |
90 | |
91 | =head2 Compound statements |
92 | |
93 | In Perl, a sequence of statements that defines a scope is called a block. |
94 | Sometimes a block is delimited by the file containing it (in the case |
95 | of a required file, or the program as a whole), and sometimes a block |
96 | is delimited by the extent of a string (in the case of an eval). |
97 | |
98 | But generally, a block is delimited by curly brackets, also known as braces. |
99 | We will call this syntactic construct a BLOCK. |
100 | |
101 | The following compound statements may be used to control flow: |
102 | |
103 | if (EXPR) BLOCK |
104 | if (EXPR) BLOCK else BLOCK |
105 | if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK |
106 | LABEL while (EXPR) BLOCK |
107 | LABEL while (EXPR) BLOCK continue BLOCK |
108 | LABEL for (EXPR; EXPR; EXPR) BLOCK |
748a9306 |
109 | LABEL foreach VAR (LIST) BLOCK |
a0d0e21e |
110 | LABEL BLOCK continue BLOCK |
111 | |
112 | Note that, unlike C and Pascal, these are defined in terms of BLOCKs, |
113 | not statements. This means that the curly brackets are I<required>--no |
114 | dangling statements allowed. If you want to write conditionals without |
115 | curly brackets there are several other ways to do it. The following |
116 | all do the same thing: |
117 | |
118 | if (!open(FOO)) { die "Can't open $FOO: $!"; } |
119 | die "Can't open $FOO: $!" unless open(FOO); |
120 | open(FOO) or die "Can't open $FOO: $!"; # FOO or bust! |
121 | open(FOO) ? 'hi mom' : die "Can't open $FOO: $!"; |
122 | # a bit exotic, that last one |
123 | |
124 | The C<if> statement is straightforward. Since BLOCKs are always |
125 | bounded by curly brackets, there is never any ambiguity about which |
126 | C<if> an C<else> goes with. If you use C<unless> in place of C<if>, |
127 | the sense of the test is reversed. |
128 | |
129 | The C<while> statement executes the block as long as the expression is |
130 | true (does not evaluate to the null string or 0 or "0"). The LABEL is |
131 | optional, and if present, consists of an identifier followed by a |
132 | colon. The LABEL identifies the loop for the loop control statements |
133 | C<next>, C<last>, and C<redo> (see below). If there is a C<continue> |
134 | BLOCK, it is always executed just before the conditional is about to be |
135 | evaluated again, just like the third part of a C<for> loop in C. |
136 | Thus it can be used to increment a loop variable, even when the loop |
137 | has been continued via the C<next> statement (which is similar to the C |
138 | C<continue> statement). |
139 | |
140 | If the word C<while> is replaced by the word C<until>, the sense of the |
141 | test is reversed, but the conditional is still tested before the first |
142 | iteration. |
143 | |
144 | In either the C<if> or the C<while> statement, you may replace "(EXPR)" |
145 | with a BLOCK, and the conditional is true if the value of the last |
146 | statement in that block is true. (This feature continues to work in Perl |
147 | 5 but is deprecated. Please change any occurrences of "if BLOCK" to |
148 | "if (do BLOCK)".) |
149 | |
150 | The C-style C<for> loop works exactly like the corresponding C<while> loop: |
151 | |
152 | for ($i = 1; $i < 10; $i++) { |
153 | ... |
154 | } |
155 | |
156 | is the same as |
157 | |
158 | $i = 1; |
159 | while ($i < 10) { |
160 | ... |
161 | } continue { |
162 | $i++; |
163 | } |
164 | |
165 | The foreach loop iterates over a normal list value and sets the |
166 | variable VAR to be each element of the list in turn. The variable is |
748a9306 |
167 | implicitly local to the loop and regains its former value upon exiting |
168 | the loop. (If the variable was previously declared with C<my>, it uses |
169 | that variable instead of the global one, but it's still localized to |
170 | the loop.) The C<foreach> keyword is actually a synonym for the C<for> |
171 | keyword, so you can use C<foreach> for readability or C<for> for |
172 | brevity. If VAR is omitted, $_ is set to each value. If LIST is an |
173 | actual array (as opposed to an expression returning a list value), you |
174 | can modify each element of the array by modifying VAR inside the loop. |
175 | Examples: |
a0d0e21e |
176 | |
177 | for (@ary) { s/foo/bar/; } |
178 | |
179 | foreach $elem (@elements) { |
180 | $elem *= 2; |
181 | } |
182 | |
183 | for ((10,9,8,7,6,5,4,3,2,1,'BOOM')) { |
184 | print $_, "\n"; sleep(1); |
185 | } |
186 | |
187 | for (1..15) { print "Merry Christmas\n"; } |
188 | |
189 | foreach $item (split(/:[\\\n:]*/, $ENV{'TERMCAP'})) { |
190 | print "Item: $item\n"; |
191 | } |
192 | |
193 | A BLOCK by itself (labeled or not) is semantically equivalent to a loop |
194 | that executes once. Thus you can use any of the loop control |
195 | statements in it to leave or restart the block. The C<continue> block |
196 | is optional. This construct is particularly nice for doing case |
197 | structures. |
198 | |
199 | SWITCH: { |
200 | if (/^abc/) { $abc = 1; last SWITCH; } |
201 | if (/^def/) { $def = 1; last SWITCH; } |
202 | if (/^xyz/) { $xyz = 1; last SWITCH; } |
203 | $nothing = 1; |
204 | } |
205 | |
206 | There is no official switch statement in Perl, because there are |
207 | already several ways to write the equivalent. In addition to the |
208 | above, you could write |
209 | |
210 | SWITCH: { |
211 | $abc = 1, last SWITCH if /^abc/; |
212 | $def = 1, last SWITCH if /^def/; |
213 | $xyz = 1, last SWITCH if /^xyz/; |
214 | $nothing = 1; |
215 | } |
216 | |
217 | (That's actually not as strange as it looks one you realize that you can |
218 | use loop control "operators" within an expression, That's just the normal |
219 | C comma operator.) |
220 | |
221 | or |
222 | |
223 | SWITCH: { |
224 | /^abc/ && do { $abc = 1; last SWITCH; }; |
225 | /^def/ && do { $def = 1; last SWITCH; }; |
226 | /^xyz/ && do { $xyz = 1; last SWITCH; }; |
227 | $nothing = 1; |
228 | } |
229 | |
230 | or formatted so it stands out more as a "proper" switch statement: |
231 | |
232 | SWITCH: { |
233 | /^abc/ && do { |
234 | $abc = 1; |
235 | last SWITCH; |
236 | }; |
237 | |
238 | /^def/ && do { |
239 | $def = 1; |
240 | last SWITCH; |
241 | }; |
242 | |
243 | /^xyz/ && do { |
244 | $xyz = 1; |
245 | last SWITCH; |
246 | }; |
247 | $nothing = 1; |
248 | } |
249 | |
250 | or |
251 | |
252 | SWITCH: { |
253 | /^abc/ and $abc = 1, last SWITCH; |
254 | /^def/ and $def = 1, last SWITCH; |
255 | /^xyz/ and $xyz = 1, last SWITCH; |
256 | $nothing = 1; |
257 | } |
258 | |
259 | or even, horrors, |
260 | |
261 | if (/^abc/) |
262 | { $abc = 1 } |
263 | elsif (/^def/) |
264 | { $def = 1 } |
265 | elsif (/^xyz/) |
266 | { $xyz = 1 } |
267 | else |
268 | { $nothing = 1 } |
269 | |