Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "PERLSYN 1" | |
132 | .TH PERLSYN 1 "2002-06-08" "perl v5.8.0" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | perlsyn \- Perl syntax | |
135 | .SH "DESCRIPTION" | |
136 | .IX Header "DESCRIPTION" | |
137 | A Perl script consists of a sequence of declarations and statements. | |
138 | The sequence of statements is executed just once, unlike in \fBsed\fR | |
139 | and \fBawk\fR scripts, where the sequence of statements is executed | |
140 | for each input line. While this means that you must explicitly | |
141 | loop over the lines of your input file (or files), it also means | |
142 | you have much more control over which files and which lines you look at. | |
143 | (Actually, I'm lying\*(--it is possible to do an implicit loop with | |
144 | either the \fB\-n\fR or \fB\-p\fR switch. It's just not the mandatory | |
145 | default like it is in \fBsed\fR and \fBawk\fR.) | |
146 | .PP | |
147 | Perl is, for the most part, a free-form language. (The only exception | |
148 | to this is format declarations, for obvious reasons.) Text from a | |
149 | \&\f(CW"#"\fR character until the end of the line is a comment, and is | |
150 | ignored. If you attempt to use \f(CW\*(C`/* */\*(C'\fR C\-style comments, it will be | |
151 | interpreted either as division or pattern matching, depending on the | |
152 | context, and \*(C+ \f(CW\*(C`//\*(C'\fR comments just look like a null regular | |
153 | expression, so don't do that. | |
154 | .Sh "Declarations" | |
155 | .IX Subsection "Declarations" | |
156 | The only things you need to declare in Perl are report formats | |
157 | and subroutines\*(--and even undefined subroutines can be handled | |
158 | through \s-1AUTOLOAD\s0. A variable holds the undefined value (\f(CW\*(C`undef\*(C'\fR) | |
159 | until it has been assigned a defined value, which is anything | |
160 | other than \f(CW\*(C`undef\*(C'\fR. When used as a number, \f(CW\*(C`undef\*(C'\fR is treated | |
161 | as \f(CW0\fR; when used as a string, it is treated the empty string, | |
162 | \&\f(CW""\fR; and when used as a reference that isn't being assigned | |
163 | to, it is treated as an error. If you enable warnings, you'll | |
164 | be notified of an uninitialized value whenever you treat \f(CW\*(C`undef\*(C'\fR | |
165 | as a string or a number. Well, usually. Boolean contexts, such as: | |
166 | .PP | |
167 | .Vb 2 | |
168 | \& my $a; | |
169 | \& if ($a) {} | |
170 | .Ve | |
171 | .PP | |
172 | are exempt from warnings (because they care about truth rather than | |
173 | definedness). Operators such as \f(CW\*(C`++\*(C'\fR, \f(CW\*(C`\-\-\*(C'\fR, \f(CW\*(C`+=\*(C'\fR, | |
174 | \&\f(CW\*(C`\-=\*(C'\fR, and \f(CW\*(C`.=\*(C'\fR, that operate on undefined left values such as: | |
175 | .PP | |
176 | .Vb 2 | |
177 | \& my $a; | |
178 | \& $a++; | |
179 | .Ve | |
180 | .PP | |
181 | are also always exempt from such warnings. | |
182 | .PP | |
183 | A declaration can be put anywhere a statement can, but has no effect on | |
184 | the execution of the primary sequence of statements\*(--declarations all | |
185 | take effect at compile time. Typically all the declarations are put at | |
186 | the beginning or the end of the script. However, if you're using | |
187 | lexically-scoped private variables created with \f(CW\*(C`my()\*(C'\fR, you'll | |
188 | have to make sure | |
189 | your format or subroutine definition is within the same block scope | |
190 | as the my if you expect to be able to access those private variables. | |
191 | .PP | |
192 | Declaring a subroutine allows a subroutine name to be used as if it were a | |
193 | list operator from that point forward in the program. You can declare a | |
194 | subroutine without defining it by saying \f(CW\*(C`sub name\*(C'\fR, thus: | |
195 | .PP | |
196 | .Vb 2 | |
197 | \& sub myname; | |
198 | \& $me = myname $0 or die "can't get myname"; | |
199 | .Ve | |
200 | .PP | |
201 | Note that \fImyname()\fR functions as a list operator, not as a unary operator; | |
202 | so be careful to use \f(CW\*(C`or\*(C'\fR instead of \f(CW\*(C`||\*(C'\fR in this case. However, if | |
203 | you were to declare the subroutine as \f(CW\*(C`sub myname ($)\*(C'\fR, then | |
204 | \&\f(CW\*(C`myname\*(C'\fR would function as a unary operator, so either \f(CW\*(C`or\*(C'\fR or | |
205 | \&\f(CW\*(C`||\*(C'\fR would work. | |
206 | .PP | |
207 | Subroutines declarations can also be loaded up with the \f(CW\*(C`require\*(C'\fR statement | |
208 | or both loaded and imported into your namespace with a \f(CW\*(C`use\*(C'\fR statement. | |
209 | See perlmod for details on this. | |
210 | .PP | |
211 | A statement sequence may contain declarations of lexically-scoped | |
212 | variables, but apart from declaring a variable name, the declaration acts | |
213 | like an ordinary statement, and is elaborated within the sequence of | |
214 | statements as if it were an ordinary statement. That means it actually | |
215 | has both compile-time and run-time effects. | |
216 | .Sh "Simple statements" | |
217 | .IX Subsection "Simple statements" | |
218 | The only kind of simple statement is an expression evaluated for its | |
219 | side effects. Every simple statement must be terminated with a | |
220 | semicolon, unless it is the final statement in a block, in which case | |
221 | the semicolon is optional. (A semicolon is still encouraged there if the | |
222 | block takes up more than one line, because you may eventually add another line.) | |
223 | Note that there are some operators like \f(CW\*(C`eval {}\*(C'\fR and \f(CW\*(C`do {}\*(C'\fR that look | |
224 | like compound statements, but aren't (they're just TERMs in an expression), | |
225 | and thus need an explicit termination if used as the last item in a statement. | |
226 | .PP | |
227 | Any simple statement may optionally be followed by a \fI\s-1SINGLE\s0\fR modifier, | |
228 | just before the terminating semicolon (or block ending). The possible | |
229 | modifiers are: | |
230 | .PP | |
231 | .Vb 5 | |
232 | \& if EXPR | |
233 | \& unless EXPR | |
234 | \& while EXPR | |
235 | \& until EXPR | |
236 | \& foreach EXPR | |
237 | .Ve | |
238 | .PP | |
239 | The \f(CW\*(C`if\*(C'\fR and \f(CW\*(C`unless\*(C'\fR modifiers have the expected semantics, | |
240 | presuming you're a speaker of English. The \f(CW\*(C`foreach\*(C'\fR modifier is an | |
241 | iterator: For each value in \s-1EXPR\s0, it aliases \f(CW$_\fR to the value and | |
242 | executes the statement. The \f(CW\*(C`while\*(C'\fR and \f(CW\*(C`until\*(C'\fR modifiers have the | |
243 | usual "\f(CW\*(C`while\*(C'\fR loop" semantics (conditional evaluated first), except | |
244 | when applied to a \f(CW\*(C`do\*(C'\fR\-BLOCK (or to the deprecated \f(CW\*(C`do\*(C'\fR\-SUBROUTINE | |
245 | statement), in which case the block executes once before the | |
246 | conditional is evaluated. This is so that you can write loops like: | |
247 | .PP | |
248 | .Vb 4 | |
249 | \& do { | |
250 | \& $line = <STDIN>; | |
251 | \& ... | |
252 | \& } until $line eq ".\en"; | |
253 | .Ve | |
254 | .PP | |
255 | See \*(L"do\*(R" in perlfunc. Note also that the loop control statements described | |
256 | later will \fI\s-1NOT\s0\fR work in this construct, because modifiers don't take | |
257 | loop labels. Sorry. You can always put another block inside of it | |
258 | (for \f(CW\*(C`next\*(C'\fR) or around it (for \f(CW\*(C`last\*(C'\fR) to do that sort of thing. | |
259 | For \f(CW\*(C`next\*(C'\fR, just double the braces: | |
260 | .PP | |
261 | .Vb 4 | |
262 | \& do {{ | |
263 | \& next if $x == $y; | |
264 | \& # do something here | |
265 | \& }} until $x++ > $z; | |
266 | .Ve | |
267 | .PP | |
268 | For \f(CW\*(C`last\*(C'\fR, you have to be more elaborate: | |
269 | .PP | |
270 | .Vb 6 | |
271 | \& LOOP: { | |
272 | \& do { | |
273 | \& last if $x = $y**2; | |
274 | \& # do something here | |
275 | \& } while $x++ <= $z; | |
276 | \& } | |
277 | .Ve | |
278 | .Sh "Compound statements" | |
279 | .IX Subsection "Compound statements" | |
280 | In Perl, a sequence of statements that defines a scope is called a block. | |
281 | Sometimes a block is delimited by the file containing it (in the case | |
282 | of a required file, or the program as a whole), and sometimes a block | |
283 | is delimited by the extent of a string (in the case of an eval). | |
284 | .PP | |
285 | But generally, a block is delimited by curly brackets, also known as braces. | |
286 | We will call this syntactic construct a \s-1BLOCK\s0. | |
287 | .PP | |
288 | The following compound statements may be used to control flow: | |
289 | .PP | |
290 | .Vb 9 | |
291 | \& if (EXPR) BLOCK | |
292 | \& if (EXPR) BLOCK else BLOCK | |
293 | \& if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK | |
294 | \& LABEL while (EXPR) BLOCK | |
295 | \& LABEL while (EXPR) BLOCK continue BLOCK | |
296 | \& LABEL for (EXPR; EXPR; EXPR) BLOCK | |
297 | \& LABEL foreach VAR (LIST) BLOCK | |
298 | \& LABEL foreach VAR (LIST) BLOCK continue BLOCK | |
299 | \& LABEL BLOCK continue BLOCK | |
300 | .Ve | |
301 | .PP | |
302 | Note that, unlike C and Pascal, these are defined in terms of BLOCKs, | |
303 | not statements. This means that the curly brackets are \fIrequired\fR\-\-no | |
304 | dangling statements allowed. If you want to write conditionals without | |
305 | curly brackets there are several other ways to do it. The following | |
306 | all do the same thing: | |
307 | .PP | |
308 | .Vb 5 | |
309 | \& if (!open(FOO)) { die "Can't open $FOO: $!"; } | |
310 | \& die "Can't open $FOO: $!" unless open(FOO); | |
311 | \& open(FOO) or die "Can't open $FOO: $!"; # FOO or bust! | |
312 | \& open(FOO) ? 'hi mom' : die "Can't open $FOO: $!"; | |
313 | \& # a bit exotic, that last one | |
314 | .Ve | |
315 | .PP | |
316 | The \f(CW\*(C`if\*(C'\fR statement is straightforward. Because BLOCKs are always | |
317 | bounded by curly brackets, there is never any ambiguity about which | |
318 | \&\f(CW\*(C`if\*(C'\fR an \f(CW\*(C`else\*(C'\fR goes with. If you use \f(CW\*(C`unless\*(C'\fR in place of \f(CW\*(C`if\*(C'\fR, | |
319 | the sense of the test is reversed. | |
320 | .PP | |
321 | The \f(CW\*(C`while\*(C'\fR statement executes the block as long as the expression is | |
322 | true (does not evaluate to the null string \f(CW""\fR or \f(CW0\fR or \f(CW"0"\fR). | |
323 | The \s-1LABEL\s0 is optional, and if present, consists of an identifier followed | |
324 | by a colon. The \s-1LABEL\s0 identifies the loop for the loop control | |
325 | statements \f(CW\*(C`next\*(C'\fR, \f(CW\*(C`last\*(C'\fR, and \f(CW\*(C`redo\*(C'\fR. | |
326 | If the \s-1LABEL\s0 is omitted, the loop control statement | |
327 | refers to the innermost enclosing loop. This may include dynamically | |
328 | looking back your call-stack at run time to find the \s-1LABEL\s0. Such | |
329 | desperate behavior triggers a warning if you use the \f(CW\*(C`use warnings\*(C'\fR | |
330 | pragma or the \fB\-w\fR flag. | |
331 | Unlike a \f(CW\*(C`foreach\*(C'\fR statement, a \f(CW\*(C`while\*(C'\fR statement never implicitly | |
332 | localises any variables. | |
333 | .PP | |
334 | If there is a \f(CW\*(C`continue\*(C'\fR \s-1BLOCK\s0, it is always executed just before the | |
335 | conditional is about to be evaluated again, just like the third part of a | |
336 | \&\f(CW\*(C`for\*(C'\fR loop in C. Thus it can be used to increment a loop variable, even | |
337 | when the loop has been continued via the \f(CW\*(C`next\*(C'\fR statement (which is | |
338 | similar to the C \f(CW\*(C`continue\*(C'\fR statement). | |
339 | .Sh "Loop Control" | |
340 | .IX Subsection "Loop Control" | |
341 | The \f(CW\*(C`next\*(C'\fR command is like the \f(CW\*(C`continue\*(C'\fR statement in C; it starts | |
342 | the next iteration of the loop: | |
343 | .PP | |
344 | .Vb 4 | |
345 | \& LINE: while (<STDIN>) { | |
346 | \& next LINE if /^#/; # discard comments | |
347 | \& ... | |
348 | \& } | |
349 | .Ve | |
350 | .PP | |
351 | The \f(CW\*(C`last\*(C'\fR command is like the \f(CW\*(C`break\*(C'\fR statement in C (as used in | |
352 | loops); it immediately exits the loop in question. The | |
353 | \&\f(CW\*(C`continue\*(C'\fR block, if any, is not executed: | |
354 | .PP | |
355 | .Vb 4 | |
356 | \& LINE: while (<STDIN>) { | |
357 | \& last LINE if /^$/; # exit when done with header | |
358 | \& ... | |
359 | \& } | |
360 | .Ve | |
361 | .PP | |
362 | The \f(CW\*(C`redo\*(C'\fR command restarts the loop block without evaluating the | |
363 | conditional again. The \f(CW\*(C`continue\*(C'\fR block, if any, is \fInot\fR executed. | |
364 | This command is normally used by programs that want to lie to themselves | |
365 | about what was just input. | |
366 | .PP | |
367 | For example, when processing a file like \fI/etc/termcap\fR. | |
368 | If your input lines might end in backslashes to indicate continuation, you | |
369 | want to skip ahead and get the next record. | |
370 | .PP | |
371 | .Vb 8 | |
372 | \& while (<>) { | |
373 | \& chomp; | |
374 | \& if (s/\e\e$//) { | |
375 | \& $_ .= <>; | |
376 | \& redo unless eof(); | |
377 | \& } | |
378 | \& # now process $_ | |
379 | \& } | |
380 | .Ve | |
381 | .PP | |
382 | which is Perl short-hand for the more explicitly written version: | |
383 | .PP | |
384 | .Vb 8 | |
385 | \& LINE: while (defined($line = <ARGV>)) { | |
386 | \& chomp($line); | |
387 | \& if ($line =~ s/\e\e$//) { | |
388 | \& $line .= <ARGV>; | |
389 | \& redo LINE unless eof(); # not eof(ARGV)! | |
390 | \& } | |
391 | \& # now process $line | |
392 | \& } | |
393 | .Ve | |
394 | .PP | |
395 | Note that if there were a \f(CW\*(C`continue\*(C'\fR block on the above code, it would | |
396 | get executed only on lines discarded by the regex (since redo skips the | |
397 | continue block). A continue block is often used to reset line counters | |
398 | or \f(CW\*(C`?pat?\*(C'\fR one-time matches: | |
399 | .PP | |
400 | .Vb 10 | |
401 | \& # inspired by :1,$g/fred/s//WILMA/ | |
402 | \& while (<>) { | |
403 | \& ?(fred)? && s//WILMA $1 WILMA/; | |
404 | \& ?(barney)? && s//BETTY $1 BETTY/; | |
405 | \& ?(homer)? && s//MARGE $1 MARGE/; | |
406 | \& } continue { | |
407 | \& print "$ARGV $.: $_"; | |
408 | \& close ARGV if eof(); # reset $. | |
409 | \& reset if eof(); # reset ?pat? | |
410 | \& } | |
411 | .Ve | |
412 | .PP | |
413 | If the word \f(CW\*(C`while\*(C'\fR is replaced by the word \f(CW\*(C`until\*(C'\fR, the sense of the | |
414 | test is reversed, but the conditional is still tested before the first | |
415 | iteration. | |
416 | .PP | |
417 | The loop control statements don't work in an \f(CW\*(C`if\*(C'\fR or \f(CW\*(C`unless\*(C'\fR, since | |
418 | they aren't loops. You can double the braces to make them such, though. | |
419 | .PP | |
420 | .Vb 5 | |
421 | \& if (/pattern/) {{ | |
422 | \& last if /fred/; | |
423 | \& next if /barney/; # same effect as "last", but doesn't document as well | |
424 | \& # do something here | |
425 | \& }} | |
426 | .Ve | |
427 | .PP | |
428 | This is caused by the fact that a block by itself acts as a loop that | |
429 | executes once, see \*(L"Basic BLOCKs and Switch Statements\*(R". | |
430 | .PP | |
431 | The form \f(CW\*(C`while/if BLOCK BLOCK\*(C'\fR, available in Perl 4, is no longer | |
432 | available. Replace any occurrence of \f(CW\*(C`if BLOCK\*(C'\fR by \f(CW\*(C`if (do BLOCK)\*(C'\fR. | |
433 | .Sh "For Loops" | |
434 | .IX Subsection "For Loops" | |
435 | Perl's C\-style \f(CW\*(C`for\*(C'\fR loop works like the corresponding \f(CW\*(C`while\*(C'\fR loop; | |
436 | that means that this: | |
437 | .PP | |
438 | .Vb 3 | |
439 | \& for ($i = 1; $i < 10; $i++) { | |
440 | \& ... | |
441 | \& } | |
442 | .Ve | |
443 | .PP | |
444 | is the same as this: | |
445 | .PP | |
446 | .Vb 6 | |
447 | \& $i = 1; | |
448 | \& while ($i < 10) { | |
449 | \& ... | |
450 | \& } continue { | |
451 | \& $i++; | |
452 | \& } | |
453 | .Ve | |
454 | .PP | |
455 | There is one minor difference: if variables are declared with \f(CW\*(C`my\*(C'\fR | |
456 | in the initialization section of the \f(CW\*(C`for\*(C'\fR, the lexical scope of | |
457 | those variables is exactly the \f(CW\*(C`for\*(C'\fR loop (the body of the loop | |
458 | and the control sections). | |
459 | .PP | |
460 | Besides the normal array index looping, \f(CW\*(C`for\*(C'\fR can lend itself | |
461 | to many other interesting applications. Here's one that avoids the | |
462 | problem you get into if you explicitly test for end-of-file on | |
463 | an interactive file descriptor causing your program to appear to | |
464 | hang. | |
465 | .PP | |
466 | .Vb 5 | |
467 | \& $on_a_tty = -t STDIN && -t STDOUT; | |
468 | \& sub prompt { print "yes? " if $on_a_tty } | |
469 | \& for ( prompt(); <STDIN>; prompt() ) { | |
470 | \& # do something | |
471 | \& } | |
472 | .Ve | |
473 | .Sh "Foreach Loops" | |
474 | .IX Subsection "Foreach Loops" | |
475 | The \f(CW\*(C`foreach\*(C'\fR loop iterates over a normal list value and sets the | |
476 | variable \s-1VAR\s0 to be each element of the list in turn. If the variable | |
477 | is preceded with the keyword \f(CW\*(C`my\*(C'\fR, then it is lexically scoped, and | |
478 | is therefore visible only within the loop. Otherwise, the variable is | |
479 | implicitly local to the loop and regains its former value upon exiting | |
480 | the loop. If the variable was previously declared with \f(CW\*(C`my\*(C'\fR, it uses | |
481 | that variable instead of the global one, but it's still localized to | |
482 | the loop. | |
483 | .PP | |
484 | The \f(CW\*(C`foreach\*(C'\fR keyword is actually a synonym for the \f(CW\*(C`for\*(C'\fR keyword, so | |
485 | you can use \f(CW\*(C`foreach\*(C'\fR for readability or \f(CW\*(C`for\*(C'\fR for brevity. (Or because | |
486 | the Bourne shell is more familiar to you than \fIcsh\fR, so writing \f(CW\*(C`for\*(C'\fR | |
487 | comes more naturally.) If \s-1VAR\s0 is omitted, \f(CW$_\fR is set to each value. | |
488 | .PP | |
489 | If any element of \s-1LIST\s0 is an lvalue, you can modify it by modifying | |
490 | \&\s-1VAR\s0 inside the loop. Conversely, if any element of \s-1LIST\s0 is \s-1NOT\s0 an | |
491 | lvalue, any attempt to modify that element will fail. In other words, | |
492 | the \f(CW\*(C`foreach\*(C'\fR loop index variable is an implicit alias for each item | |
493 | in the list that you're looping over. | |
494 | .PP | |
495 | If any part of \s-1LIST\s0 is an array, \f(CW\*(C`foreach\*(C'\fR will get very confused if | |
496 | you add or remove elements within the loop body, for example with | |
497 | \&\f(CW\*(C`splice\*(C'\fR. So don't do that. | |
498 | .PP | |
499 | \&\f(CW\*(C`foreach\*(C'\fR probably won't do what you expect if \s-1VAR\s0 is a tied or other | |
500 | special variable. Don't do that either. | |
501 | .PP | |
502 | Examples: | |
503 | .PP | |
504 | .Vb 1 | |
505 | \& for (@ary) { s/foo/bar/ } | |
506 | .Ve | |
507 | .PP | |
508 | .Vb 3 | |
509 | \& for my $elem (@elements) { | |
510 | \& $elem *= 2; | |
511 | \& } | |
512 | .Ve | |
513 | .PP | |
514 | .Vb 3 | |
515 | \& for $count (10,9,8,7,6,5,4,3,2,1,'BOOM') { | |
516 | \& print $count, "\en"; sleep(1); | |
517 | \& } | |
518 | .Ve | |
519 | .PP | |
520 | .Vb 1 | |
521 | \& for (1..15) { print "Merry Christmas\en"; } | |
522 | .Ve | |
523 | .PP | |
524 | .Vb 3 | |
525 | \& foreach $item (split(/:[\e\e\en:]*/, $ENV{TERMCAP})) { | |
526 | \& print "Item: $item\en"; | |
527 | \& } | |
528 | .Ve | |
529 | .PP | |
530 | Here's how a C programmer might code up a particular algorithm in Perl: | |
531 | .PP | |
532 | .Vb 9 | |
533 | \& for (my $i = 0; $i < @ary1; $i++) { | |
534 | \& for (my $j = 0; $j < @ary2; $j++) { | |
535 | \& if ($ary1[$i] > $ary2[$j]) { | |
536 | \& last; # can't go to outer :-( | |
537 | \& } | |
538 | \& $ary1[$i] += $ary2[$j]; | |
539 | \& } | |
540 | \& # this is where that last takes me | |
541 | \& } | |
542 | .Ve | |
543 | .PP | |
544 | Whereas here's how a Perl programmer more comfortable with the idiom might | |
545 | do it: | |
546 | .PP | |
547 | .Vb 6 | |
548 | \& OUTER: for my $wid (@ary1) { | |
549 | \& INNER: for my $jet (@ary2) { | |
550 | \& next OUTER if $wid > $jet; | |
551 | \& $wid += $jet; | |
552 | \& } | |
553 | \& } | |
554 | .Ve | |
555 | .PP | |
556 | See how much easier this is? It's cleaner, safer, and faster. It's | |
557 | cleaner because it's less noisy. It's safer because if code gets added | |
558 | between the inner and outer loops later on, the new code won't be | |
559 | accidentally executed. The \f(CW\*(C`next\*(C'\fR explicitly iterates the other loop | |
560 | rather than merely terminating the inner one. And it's faster because | |
561 | Perl executes a \f(CW\*(C`foreach\*(C'\fR statement more rapidly than it would the | |
562 | equivalent \f(CW\*(C`for\*(C'\fR loop. | |
563 | .Sh "Basic BLOCKs and Switch Statements" | |
564 | .IX Subsection "Basic BLOCKs and Switch Statements" | |
565 | A \s-1BLOCK\s0 by itself (labeled or not) is semantically equivalent to a | |
566 | loop that executes once. Thus you can use any of the loop control | |
567 | statements in it to leave or restart the block. (Note that this is | |
568 | \&\fI\s-1NOT\s0\fR true in \f(CW\*(C`eval{}\*(C'\fR, \f(CW\*(C`sub{}\*(C'\fR, or contrary to popular belief | |
569 | \&\f(CW\*(C`do{}\*(C'\fR blocks, which do \fI\s-1NOT\s0\fR count as loops.) The \f(CW\*(C`continue\*(C'\fR | |
570 | block is optional. | |
571 | .PP | |
572 | The \s-1BLOCK\s0 construct is particularly nice for doing case | |
573 | structures. | |
574 | .PP | |
575 | .Vb 6 | |
576 | \& SWITCH: { | |
577 | \& if (/^abc/) { $abc = 1; last SWITCH; } | |
578 | \& if (/^def/) { $def = 1; last SWITCH; } | |
579 | \& if (/^xyz/) { $xyz = 1; last SWITCH; } | |
580 | \& $nothing = 1; | |
581 | \& } | |
582 | .Ve | |
583 | .PP | |
584 | There is no official \f(CW\*(C`switch\*(C'\fR statement in Perl, because there are | |
585 | already several ways to write the equivalent. | |
586 | .PP | |
587 | However, starting from Perl 5.8 to get switch and case one can use | |
588 | the Switch extension and say: | |
589 | .PP | |
590 | .Vb 1 | |
591 | \& use Switch; | |
592 | .Ve | |
593 | .PP | |
594 | after which one has switch and case. It is not as fast as it could be | |
595 | because it's not really part of the language (it's done using source | |
596 | filters) but it is available, and it's very flexible. | |
597 | .PP | |
598 | In addition to the above \s-1BLOCK\s0 construct, you could write | |
599 | .PP | |
600 | .Vb 6 | |
601 | \& SWITCH: { | |
602 | \& $abc = 1, last SWITCH if /^abc/; | |
603 | \& $def = 1, last SWITCH if /^def/; | |
604 | \& $xyz = 1, last SWITCH if /^xyz/; | |
605 | \& $nothing = 1; | |
606 | \& } | |
607 | .Ve | |
608 | .PP | |
609 | (That's actually not as strange as it looks once you realize that you can | |
610 | use loop control \*(L"operators\*(R" within an expression, That's just the normal | |
611 | C comma operator.) | |
612 | .PP | |
613 | or | |
614 | .PP | |
615 | .Vb 6 | |
616 | \& SWITCH: { | |
617 | \& /^abc/ && do { $abc = 1; last SWITCH; }; | |
618 | \& /^def/ && do { $def = 1; last SWITCH; }; | |
619 | \& /^xyz/ && do { $xyz = 1; last SWITCH; }; | |
620 | \& $nothing = 1; | |
621 | \& } | |
622 | .Ve | |
623 | .PP | |
624 | or formatted so it stands out more as a \*(L"proper\*(R" \f(CW\*(C`switch\*(C'\fR statement: | |
625 | .PP | |
626 | .Vb 5 | |
627 | \& SWITCH: { | |
628 | \& /^abc/ && do { | |
629 | \& $abc = 1; | |
630 | \& last SWITCH; | |
631 | \& }; | |
632 | .Ve | |
633 | .PP | |
634 | .Vb 4 | |
635 | \& /^def/ && do { | |
636 | \& $def = 1; | |
637 | \& last SWITCH; | |
638 | \& }; | |
639 | .Ve | |
640 | .PP | |
641 | .Vb 6 | |
642 | \& /^xyz/ && do { | |
643 | \& $xyz = 1; | |
644 | \& last SWITCH; | |
645 | \& }; | |
646 | \& $nothing = 1; | |
647 | \& } | |
648 | .Ve | |
649 | .PP | |
650 | or | |
651 | .PP | |
652 | .Vb 6 | |
653 | \& SWITCH: { | |
654 | \& /^abc/ and $abc = 1, last SWITCH; | |
655 | \& /^def/ and $def = 1, last SWITCH; | |
656 | \& /^xyz/ and $xyz = 1, last SWITCH; | |
657 | \& $nothing = 1; | |
658 | \& } | |
659 | .Ve | |
660 | .PP | |
661 | or even, horrors, | |
662 | .PP | |
663 | .Vb 8 | |
664 | \& if (/^abc/) | |
665 | \& { $abc = 1 } | |
666 | \& elsif (/^def/) | |
667 | \& { $def = 1 } | |
668 | \& elsif (/^xyz/) | |
669 | \& { $xyz = 1 } | |
670 | \& else | |
671 | \& { $nothing = 1 } | |
672 | .Ve | |
673 | .PP | |
674 | A common idiom for a \f(CW\*(C`switch\*(C'\fR statement is to use \f(CW\*(C`foreach\*(C'\fR's aliasing to make | |
675 | a temporary assignment to \f(CW$_\fR for convenient matching: | |
676 | .PP | |
677 | .Vb 6 | |
678 | \& SWITCH: for ($where) { | |
679 | \& /In Card Names/ && do { push @flags, '-e'; last; }; | |
680 | \& /Anywhere/ && do { push @flags, '-h'; last; }; | |
681 | \& /In Rulings/ && do { last; }; | |
682 | \& die "unknown value for form variable where: `$where'"; | |
683 | \& } | |
684 | .Ve | |
685 | .PP | |
686 | Another interesting approach to a switch statement is arrange | |
687 | for a \f(CW\*(C`do\*(C'\fR block to return the proper value: | |
688 | .PP | |
689 | .Vb 8 | |
690 | \& $amode = do { | |
691 | \& if ($flag & O_RDONLY) { "r" } # XXX: isn't this 0? | |
692 | \& elsif ($flag & O_WRONLY) { ($flag & O_APPEND) ? "a" : "w" } | |
693 | \& elsif ($flag & O_RDWR) { | |
694 | \& if ($flag & O_CREAT) { "w+" } | |
695 | \& else { ($flag & O_APPEND) ? "a+" : "r+" } | |
696 | \& } | |
697 | \& }; | |
698 | .Ve | |
699 | .PP | |
700 | Or | |
701 | .PP | |
702 | .Vb 5 | |
703 | \& print do { | |
704 | \& ($flags & O_WRONLY) ? "write-only" : | |
705 | \& ($flags & O_RDWR) ? "read-write" : | |
706 | \& "read-only"; | |
707 | \& }; | |
708 | .Ve | |
709 | .PP | |
710 | Or if you are certain that all the \f(CW\*(C`&&\*(C'\fR clauses are true, you can use | |
711 | something like this, which \*(L"switches\*(R" on the value of the | |
712 | \&\f(CW\*(C`HTTP_USER_AGENT\*(C'\fR environment variable. | |
713 | .PP | |
714 | .Vb 13 | |
715 | \& #!/usr/bin/perl | |
716 | \& # pick out jargon file page based on browser | |
717 | \& $dir = 'http://www.wins.uva.nl/~mes/jargon'; | |
718 | \& for ($ENV{HTTP_USER_AGENT}) { | |
719 | \& $page = /Mac/ && 'm/Macintrash.html' | |
720 | \& || /Win(dows )?NT/ && 'e/evilandrude.html' | |
721 | \& || /Win|MSIE|WebTV/ && 'm/MicroslothWindows.html' | |
722 | \& || /Linux/ && 'l/Linux.html' | |
723 | \& || /HP-UX/ && 'h/HP-SUX.html' | |
724 | \& || /SunOS/ && 's/ScumOS.html' | |
725 | \& || 'a/AppendixB.html'; | |
726 | \& } | |
727 | \& print "Location: $dir/$page\e015\e012\e015\e012"; | |
728 | .Ve | |
729 | .PP | |
730 | That kind of switch statement only works when you know the \f(CW\*(C`&&\*(C'\fR clauses | |
731 | will be true. If you don't, the previous \f(CW\*(C`?:\*(C'\fR example should be used. | |
732 | .PP | |
733 | You might also consider writing a hash of subroutine references | |
734 | instead of synthesizing a \f(CW\*(C`switch\*(C'\fR statement. | |
735 | .Sh "Goto" | |
736 | .IX Subsection "Goto" | |
737 | Although not for the faint of heart, Perl does support a \f(CW\*(C`goto\*(C'\fR | |
738 | statement. There are three forms: \f(CW\*(C`goto\*(C'\fR\-LABEL, \f(CW\*(C`goto\*(C'\fR\-EXPR, and | |
739 | \&\f(CW\*(C`goto\*(C'\fR\-&NAME. A loop's \s-1LABEL\s0 is not actually a valid target for | |
740 | a \f(CW\*(C`goto\*(C'\fR; it's just the name of the loop. | |
741 | .PP | |
742 | The \f(CW\*(C`goto\*(C'\fR\-LABEL form finds the statement labeled with \s-1LABEL\s0 and resumes | |
743 | execution there. It may not be used to go into any construct that | |
744 | requires initialization, such as a subroutine or a \f(CW\*(C`foreach\*(C'\fR loop. It | |
745 | also can't be used to go into a construct that is optimized away. It | |
746 | can be used to go almost anywhere else within the dynamic scope, | |
747 | including out of subroutines, but it's usually better to use some other | |
748 | construct such as \f(CW\*(C`last\*(C'\fR or \f(CW\*(C`die\*(C'\fR. The author of Perl has never felt the | |
749 | need to use this form of \f(CW\*(C`goto\*(C'\fR (in Perl, that is\*(--C is another matter). | |
750 | .PP | |
751 | The \f(CW\*(C`goto\*(C'\fR\-EXPR form expects a label name, whose scope will be resolved | |
752 | dynamically. This allows for computed \f(CW\*(C`goto\*(C'\fRs per \s-1FORTRAN\s0, but isn't | |
753 | necessarily recommended if you're optimizing for maintainability: | |
754 | .PP | |
755 | .Vb 1 | |
756 | \& goto(("FOO", "BAR", "GLARCH")[$i]); | |
757 | .Ve | |
758 | .PP | |
759 | The \f(CW\*(C`goto\*(C'\fR\-&NAME form is highly magical, and substitutes a call to the | |
760 | named subroutine for the currently running subroutine. This is used by | |
761 | \&\f(CW\*(C`AUTOLOAD()\*(C'\fR subroutines that wish to load another subroutine and then | |
762 | pretend that the other subroutine had been called in the first place | |
763 | (except that any modifications to \f(CW@_\fR in the current subroutine are | |
764 | propagated to the other subroutine.) After the \f(CW\*(C`goto\*(C'\fR, not even \f(CW\*(C`caller()\*(C'\fR | |
765 | will be able to tell that this routine was called first. | |
766 | .PP | |
767 | In almost all cases like this, it's usually a far, far better idea to use the | |
768 | structured control flow mechanisms of \f(CW\*(C`next\*(C'\fR, \f(CW\*(C`last\*(C'\fR, or \f(CW\*(C`redo\*(C'\fR instead of | |
769 | resorting to a \f(CW\*(C`goto\*(C'\fR. For certain applications, the catch and throw pair of | |
770 | \&\f(CW\*(C`eval{}\*(C'\fR and \fIdie()\fR for exception processing can also be a prudent approach. | |
771 | .Sh "PODs: Embedded Documentation" | |
772 | .IX Subsection "PODs: Embedded Documentation" | |
773 | Perl has a mechanism for intermixing documentation with source code. | |
774 | While it's expecting the beginning of a new statement, if the compiler | |
775 | encounters a line that begins with an equal sign and a word, like this | |
776 | .PP | |
777 | .Vb 1 | |
778 | \& =head1 Here There Be Pods! | |
779 | .Ve | |
780 | .PP | |
781 | Then that text and all remaining text up through and including a line | |
782 | beginning with \f(CW\*(C`=cut\*(C'\fR will be ignored. The format of the intervening | |
783 | text is described in perlpod. | |
784 | .PP | |
785 | This allows you to intermix your source code | |
786 | and your documentation text freely, as in | |
787 | .PP | |
788 | .Vb 1 | |
789 | \& =item snazzle($) | |
790 | .Ve | |
791 | .PP | |
792 | .Vb 3 | |
793 | \& The snazzle() function will behave in the most spectacular | |
794 | \& form that you can possibly imagine, not even excepting | |
795 | \& cybernetic pyrotechnics. | |
796 | .Ve | |
797 | .PP | |
798 | .Vb 1 | |
799 | \& =cut back to the compiler, nuff of this pod stuff! | |
800 | .Ve | |
801 | .PP | |
802 | .Vb 4 | |
803 | \& sub snazzle($) { | |
804 | \& my $thingie = shift; | |
805 | \& ......... | |
806 | \& } | |
807 | .Ve | |
808 | .PP | |
809 | Note that pod translators should look at only paragraphs beginning | |
810 | with a pod directive (it makes parsing easier), whereas the compiler | |
811 | actually knows to look for pod escapes even in the middle of a | |
812 | paragraph. This means that the following secret stuff will be | |
813 | ignored by both the compiler and the translators. | |
814 | .PP | |
815 | .Vb 5 | |
816 | \& $a=3; | |
817 | \& =secret stuff | |
818 | \& warn "Neither POD nor CODE!?" | |
819 | \& =cut back | |
820 | \& print "got $a\en"; | |
821 | .Ve | |
822 | .PP | |
823 | You probably shouldn't rely upon the \f(CW\*(C`warn()\*(C'\fR being podded out forever. | |
824 | Not all pod translators are well-behaved in this regard, and perhaps | |
825 | the compiler will become pickier. | |
826 | .PP | |
827 | One may also use pod directives to quickly comment out a section | |
828 | of code. | |
829 | .Sh "Plain Old Comments (Not!)" | |
830 | .IX Subsection "Plain Old Comments (Not!)" | |
831 | Much like the C preprocessor, Perl can process line directives. Using | |
832 | this, one can control Perl's idea of filenames and line numbers in | |
833 | error or warning messages (especially for strings that are processed | |
834 | with \f(CW\*(C`eval()\*(C'\fR). The syntax for this mechanism is the same as for most | |
835 | C preprocessors: it matches the regular expression | |
836 | \&\f(CW\*(C`/^#\es*line\es+(\ed+)\es*(?:\es"([^"]+)")?\es*$/\*(C'\fR with \f(CW$1\fR being the line | |
837 | number for the next line, and \f(CW$2\fR being the optional filename | |
838 | (specified within quotes). | |
839 | .PP | |
840 | There is a fairly obvious gotcha included with the line directive: | |
841 | Debuggers and profilers will only show the last source line to appear | |
842 | at a particular line number in a given file. Care should be taken not | |
843 | to cause line number collisions in code you'd like to debug later. | |
844 | .PP | |
845 | Here are some examples that you should be able to type into your command | |
846 | shell: | |
847 | .PP | |
848 | .Vb 6 | |
849 | \& % perl | |
850 | \& # line 200 "bzzzt" | |
851 | \& # the `#' on the previous line must be the first char on line | |
852 | \& die 'foo'; | |
853 | \& __END__ | |
854 | \& foo at bzzzt line 201. | |
855 | .Ve | |
856 | .PP | |
857 | .Vb 5 | |
858 | \& % perl | |
859 | \& # line 200 "bzzzt" | |
860 | \& eval qq[\en#line 2001 ""\endie 'foo']; print $@; | |
861 | \& __END__ | |
862 | \& foo at - line 2001. | |
863 | .Ve | |
864 | .PP | |
865 | .Vb 4 | |
866 | \& % perl | |
867 | \& eval qq[\en#line 200 "foo bar"\endie 'foo']; print $@; | |
868 | \& __END__ | |
869 | \& foo at foo bar line 200. | |
870 | .Ve | |
871 | .PP | |
872 | .Vb 6 | |
873 | \& % perl | |
874 | \& # line 345 "goop" | |
875 | \& eval "\en#line " . __LINE__ . ' "' . __FILE__ ."\e"\endie 'foo'"; | |
876 | \& print $@; | |
877 | \& __END__ | |
878 | \& foo at goop line 345. | |
879 | .Ve |