This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
(perl #133706) remove exploit code from Storable
[perl5.git] / pod / perlsyn.pod
CommitLineData
a0d0e21e 1=head1 NAME
d74e8afc 2X<syntax>
a0d0e21e
LW
3
4perlsyn - Perl syntax
5
6=head1 DESCRIPTION
7
6014d0cb 8A Perl program consists of a sequence of declarations and statements
c2f1e229 9which run from the top to the bottom. Loops, subroutines, and other
6014d0cb
MS
10control structures allow you to jump around within the code.
11
c2f1e229
TC
12Perl is a B<free-form> language: you can format and indent it however
13you like. Whitespace serves mostly to separate tokens, unlike
14languages like Python where it is an important part of the syntax,
15or Fortran where it is immaterial.
6014d0cb
MS
16
17Many of Perl's syntactic elements are B<optional>. Rather than
110b9c83 18requiring you to put parentheses around every function call and
6014d0cb
MS
19declare every variable, you can often leave such explicit elements off
20and Perl will figure out what you meant. This is known as B<Do What I
21Mean>, abbreviated B<DWIM>. It allows programmers to be B<lazy> and to
110b9c83 22code in a style with which they are comfortable.
6014d0cb
MS
23
24Perl B<borrows syntax> and concepts from many languages: awk, sed, C,
25Bourne Shell, Smalltalk, Lisp and even English. Other
26languages have borrowed syntax from Perl, particularly its regular
27expression extensions. So if you have programmed in another language
28you will see familiar pieces in Perl. They often work the same, but
29see L<perltrap> for information about how they differ.
a0d0e21e 30
0b8d69e9 31=head2 Declarations
d74e8afc 32X<declaration> X<undef> X<undefined> X<uninitialized>
0b8d69e9 33
cf48932e 34The only things you need to declare in Perl are report formats and
c2f1e229 35subroutines (and sometimes not even subroutines). A scalar variable holds
cf48932e
SF
36the undefined value (C<undef>) until it has been assigned a defined
37value, which is anything other than C<undef>. When used as a number,
38C<undef> is treated as C<0>; when used as a string, it is treated as
39the empty string, C<"">; and when used as a reference that isn't being
40assigned to, it is treated as an error. If you enable warnings,
41you'll be notified of an uninitialized value whenever you treat
42C<undef> as a string or a number. Well, usually. Boolean contexts,
43such as:
7bd1983c 44
7bd1983c
EM
45 if ($a) {}
46
a6b1f6d8
RGS
47are exempt from warnings (because they care about truth rather than
48definedness). Operators such as C<++>, C<-->, C<+=>,
c2f1e229 49C<-=>, and C<.=>, that operate on undefined variables such as:
7bd1983c 50
c2f1e229 51 undef $a;
7bd1983c
EM
52 $a++;
53
54are also always exempt from such warnings.
0b8d69e9 55
a0d0e21e 56A declaration can be put anywhere a statement can, but has no effect on
c2f1e229
TC
57the execution of the primary sequence of statements: declarations all
58take effect at compile time. All declarations are typically put at
54310121 59the beginning or the end of the script. However, if you're using
c2f1e229
TC
60lexically-scoped private variables created with C<my()>,
61C<state()>, or C<our()>, you'll have to make sure
4633a7c4 62your format or subroutine definition is within the same block scope
5f05dabc 63as the my if you expect to be able to access those private variables.
a0d0e21e 64
4633a7c4
LW
65Declaring a subroutine allows a subroutine name to be used as if it were a
66list operator from that point forward in the program. You can declare a
54310121 67subroutine without defining it by saying C<sub name>, thus:
d74e8afc 68X<subroutine, declaration>
a0d0e21e 69
54310121 70 sub myname;
15faabe4 71 $me = myname $0 or die "can't get myname";
a0d0e21e 72
c2f1e229
TC
73A bare declaration like that declares the function to be a list operator,
74not a unary operator, so you have to be careful to use parentheses (or
89a3b501 75C<or> instead of C<||>.) The C<||> operator binds too tightly to use after
c2f1e229
TC
76list operators; it becomes part of the last element. You can always use
77parentheses around the list operators arguments to turn the list operator
78back into something that behaves more like a function call. Alternatively,
79you can use the prototype C<($)> to turn the subroutine into a unary
80operator:
81
82 sub myname ($);
83 $me = myname $0 || die "can't get myname";
84
85That now parses as you'd expect, but you still ought to get in the habit of
86using parentheses in that situation. For more on prototypes, see
368fb018 87L<perlsub>.
a0d0e21e 88
4633a7c4
LW
89Subroutines declarations can also be loaded up with the C<require> statement
90or both loaded and imported into your namespace with a C<use> statement.
91See L<perlmod> for details on this.
a0d0e21e 92
4633a7c4
LW
93A statement sequence may contain declarations of lexically-scoped
94variables, but apart from declaring a variable name, the declaration acts
95like an ordinary statement, and is elaborated within the sequence of
96statements as if it were an ordinary statement. That means it actually
97has both compile-time and run-time effects.
a0d0e21e 98
6014d0cb 99=head2 Comments
d74e8afc 100X<comment> X<#>
6014d0cb
MS
101
102Text from a C<"#"> character until the end of the line is a comment,
103and is ignored. Exceptions include C<"#"> inside a string or regular
104expression.
105
6ec4bd10 106=head2 Simple Statements
d74e8afc 107X<statement> X<semicolon> X<expression> X<;>
a0d0e21e
LW
108
109The only kind of simple statement is an expression evaluated for its
c2f1e229 110side-effects. Every simple statement must be terminated with a
a0d0e21e 111semicolon, unless it is the final statement in a block, in which case
c2f1e229 112the semicolon is optional. But put the semicolon in anyway if the
f386e492 113block takes up more than one line, because you may eventually add
c2f1e229
TC
114another line. Note that there are operators like C<eval {}>, C<sub {}>, and
115C<do {}> that I<look> like compound statements, but aren't--they're just
116TERMs in an expression--and thus need an explicit termination when used
cf48932e
SF
117as the last item in a statement.
118
cf48932e 119=head2 Statement Modifiers
d74e8afc 120X<statement modifier> X<modifier> X<if> X<unless> X<while>
7896dde7 121X<until> X<when> X<foreach> X<for>
a0d0e21e
LW
122
123Any simple statement may optionally be followed by a I<SINGLE> modifier,
124just before the terminating semicolon (or block ending). The possible
125modifiers are:
126
127 if EXPR
128 unless EXPR
129 while EXPR
130 until EXPR
4f8ea571 131 for LIST
cf48932e 132 foreach LIST
7896dde7 133 when EXPR
cf48932e
SF
134
135The C<EXPR> following the modifier is referred to as the "condition".
136Its truth or falsehood determines how the modifier will behave.
137
138C<if> executes the statement once I<if> and only if the condition is
139true. C<unless> is the opposite, it executes the statement I<unless>
c2f1e229 140the condition is true (that is, if the condition is false).
cf48932e
SF
141
142 print "Basset hounds got long ears" if length $ear >= 10;
143 go_outside() and play() unless $is_raining;
144
c2f1e229 145The C<for(each)> modifier is an iterator: it executes the statement once
cf48932e
SF
146for each item in the LIST (with C<$_> aliased to each item in turn).
147
c2f1e229 148 print "Hello $_!\n" for qw(world Dolly nurse);
cf48932e
SF
149
150C<while> repeats the statement I<while> the condition is true.
5e979393
Z
151Postfix C<while> has the same magic treatment of some kinds of condition
152that prefix C<while> has.
cf48932e
SF
153C<until> does the opposite, it repeats the statement I<until> the
154condition is true (or while the condition is false):
155
156 # Both of these count from 0 to 10.
157 print $i++ while $i <= 10;
158 print $j++ until $j > 10;
159
160The C<while> and C<until> modifiers have the usual "C<while> loop"
161semantics (conditional evaluated first), except when applied to a
c2f1e229 162C<do>-BLOCK (or to the Perl4 C<do>-SUBROUTINE statement), in
cf48932e 163which case the block executes once before the conditional is
c2f1e229
TC
164evaluated.
165
166This is so that you can write loops like:
a0d0e21e
LW
167
168 do {
15faabe4
SF
169 $line = <STDIN>;
170 ...
c2f1e229 171 } until !defined($line) || $line eq ".\n"
a0d0e21e 172
5a964f20
TC
173See L<perlfunc/do>. Note also that the loop control statements described
174later will I<NOT> work in this construct, because modifiers don't take
175loop labels. Sorry. You can always put another block inside of it
015aa1a8 176(for C<next>/C<redo>) or around it (for C<last>) to do that sort of thing.
d74e8afc 177X<next> X<last> X<redo>
5a964f20 178
015aa1a8
LM
179For C<next> or C<redo>, just double the braces:
180
5a964f20 181 do {{
15faabe4
SF
182 next if $x == $y;
183 # do something here
5a964f20
TC
184 }} until $x++ > $z;
185
015aa1a8 186For C<last>, you have to be more elaborate and put braces around it:
d74e8afc 187X<last>
5a964f20 188
015aa1a8
LM
189 {
190 do {
191 last if $x == $y**2;
192 # do something here
193 } while $x++ <= $z;
194 }
195
196If you need both C<next> and C<last>, you have to do both and also use a
197loop label:
198
15faabe4 199 LOOP: {
015aa1a8
LM
200 do {{
201 next if $x == $y;
202 last LOOP if $x == $y**2;
203 # do something here
204 }} until $x++ > $z;
5a964f20 205 }
a0d0e21e 206
c2f1e229
TC
207B<NOTE:> The behaviour of a C<my>, C<state>, or
208C<our> modified with a statement modifier conditional
209or loop construct (for example, C<my $x if ...>) is
457b36cb
MV
210B<undefined>. The value of the C<my> variable may be C<undef>, any
211previously assigned value, or possibly anything else. Don't rely on
212it. Future versions of perl might do something different from the
213version of perl you try it out on. Here be dragons.
d74e8afc 214X<my>
457b36cb 215
7896dde7
Z
216The C<when> modifier is an experimental feature that first appeared in Perl
2175.14. To use it, you should include a C<use v5.14> declaration.
218(Technically, it requires only the C<switch> feature, but that aspect of it
219was not available before 5.14.) Operative only from within a C<foreach>
220loop or a C<given> block, it executes the statement only if the smartmatch
221C<< $_ ~~ I<EXPR> >> is true. If the statement executes, it is followed by
222a C<next> from inside a C<foreach> and C<break> from inside a C<given>.
223
224Under the current implementation, the C<foreach> loop can be
225anywhere within the C<when> modifier's dynamic scope, but must be
226within the C<given> block's lexical scope. This restriction may
227be relaxed in a future release. See L</"Switch Statements"> below.
c2f1e229 228
6ec4bd10 229=head2 Compound Statements
d74e8afc 230X<statement, compound> X<block> X<bracket, curly> X<curly bracket> X<brace>
c2f1e229 231X<{> X<}> X<if> X<unless> X<given> X<while> X<until> X<foreach> X<for> X<continue>
a0d0e21e
LW
232
233In Perl, a sequence of statements that defines a scope is called a block.
234Sometimes a block is delimited by the file containing it (in the case
235of a required file, or the program as a whole), and sometimes a block
236is delimited by the extent of a string (in the case of an eval).
237
55771418
Z
238But generally, a block is delimited by curly brackets, also known as
239braces. We will call this syntactic construct a BLOCK. Because enclosing
240braces are also the syntax for hash reference constructor expressions
241(see L<perlref>), you may occasionally need to disambiguate by placing a
242C<;> immediately after an opening brace so that Perl realises the brace
243is the start of a block. You will more frequently need to disambiguate
244the other way, by placing a C<+> immediately before an opening brace to
245force it to be interpreted as a hash reference constructor expression.
246It is considered good style to use these disambiguating mechanisms
247liberally, not only when Perl would otherwise guess incorrectly.
a0d0e21e
LW
248
249The following compound statements may be used to control flow:
250
251 if (EXPR) BLOCK
252 if (EXPR) BLOCK else BLOCK
c2f1e229 253 if (EXPR) BLOCK elsif (EXPR) BLOCK ...
a0d0e21e 254 if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
c2f1e229 255
62d98eed
RU
256 unless (EXPR) BLOCK
257 unless (EXPR) BLOCK else BLOCK
c2f1e229 258 unless (EXPR) BLOCK elsif (EXPR) BLOCK ...
d27f8d4b 259 unless (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
c2f1e229
TC
260
261 given (EXPR) BLOCK
262
a0d0e21e
LW
263 LABEL while (EXPR) BLOCK
264 LABEL while (EXPR) BLOCK continue BLOCK
c2f1e229 265
5ec6d87f
EA
266 LABEL until (EXPR) BLOCK
267 LABEL until (EXPR) BLOCK continue BLOCK
c2f1e229 268
a0d0e21e 269 LABEL for (EXPR; EXPR; EXPR) BLOCK
7808b687
AB
270 LABEL for VAR (LIST) BLOCK
271 LABEL for VAR (LIST) BLOCK continue BLOCK
c2f1e229 272
7808b687 273 LABEL foreach (EXPR; EXPR; EXPR) BLOCK
748a9306 274 LABEL foreach VAR (LIST) BLOCK
b303ae78 275 LABEL foreach VAR (LIST) BLOCK continue BLOCK
c2f1e229
TC
276
277 LABEL BLOCK
a0d0e21e 278 LABEL BLOCK continue BLOCK
c2f1e229 279
43f66a76 280 PHASE BLOCK
a0d0e21e 281
7896dde7
Z
282The experimental C<given> statement is I<not automatically enabled>; see
283L</"Switch Statements"> below for how to do so, and the attendant caveats.
c2f1e229
TC
284
285Unlike in C and Pascal, in Perl these are all defined in terms of BLOCKs,
a0d0e21e
LW
286not statements. This means that the curly brackets are I<required>--no
287dangling statements allowed. If you want to write conditionals without
c2f1e229 288curly brackets, there are several other ways to do it. The following
a0d0e21e
LW
289all do the same thing:
290
c2f1e229 291 if (!open(FOO)) { die "Can't open $FOO: $!" }
a0d0e21e 292 die "Can't open $FOO: $!" unless open(FOO);
c2f1e229
TC
293 open(FOO) || die "Can't open $FOO: $!";
294 open(FOO) ? () : die "Can't open $FOO: $!";
15faabe4 295 # a bit exotic, that last one
a0d0e21e 296
5f05dabc 297The C<if> statement is straightforward. Because BLOCKs are always
a0d0e21e
LW
298bounded by curly brackets, there is never any ambiguity about which
299C<if> an C<else> goes with. If you use C<unless> in place of C<if>,
89a3b501
FC
300the sense of the test is reversed. Like C<if>, C<unless> can be followed
301by C<else>. C<unless> can even be followed by one or more C<elsif>
d27f8d4b
JV
302statements, though you may want to think twice before using that particular
303language construct, as everyone reading your code will have to think at least
304twice before they can understand what's going on.
a0d0e21e
LW
305
306The C<while> statement executes the block as long as the expression is
77fae439 307true.
1d5653dd
RGS
308The C<until> statement executes the block as long as the expression is
309false.
b78218b7
GS
310The LABEL is optional, and if present, consists of an identifier followed
311by a colon. The LABEL identifies the loop for the loop control
312statements C<next>, C<last>, and C<redo>.
313If the LABEL is omitted, the loop control statement
4633a7c4
LW
314refers to the innermost enclosing loop. This may include dynamically
315looking back your call-stack at run time to find the LABEL. Such
9f1b1f2d 316desperate behavior triggers a warning if you use the C<use warnings>
a2293a43 317pragma or the B<-w> flag.
4633a7c4 318
5e979393
Z
319If the condition expression of a C<while> statement is based
320on any of a group of iterative expression types then it gets
321some magic treatment. The affected iterative expression types
322are L<C<readline>|perlfunc/readline EXPR>, the L<C<< <FILEHANDLE>
323>>|perlop/"I/O Operators"> input operator, L<C<readdir>|perlfunc/readdir
324DIRHANDLE>, L<C<glob>|perlfunc/glob EXPR>, the L<C<< <PATTERN>
325>>|perlop/"I/O Operators"> globbing operator, and L<C<each>|perlfunc/each
326HASH>. If the condition expression is one of these expression types, then
327the value yielded by the iterative operator will be implicitly assigned
328to C<$_>. If the condition expression is one of these expression types
329or an explicit assignment of one of them to a scalar, then the condition
330actually tests for definedness of the expression's value, not for its
331regular truth value.
332
4633a7c4 333If there is a C<continue> BLOCK, it is always executed just before the
6ec4bd10
MS
334conditional is about to be evaluated again. Thus it can be used to
335increment a loop variable, even when the loop has been continued via
336the C<next> statement.
4633a7c4 337
43f66a76
DG
338When a block is preceding by a compilation phase keyword such as C<BEGIN>,
339C<END>, C<INIT>, C<CHECK>, or C<UNITCHECK>, then the block will run only
340during the corresponding phase of execution. See L<perlmod> for more details.
341
88e1f1a2 342Extension modules can also hook into the Perl parser to define new
c2f1e229 343kinds of compound statements. These are introduced by a keyword which
6a0969e5 344the extension recognizes, and the syntax following the keyword is
88e1f1a2
JV
345defined entirely by the extension. If you are an implementor, see
346L<perlapi/PL_keyword_plugin> for the mechanism. If you are using such
347a module, see the module's documentation for details of the syntax that
348it defines.
349
4633a7c4 350=head2 Loop Control
d74e8afc 351X<loop control> X<loop, control> X<next> X<last> X<redo> X<continue>
4633a7c4 352
6ec4bd10 353The C<next> command starts the next iteration of the loop:
4633a7c4
LW
354
355 LINE: while (<STDIN>) {
15faabe4
SF
356 next LINE if /^#/; # discard comments
357 ...
4633a7c4
LW
358 }
359
6ec4bd10 360The C<last> command immediately exits the loop in question. The
4633a7c4
LW
361C<continue> block, if any, is not executed:
362
363 LINE: while (<STDIN>) {
15faabe4
SF
364 last LINE if /^$/; # exit when done with header
365 ...
4633a7c4
LW
366 }
367
368The C<redo> command restarts the loop block without evaluating the
369conditional again. The C<continue> block, if any, is I<not> executed.
370This command is normally used by programs that want to lie to themselves
371about what was just input.
372
373For example, when processing a file like F</etc/termcap>.
374If your input lines might end in backslashes to indicate continuation, you
375want to skip ahead and get the next record.
376
377 while (<>) {
15faabe4
SF
378 chomp;
379 if (s/\\$//) {
380 $_ .= <>;
381 redo unless eof();
382 }
383 # now process $_
54310121 384 }
4633a7c4 385
c2f1e229 386which is Perl shorthand for the more explicitly written version:
4633a7c4 387
54310121 388 LINE: while (defined($line = <ARGV>)) {
15faabe4
SF
389 chomp($line);
390 if ($line =~ s/\\$//) {
391 $line .= <ARGV>;
392 redo LINE unless eof(); # not eof(ARGV)!
393 }
394 # now process $line
54310121 395 }
4633a7c4 396
36e7a065
AMS
397Note that if there were a C<continue> block on the above code, it would
398get executed only on lines discarded by the regex (since redo skips the
89a3b501 399continue block). A continue block is often used to reset line counters
499a640d 400or C<m?pat?> one-time matches:
4633a7c4 401
5a964f20
TC
402 # inspired by :1,$g/fred/s//WILMA/
403 while (<>) {
15faabe4
SF
404 m?(fred)? && s//WILMA $1 WILMA/;
405 m?(barney)? && s//BETTY $1 BETTY/;
406 m?(homer)? && s//MARGE $1 MARGE/;
5a964f20 407 } continue {
15faabe4
SF
408 print "$ARGV $.: $_";
409 close ARGV if eof; # reset $.
410 reset if eof; # reset ?pat?
4633a7c4
LW
411 }
412
a0d0e21e
LW
413If the word C<while> is replaced by the word C<until>, the sense of the
414test is reversed, but the conditional is still tested before the first
415iteration.
416
c2f1e229 417Loop control statements don't work in an C<if> or C<unless>, since
5a964f20
TC
418they aren't loops. You can double the braces to make them such, though.
419
420 if (/pattern/) {{
15faabe4
SF
421 last if /fred/;
422 next if /barney/; # same effect as "last",
423 # but doesn't document as well
424 # do something here
5a964f20
TC
425 }}
426
7bd1983c 427This is caused by the fact that a block by itself acts as a loop that
88bd7502 428executes once, see L</"Basic BLOCKs">.
7bd1983c 429
5b23ba8b
MG
430The form C<while/if BLOCK BLOCK>, available in Perl 4, is no longer
431available. Replace any occurrence of C<if BLOCK> by C<if (do BLOCK)>.
4633a7c4 432
cb1a09d0 433=head2 For Loops
d74e8afc 434X<for> X<foreach>
a0d0e21e 435
b78df5de 436Perl's C-style C<for> loop works like the corresponding C<while> loop;
cb1a09d0 437that means that this:
a0d0e21e
LW
438
439 for ($i = 1; $i < 10; $i++) {
15faabe4 440 ...
a0d0e21e
LW
441 }
442
cb1a09d0 443is the same as this:
a0d0e21e
LW
444
445 $i = 1;
446 while ($i < 10) {
15faabe4 447 ...
a0d0e21e 448 } continue {
15faabe4 449 $i++;
a0d0e21e
LW
450 }
451
b78df5de
JA
452There is one minor difference: if variables are declared with C<my>
453in the initialization section of the C<for>, the lexical scope of
454those variables is exactly the C<for> loop (the body of the loop
455and the control sections).
d74e8afc 456X<my>
55497cff 457
62bd8e4f
PM
458As a special case, if the test in the C<for> loop (or the corresponding
459C<while> loop) is empty, it is treated as true. That is, both
460
461 for (;;) {
15faabe4 462 ...
62bd8e4f
PM
463 }
464
465and
466
467 while () {
15faabe4 468 ...
62bd8e4f
PM
469 }
470
471are treated as infinite loops.
472
cb1a09d0
AD
473Besides the normal array index looping, C<for> can lend itself
474to many other interesting applications. Here's one that avoids the
54310121 475problem you get into if you explicitly test for end-of-file on
476an interactive file descriptor causing your program to appear to
cb1a09d0 477hang.
d74e8afc 478X<eof> X<end-of-file> X<end of file>
cb1a09d0
AD
479
480 $on_a_tty = -t STDIN && -t STDOUT;
481 sub prompt { print "yes? " if $on_a_tty }
482 for ( prompt(); <STDIN>; prompt() ) {
15faabe4 483 # do something
54310121 484 }
cb1a09d0 485
5e979393
Z
486The condition expression of a C<for> loop gets the same magic treatment of
487C<readline> et al that the condition expression of a C<while> loop gets.
00cb5da1 488
cb1a09d0 489=head2 Foreach Loops
d74e8afc 490X<for> X<foreach>
cb1a09d0 491
82848c10 492The C<foreach> loop iterates over a normal list value and sets the scalar
55497cff 493variable VAR to be each element of the list in turn. If the variable
494is preceded with the keyword C<my>, then it is lexically scoped, and
495is therefore visible only within the loop. Otherwise, the variable is
496implicitly local to the loop and regains its former value upon exiting
497the loop. If the variable was previously declared with C<my>, it uses
498that variable instead of the global one, but it's still localized to
6a0969e5 499the loop. This implicit localization occurs I<only> in a C<foreach>
5c502d37 500loop.
d74e8afc 501X<my> X<local>
4633a7c4
LW
502
503The C<foreach> keyword is actually a synonym for the C<for> keyword, so
c2f1e229 504you can use either. If VAR is omitted, C<$_> is set to each value.
d74e8afc 505X<$_>
c5674021
PDF
506
507If any element of LIST is an lvalue, you can modify it by modifying
508VAR inside the loop. Conversely, if any element of LIST is NOT an
509lvalue, any attempt to modify that element will fail. In other words,
510the C<foreach> loop index variable is an implicit alias for each item
511in the list that you're looping over.
d74e8afc 512X<alias>
302617ea
MG
513
514If any part of LIST is an array, C<foreach> will get very confused if
515you add or remove elements within the loop body, for example with
516C<splice>. So don't do that.
d74e8afc 517X<splice>
302617ea
MG
518
519C<foreach> probably won't do what you expect if VAR is a tied or other
520special variable. Don't do that either.
4633a7c4 521
82848c10
FC
522As of Perl 5.22, there is an experimental variant of this loop that accepts
523a variable preceded by a backslash for VAR, in which case the items in the
524LIST must be references. The backslashed variable will become an alias
525to each referenced item in the LIST, which must be of the correct type.
526The variable needn't be a scalar in this case, and the backslash may be
baabe3fb 527followed by C<my>. To use this form, you must enable the C<refaliasing>
82848c10
FC
528feature via C<use feature>. (See L<feature>. See also L<perlref/Assigning
529to References>.)
530
748a9306 531Examples:
a0d0e21e 532
4633a7c4 533 for (@ary) { s/foo/bar/ }
a0d0e21e 534
96f2dc66 535 for my $elem (@elements) {
15faabe4 536 $elem *= 2;
a0d0e21e
LW
537 }
538
c2f1e229 539 for $count (reverse(1..10), "BOOM") {
15faabe4
SF
540 print $count, "\n";
541 sleep(1);
a0d0e21e
LW
542 }
543
544 for (1..15) { print "Merry Christmas\n"; }
545
4633a7c4 546 foreach $item (split(/:[\\\n:]*/, $ENV{TERMCAP})) {
15faabe4 547 print "Item: $item\n";
a0d0e21e
LW
548 }
549
baabe3fb
FC
550 use feature "refaliasing";
551 no warnings "experimental::refaliasing";
82848c10 552 foreach \my %hash (@array_of_hash_references) {
15faabe4 553 # do something which each %hash
82848c10
FC
554 }
555
4633a7c4
LW
556Here's how a C programmer might code up a particular algorithm in Perl:
557
55497cff 558 for (my $i = 0; $i < @ary1; $i++) {
15faabe4
SF
559 for (my $j = 0; $j < @ary2; $j++) {
560 if ($ary1[$i] > $ary2[$j]) {
561 last; # can't go to outer :-(
562 }
563 $ary1[$i] += $ary2[$j];
564 }
565 # this is where that last takes me
4633a7c4
LW
566 }
567
184e9718 568Whereas here's how a Perl programmer more comfortable with the idiom might
cb1a09d0 569do it:
4633a7c4 570
96f2dc66
GS
571 OUTER: for my $wid (@ary1) {
572 INNER: for my $jet (@ary2) {
15faabe4
SF
573 next OUTER if $wid > $jet;
574 $wid += $jet;
575 }
576 }
4633a7c4 577
cb1a09d0
AD
578See how much easier this is? It's cleaner, safer, and faster. It's
579cleaner because it's less noisy. It's safer because if code gets added
c07a80fd 580between the inner and outer loops later on, the new code won't be
5f05dabc 581accidentally executed. The C<next> explicitly iterates the other loop
c07a80fd 582rather than merely terminating the inner one. And it's faster because
583Perl executes a C<foreach> statement more rapidly than it would the
0d86a399 584equivalent C-style C<for> loop.
4633a7c4 585
739ba955
RS
586Perceptive Perl hackers may have noticed that a C<for> loop has a return
587value, and that this value can be captured by wrapping the loop in a C<do>
588block. The reward for this discovery is this cautionary advice: The
589return value of a C<for> loop is unspecified and may change without notice.
590Do not rely on it.
591
0d863452
RH
592=head2 Basic BLOCKs
593X<block>
4633a7c4 594
55497cff 595A BLOCK by itself (labeled or not) is semantically equivalent to a
596loop that executes once. Thus you can use any of the loop control
597statements in it to leave or restart the block. (Note that this is
598I<NOT> true in C<eval{}>, C<sub{}>, or contrary to popular belief
599C<do{}> blocks, which do I<NOT> count as loops.) The C<continue>
600block is optional.
4633a7c4 601
27cec4bd 602The BLOCK construct can be used to emulate case structures.
a0d0e21e
LW
603
604 SWITCH: {
15faabe4
SF
605 if (/^abc/) { $abc = 1; last SWITCH; }
606 if (/^def/) { $def = 1; last SWITCH; }
607 if (/^xyz/) { $xyz = 1; last SWITCH; }
608 $nothing = 1;
a0d0e21e
LW
609 }
610
7896dde7
Z
611You'll also find that C<foreach> loop used to create a topicalizer
612and a switch:
c2f1e229
TC
613
614 SWITCH:
615 for ($var) {
15faabe4
SF
616 if (/^abc/) { $abc = 1; last SWITCH; }
617 if (/^def/) { $def = 1; last SWITCH; }
618 if (/^xyz/) { $xyz = 1; last SWITCH; }
619 $nothing = 1;
c2f1e229
TC
620 }
621
622Such constructs are quite frequently used, both because older versions of
623Perl had no official C<switch> statement, and also because the new version
7896dde7 624described immediately below remains experimental and can sometimes be confusing.
83df6a1d 625
c2f1e229 626=head2 Switch Statements
fd4f5766 627
7896dde7 628X<switch> X<case> X<given> X<when> X<default>
83df6a1d 629
7896dde7
Z
630Starting from Perl 5.10.1 (well, 5.10.0, but it didn't work
631right), you can say
83df6a1d 632
27cec4bd 633 use feature "switch";
a0d0e21e 634
7896dde7
Z
635to enable an experimental switch feature. This is loosely based on an
636old version of a Perl 6 proposal, but it no longer resembles the Perl 6
637construct. You also get the switch feature whenever you declare that your
c2f1e229
TC
638code prefers to run under a version of Perl that is 5.10 or later. For
639example:
640
641 use v5.14;
642
7896dde7
Z
643Under the "switch" feature, Perl gains the experimental keywords
644C<given>, C<when>, C<default>, C<continue>, and C<break>.
645Starting from Perl 5.16, one can prefix the switch
646keywords with C<CORE::> to access the feature without a C<use feature>
647statement. The keywords C<given> and
648C<when> are analogous to C<switch> and
649C<case> in other languages -- though C<continue> is not -- so the code
650in the previous section could be rewritten as
c2f1e229
TC
651
652 use v5.10.1;
7896dde7
Z
653 for ($var) {
654 when (/^abc/) { $abc = 1 }
655 when (/^def/) { $def = 1 }
656 when (/^xyz/) { $xyz = 1 }
657 default { $nothing = 1 }
c2f1e229
TC
658 }
659
7896dde7
Z
660The C<foreach> is the non-experimental way to set a topicalizer.
661If you wish to use the highly experimental C<given>, that could be
662written like this:
0d863452 663
7896dde7 664 use v5.10.1;
c2f1e229 665 given ($var) {
7896dde7
Z
666 when (/^abc/) { $abc = 1 }
667 when (/^def/) { $def = 1 }
668 when (/^xyz/) { $xyz = 1 }
669 default { $nothing = 1 }
c2f1e229
TC
670 }
671
7896dde7 672As of 5.14, that can also be written this way:
0d863452 673
7896dde7
Z
674 use v5.14;
675 for ($var) {
676 $abc = 1 when /^abc/;
677 $def = 1 when /^def/;
678 $xyz = 1 when /^xyz/;
679 default { $nothing = 1 }
a0d0e21e
LW
680 }
681
7896dde7 682Or if you don't care to play it safe, like this:
c2f1e229 683
7896dde7
Z
684 use v5.14;
685 given ($var) {
686 $abc = 1 when /^abc/;
687 $def = 1 when /^def/;
688 $xyz = 1 when /^xyz/;
689 default { $nothing = 1 }
690 }
64805db9 691
7896dde7
Z
692The arguments to C<given> and C<when> are in scalar context,
693and C<given> assigns the C<$_> variable its topic value.
694
695Exactly what the I<EXPR> argument to C<when> does is hard to describe
696precisely, but in general, it tries to guess what you want done. Sometimes
697it is interpreted as C<< $_ ~~ I<EXPR> >>, and sometimes it is not. It
698also behaves differently when lexically enclosed by a C<given> block than
699it does when dynamically enclosed by a C<foreach> loop. The rules are far
700too difficult to understand to be described here. See L</"Experimental Details
701on given and when"> later on.
702
703Due to an unfortunate bug in how C<given> was implemented between Perl 5.10
704and 5.16, under those implementations the version of C<$_> governed by
705C<given> is merely a lexically scoped copy of the original, not a
706dynamically scoped alias to the original, as it would be if it were a
707C<foreach> or under both the original and the current Perl 6 language
708specification. This bug was fixed in Perl 5.18 (and lexicalized C<$_> itself
709was removed in Perl 5.24).
710
711If your code still needs to run on older versions,
712stick to C<foreach> for your topicalizer and
713you will be less unhappy.
c2f1e229
TC
714
715=head2 Goto
716X<goto>
717
718Although not for the faint of heart, Perl does support a C<goto>
719statement. There are three forms: C<goto>-LABEL, C<goto>-EXPR, and
720C<goto>-&NAME. A loop's LABEL is not actually a valid target for
721a C<goto>; it's just the name of the loop.
722
723The C<goto>-LABEL form finds the statement labeled with LABEL and resumes
724execution there. It may not be used to go into any construct that
725requires initialization, such as a subroutine or a C<foreach> loop. It
726also can't be used to go into a construct that is optimized away. It
727can be used to go almost anywhere else within the dynamic scope,
728including out of subroutines, but it's usually better to use some other
729construct such as C<last> or C<die>. The author of Perl has never felt the
730need to use this form of C<goto> (in Perl, that is--C is another matter).
731
732The C<goto>-EXPR form expects a label name, whose scope will be resolved
733dynamically. This allows for computed C<goto>s per FORTRAN, but isn't
734necessarily recommended if you're optimizing for maintainability:
735
736 goto(("FOO", "BAR", "GLARCH")[$i]);
737
738The C<goto>-&NAME form is highly magical, and substitutes a call to the
739named subroutine for the currently running subroutine. This is used by
740C<AUTOLOAD()> subroutines that wish to load another subroutine and then
741pretend that the other subroutine had been called in the first place
742(except that any modifications to C<@_> in the current subroutine are
743propagated to the other subroutine.) After the C<goto>, not even C<caller()>
744will be able to tell that this routine was called first.
745
746In almost all cases like this, it's usually a far, far better idea to use the
747structured control flow mechanisms of C<next>, C<last>, or C<redo> instead of
748resorting to a C<goto>. For certain applications, the catch and throw pair of
749C<eval{}> and die() for exception processing can also be a prudent approach.
750
751=head2 The Ellipsis Statement
752X<...>
753X<... statement>
754X<ellipsis operator>
755X<elliptical statement>
756X<unimplemented statement>
757X<unimplemented operator>
758X<yada-yada>
81104cdf
FC
759X<yada-yada operator>
760X<... operator>
761X<whatever operator>
762X<triple-dot operator>
c2f1e229
TC
763
764Beginning in Perl 5.12, Perl accepts an ellipsis, "C<...>", as a
29d69c3c 765placeholder for code that you haven't implemented yet.
b735b77b 766When Perl 5.12 or later encounters an ellipsis statement, it parses this
c2f1e229
TC
767without error, but if and when you should actually try to execute it, Perl
768throws an exception with the text C<Unimplemented>:
769
9dba9ce0 770 use v5.12;
c2f1e229
TC
771 sub unimplemented { ... }
772 eval { unimplemented() };
9dba9ce0 773 if ($@ =~ /^Unimplemented at /) {
15faabe4 774 say "I found an ellipsis!";
c2f1e229
TC
775 }
776
29d69c3c
Z
777You can only use the elliptical statement to stand in for a complete
778statement. Syntactically, "C<...;>" is a complete statement, but,
779as with other kinds of semicolon-terminated statement, the semicolon
780may be omitted if "C<...>" appears immediately before a closing brace.
781These examples show how the ellipsis works:
c2f1e229
TC
782
783 use v5.12;
784 { ... }
785 sub foo { ... }
786 ...;
787 eval { ... };
788 sub somemeth {
15faabe4
SF
789 my $self = shift;
790 ...;
c2f1e229
TC
791 }
792 $x = do {
15faabe4
SF
793 my $n;
794 ...;
795 say "Hurrah!";
796 $n;
c2f1e229
TC
797 };
798
799The elliptical statement cannot stand in for an expression that
29d69c3c 800is part of a larger statement.
c2f1e229
TC
801These examples of attempts to use an ellipsis are syntax errors:
802
803 use v5.12;
804
805 print ...;
806 open(my $fh, ">", "/dev/passwd") or ...;
807 if ($condition && ... ) { say "Howdy" };
29d69c3c
Z
808 ... if $a > $b;
809 say "Cromulent" if ...;
810 $flub = 5 + ...;
c2f1e229
TC
811
812There are some cases where Perl can't immediately tell the difference
813between an expression and a statement. For instance, the syntax for a
814block and an anonymous hash reference constructor look the same unless
815there's something in the braces to give Perl a hint. The ellipsis is a
29d69c3c 816syntax error if Perl doesn't guess that the C<{ ... }> is a block.
12d22d1f 817Inside your block, you can use a C<;> before the ellipsis to denote that the
29d69c3c 818C<{ ... }> is a block and not a hash reference constructor.
c2f1e229
TC
819
820Note: Some folks colloquially refer to this bit of punctuation as a
81104cdf 821"yada-yada" or "triple-dot", but its true name
84539229 822is actually an ellipsis.
c2f1e229
TC
823
824=head2 PODs: Embedded Documentation
825X<POD> X<documentation>
826
827Perl has a mechanism for intermixing documentation with source code.
828While it's expecting the beginning of a new statement, if the compiler
829encounters a line that begins with an equal sign and a word, like this
830
831 =head1 Here There Be Pods!
832
833Then that text and all remaining text up through and including a line
834beginning with C<=cut> will be ignored. The format of the intervening
835text is described in L<perlpod>.
836
837This allows you to intermix your source code
838and your documentation text freely, as in
839
840 =item snazzle($)
841
842 The snazzle() function will behave in the most spectacular
843 form that you can possibly imagine, not even excepting
844 cybernetic pyrotechnics.
845
846 =cut back to the compiler, nuff of this pod stuff!
847
848 sub snazzle($) {
15faabe4
SF
849 my $thingie = shift;
850 .........
c2f1e229
TC
851 }
852
853Note that pod translators should look at only paragraphs beginning
854with a pod directive (it makes parsing easier), whereas the compiler
855actually knows to look for pod escapes even in the middle of a
856paragraph. This means that the following secret stuff will be
857ignored by both the compiler and the translators.
858
859 $a=3;
860 =secret stuff
861 warn "Neither POD nor CODE!?"
862 =cut back
863 print "got $a\n";
864
865You probably shouldn't rely upon the C<warn()> being podded out forever.
866Not all pod translators are well-behaved in this regard, and perhaps
867the compiler will become pickier.
868
869One may also use pod directives to quickly comment out a section
870of code.
871
872=head2 Plain Old Comments (Not!)
873X<comment> X<line> X<#> X<preprocessor> X<eval>
874
875Perl can process line directives, much like the C preprocessor. Using
876this, one can control Perl's idea of filenames and line numbers in
877error or warning messages (especially for strings that are processed
878with C<eval()>). The syntax for this mechanism is almost the same as for
879most C preprocessors: it matches the regular expression
880
881 # example: '# line 42 "new_filename.plx"'
882 /^\# \s*
883 line \s+ (\d+) \s*
884 (?:\s("?)([^"]+)\g2)? \s*
885 $/x
886
887with C<$1> being the line number for the next line, and C<$3> being
89a3b501 888the optional filename (specified with or without quotes). Note that
c2f1e229
TC
889no whitespace may precede the C<< # >>, unlike modern C preprocessors.
890
891There is a fairly obvious gotcha included with the line directive:
892Debuggers and profilers will only show the last source line to appear
893at a particular line number in a given file. Care should be taken not
894to cause line number collisions in code you'd like to debug later.
895
896Here are some examples that you should be able to type into your command
897shell:
898
899 % perl
900 # line 200 "bzzzt"
901 # the '#' on the previous line must be the first char on line
902 die 'foo';
903 __END__
904 foo at bzzzt line 201.
905
906 % perl
907 # line 200 "bzzzt"
908 eval qq[\n#line 2001 ""\ndie 'foo']; print $@;
909 __END__
910 foo at - line 2001.
911
912 % perl
913 eval qq[\n#line 200 "foo bar"\ndie 'foo']; print $@;
914 __END__
915 foo at foo bar line 200.
916
917 % perl
918 # line 345 "goop"
919 eval "\n#line " . __LINE__ . ' "' . __FILE__ ."\"\ndie 'foo'";
920 print $@;
921 __END__
922 foo at goop line 345.
923
7896dde7
Z
924=head2 Experimental Details on given and when
925
926As previously mentioned, the "switch" feature is considered highly
927experimental; it is subject to change with little notice. In particular,
928C<when> has tricky behaviours that are expected to change to become less
929tricky in the future. Do not rely upon its current (mis)implementation.
930Before Perl 5.18, C<given> also had tricky behaviours that you should still
931beware of if your code must run on older versions of Perl.
932
933Here is a longer example of C<given>:
934
935 use feature ":5.10";
936 given ($foo) {
937 when (undef) {
938 say '$foo is undefined';
939 }
940 when ("foo") {
941 say '$foo is the string "foo"';
942 }
943 when ([1,3,5,7,9]) {
944 say '$foo is an odd digit';
945 continue; # Fall through
946 }
947 when ($_ < 100) {
948 say '$foo is numerically less than 100';
949 }
950 when (\&complicated_check) {
951 say 'a complicated check for $foo is true';
952 }
953 default {
954 die q(I don't know what to do with $foo);
955 }
956 }
957
958Before Perl 5.18, C<given(EXPR)> assigned the value of I<EXPR> to
959merely a lexically scoped I<B<copy>> (!) of C<$_>, not a dynamically
960scoped alias the way C<foreach> does. That made it similar to
961
962 do { my $_ = EXPR; ... }
963
964except that the block was automatically broken out of by a successful
965C<when> or an explicit C<break>. Because it was only a copy, and because
966it was only lexically scoped, not dynamically scoped, you could not do the
967things with it that you are used to in a C<foreach> loop. In particular,
968it did not work for arbitrary function calls if those functions might try
969to access $_. Best stick to C<foreach> for that.
970
971Most of the power comes from the implicit smartmatching that can
972sometimes apply. Most of the time, C<when(EXPR)> is treated as an
973implicit smartmatch of C<$_>, that is, C<$_ ~~ EXPR>. (See
974L<perlop/"Smartmatch Operator"> for more information on smartmatching.)
975But when I<EXPR> is one of the 10 exceptional cases (or things like them)
976listed below, it is used directly as a boolean.
977
978=over 4
979
980=item Z<>1.
981
982A user-defined subroutine call or a method invocation.
983
984=item Z<>2.
985
986A regular expression match in the form of C</REGEX/>, C<$foo =~ /REGEX/>,
987or C<$foo =~ EXPR>. Also, a negated regular expression match in
988the form C<!/REGEX/>, C<$foo !~ /REGEX/>, or C<$foo !~ EXPR>.
989
990=item Z<>3.
991
992A smart match that uses an explicit C<~~> operator, such as C<EXPR ~~ EXPR>.
993
994B<NOTE:> You will often have to use C<$c ~~ $_> because the default case
dabde021 995uses C<$_ ~~ $c> , which is frequently the opposite of what you want.
7896dde7
Z
996
997=item Z<>4.
998
999A boolean comparison operator such as C<$_ E<lt> 10> or C<$x eq "abc">. The
1000relational operators that this applies to are the six numeric comparisons
1001(C<< < >>, C<< > >>, C<< <= >>, C<< >= >>, C<< == >>, and C<< != >>), and
1002the six string comparisons (C<lt>, C<gt>, C<le>, C<ge>, C<eq>, and C<ne>).
1003
1004=item Z<>5.
1005
1006At least the three builtin functions C<defined(...)>, C<exists(...)>, and
1007C<eof(...)>. We might someday add more of these later if we think of them.
1008
1009=item Z<>6.
1010
1011A negated expression, whether C<!(EXPR)> or C<not(EXPR)>, or a logical
1012exclusive-or, C<(EXPR1) xor (EXPR2)>. The bitwise versions (C<~> and C<^>)
1013are not included.
1014
1015=item Z<>7.
1016
1017A filetest operator, with exactly 4 exceptions: C<-s>, C<-M>, C<-A>, and
1018C<-C>, as these return numerical values, not boolean ones. The C<-z>
1019filetest operator is not included in the exception list.
1020
1021=item Z<>8.
1022
1023The C<..> and C<...> flip-flop operators. Note that the C<...> flip-flop
1024operator is completely different from the C<...> elliptical statement
1025just described.
1026
1027=back
1028
1029In those 8 cases above, the value of EXPR is used directly as a boolean, so
1030no smartmatching is done. You may think of C<when> as a smartsmartmatch.
1031
1032Furthermore, Perl inspects the operands of logical operators to
1033decide whether to use smartmatching for each one by applying the
1034above test to the operands:
1035
1036=over 4
1037
1038=item Z<>9.
1039
1040If EXPR is C<EXPR1 && EXPR2> or C<EXPR1 and EXPR2>, the test is applied
1041I<recursively> to both EXPR1 and EXPR2.
1042Only if I<both> operands also pass the
1043test, I<recursively>, will the expression be treated as boolean. Otherwise,
1044smartmatching is used.
1045
1046=item Z<>10.
1047
1048If EXPR is C<EXPR1 || EXPR2>, C<EXPR1 // EXPR2>, or C<EXPR1 or EXPR2>, the
1049test is applied I<recursively> to EXPR1 only (which might itself be a
1050higher-precedence AND operator, for example, and thus subject to the
1051previous rule), not to EXPR2. If EXPR1 is to use smartmatching, then EXPR2
1052also does so, no matter what EXPR2 contains. But if EXPR2 does not get to
1053use smartmatching, then the second argument will not be either. This is
1054quite different from the C<&&> case just described, so be careful.
1055
1056=back
1057
1058These rules are complicated, but the goal is for them to do what you want
1059(even if you don't quite understand why they are doing it). For example:
1060
1061 when (/^\d+$/ && $_ < 75) { ... }
1062
1063will be treated as a boolean match because the rules say both
1064a regex match and an explicit test on C<$_> will be treated
1065as boolean.
1066
1067Also:
1068
1069 when ([qw(foo bar)] && /baz/) { ... }
1070
1071will use smartmatching because only I<one> of the operands is a boolean:
1072the other uses smartmatching, and that wins.
1073
1074Further:
1075
1076 when ([qw(foo bar)] || /^baz/) { ... }
1077
1078will use smart matching (only the first operand is considered), whereas
1079
1080 when (/^baz/ || [qw(foo bar)]) { ... }
1081
1082will test only the regex, which causes both operands to be
1083treated as boolean. Watch out for this one, then, because an
1084arrayref is always a true value, which makes it effectively
1085redundant. Not a good idea.
1086
1087Tautologous boolean operators are still going to be optimized
1088away. Don't be tempted to write
1089
1090 when ("foo" or "bar") { ... }
1091
1092This will optimize down to C<"foo">, so C<"bar"> will never be considered (even
1093though the rules say to use a smartmatch
1094on C<"foo">). For an alternation like
1095this, an array ref will work, because this will instigate smartmatching:
1096
1097 when ([qw(foo bar)] { ... }
1098
1099This is somewhat equivalent to the C-style switch statement's fallthrough
1100functionality (not to be confused with I<Perl's> fallthrough
1101functionality--see below), wherein the same block is used for several
1102C<case> statements.
1103
1104Another useful shortcut is that, if you use a literal array or hash as the
1105argument to C<given>, it is turned into a reference. So C<given(@foo)> is
1106the same as C<given(\@foo)>, for example.
1107
1108C<default> behaves exactly like C<when(1 == 1)>, which is
1109to say that it always matches.
1110
1111=head3 Breaking out
1112
1113You can use the C<break> keyword to break out of the enclosing
1114C<given> block. Every C<when> block is implicitly ended with
1115a C<break>.
1116
1117=head3 Fall-through
1118
1119You can use the C<continue> keyword to fall through from one
1120case to the next immediate C<when> or C<default>:
1121
1122 given($foo) {
1123 when (/x/) { say '$foo contains an x'; continue }
1124 when (/y/) { say '$foo contains a y' }
1125 default { say '$foo does not contain a y' }
1126 }
1127
1128=head3 Return value
1129
1130When a C<given> statement is also a valid expression (for example,
1131when it's the last statement of a block), it evaluates to:
1132
1133=over 4
1134
1135=item *
1136
1137An empty list as soon as an explicit C<break> is encountered.
1138
1139=item *
1140
1141The value of the last evaluated expression of the successful
1142C<when>/C<default> clause, if there happens to be one.
1143
1144=item *
1145
1146The value of the last evaluated expression of the C<given> block if no
1147condition is true.
1148
1149=back
1150
1151In both last cases, the last expression is evaluated in the context that
1152was applied to the C<given> block.
1153
1154Note that, unlike C<if> and C<unless>, failed C<when> statements always
1155evaluate to an empty list.
1156
1157 my $price = do {
1158 given ($item) {
1159 when (["pear", "apple"]) { 1 }
1160 break when "vote"; # My vote cannot be bought
1161 1e10 when /Mona Lisa/;
1162 "unknown";
1163 }
1164 };
1165
1166Currently, C<given> blocks can't always
1167be used as proper expressions. This
1168may be addressed in a future version of Perl.
1169
1170=head3 Switching in a loop
1171
1172Instead of using C<given()>, you can use a C<foreach()> loop.
1173For example, here's one way to count how many times a particular
1174string occurs in an array:
1175
1176 use v5.10.1;
1177 my $count = 0;
1178 for (@array) {
1179 when ("foo") { ++$count }
1180 }
1181 print "\@array contains $count copies of 'foo'\n";
1182
1183Or in a more recent version:
1184
1185 use v5.14;
1186 my $count = 0;
1187 for (@array) {
1188 ++$count when "foo";
1189 }
1190 print "\@array contains $count copies of 'foo'\n";
1191
1192At the end of all C<when> blocks, there is an implicit C<next>.
1193You can override that with an explicit C<last> if you're
1194interested in only the first match alone.
1195
1196This doesn't work if you explicitly specify a loop variable, as
1197in C<for $item (@array)>. You have to use the default variable C<$_>.
1198
1199=head3 Differences from Perl 6
1200
1201The Perl 5 smartmatch and C<given>/C<when> constructs are not compatible
1202with their Perl 6 analogues. The most visible difference and least
1203important difference is that, in Perl 5, parentheses are required around
1204the argument to C<given()> and C<when()> (except when this last one is used
1205as a statement modifier). Parentheses in Perl 6 are always optional in a
1206control construct such as C<if()>, C<while()>, or C<when()>; they can't be
1207made optional in Perl 5 without a great deal of potential confusion,
1208because Perl 5 would parse the expression
1209
1210 given $foo {
1211 ...
1212 }
1213
1214as though the argument to C<given> were an element of the hash
1215C<%foo>, interpreting the braces as hash-element syntax.
1216
1217However, their are many, many other differences. For example,
1218this works in Perl 5:
1219
1220 use v5.12;
1221 my @primary = ("red", "blue", "green");
1222
1223 if (@primary ~~ "red") {
1224 say "primary smartmatches red";
1225 }
1226
1227 if ("red" ~~ @primary) {
1228 say "red smartmatches primary";
1229 }
1230
1231 say "that's all, folks!";
1232
1233But it doesn't work at all in Perl 6. Instead, you should
1234use the (parallelizable) C<any> operator:
1235
1236 if any(@primary) eq "red" {
1237 say "primary smartmatches red";
1238 }
1239
1240 if "red" eq any(@primary) {
1241 say "red smartmatches primary";
1242 }
1243
1244The table of smartmatches in L<perlop/"Smartmatch Operator"> is not
1245identical to that proposed by the Perl 6 specification, mainly due to
1246differences between Perl 6's and Perl 5's data models, but also because
1247the Perl 6 spec has changed since Perl 5 rushed into early adoption.
1248
1249In Perl 6, C<when()> will always do an implicit smartmatch with its
1250argument, while in Perl 5 it is convenient (albeit potentially confusing) to
1251suppress this implicit smartmatch in various rather loosely-defined
1252situations, as roughly outlined above. (The difference is largely because
1253Perl 5 does not have, even internally, a boolean type.)
1254
774d564b 1255=cut