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