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 mostly serves to separate tokens, unlike languages
15 like Python where it is an important part of the syntax.
16
17 Many of Perl's syntactic elements are optional. Rather than requiring
18 you to put parentheses around every function call and declare every
19 variable, you can often leave such explicit elements off and Perl will
20 figure out what you meant. This is known as Do What I Mean,
21 abbreviated DWIM. It allows programmers to be lazy and to code in a
22 style with which they are comfortable.
23
24 Perl borrows syntax and concepts from many languages: awk, sed, C,
25 Bourne Shell, Smalltalk, Lisp and even English. Other languages have
26 borrowed syntax from Perl, particularly its regular expression
27 extensions. So if you have programmed in another language you will see
28 familiar pieces in Perl. They often work the same, but see perltrap
29 for information about how they differ.
30
31 Declarations
32 The only things you need to declare in Perl are report formats and
33 subroutines (and sometimes not even subroutines). A variable holds the
34 undefined value ("undef") until it has been assigned a defined value,
35 which is anything other than "undef". When used as a number, "undef"
36 is treated as 0; when used as a string, it is treated as the empty
37 string, ""; and when used as a reference that isn't being assigned to,
38 it is treated as an error. If you enable warnings, you'll be notified
39 of an uninitialized value whenever you treat "undef" as a string or a
40 number. Well, usually. Boolean contexts, such as:
41
42 my $a;
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 left values such as:
48
49 my $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. Typically all the declarations are put at
57 the beginning or the end of the script. However, if you're using
58 lexically-scoped private variables created with "my()", you'll have to
59 make sure your format or subroutine definition is within the same block
60 scope as the my if you expect to be able to access those private
61 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 Note that myname() functions as a list operator, not as a unary
71 operator; so be careful to use "or" instead of "||" in this case.
72 However, if you were to declare the subroutine as "sub myname ($)",
73 then "myname" would function as a unary operator, so either "or" or
74 "||" would work.
75
76 Subroutines declarations can also be loaded up with the "require"
77 statement or both loaded and imported into your namespace with a "use"
78 statement. See perlmod for details on this.
79
80 A statement sequence may contain declarations of lexically-scoped
81 variables, but apart from declaring a variable name, the declaration
82 acts like an ordinary statement, and is elaborated within the sequence
83 of statements as if it were an ordinary statement. That means it
84 actually has both compile-time and run-time effects.
85
86 Comments
87 Text from a "#" character until the end of the line is a comment, and
88 is ignored. Exceptions include "#" inside a string or regular
89 expression.
90
91 Simple Statements
92 The only kind of simple statement is an expression evaluated for its
93 side effects. Every simple statement must be terminated with a
94 semicolon, unless it is the final statement in a block, in which case
95 the semicolon is optional. (A semicolon is still encouraged if the
96 block takes up more than one line, because you may eventually add
97 another line.) Note that there are some operators like "eval {}" and
98 "do {}" that look like compound statements, but aren't (they're just
99 TERMs in an expression), and thus need an explicit termination if used
100 as the last item in a statement.
101
102 Truth and Falsehood
103 The number 0, the strings '0' and '', the empty list "()", and "undef"
104 are all false in a boolean context. All other values are true.
105 Negation of a true value by "!" or "not" returns a special false value.
106 When evaluated as a string it is treated as '', but as a number, it is
107 treated as 0.
108
109 Statement Modifiers
110 Any simple statement may optionally be followed by a SINGLE modifier,
111 just before the terminating semicolon (or block ending). The possible
112 modifiers are:
113
114 if EXPR
115 unless EXPR
116 while EXPR
117 until EXPR
118 when EXPR
119 for LIST
120 foreach LIST
121
122 The "EXPR" following the modifier is referred to as the "condition".
123 Its truth or falsehood determines how the modifier will behave.
124
125 "if" executes the statement once if and only if the condition is true.
126 "unless" is the opposite, it executes the statement unless the
127 condition is true (i.e., if the condition is false).
128
129 print "Basset hounds got long ears" if length $ear >= 10;
130 go_outside() and play() unless $is_raining;
131
132 "when" executes the statement when $_ smart matches "EXPR", and then
133 either "break"s out if it's enclosed in a "given" scope or skips to the
134 "next" element when it lies directly inside a "for" loop. See also
135 "Switch statements".
136
137 given ($something) {
138 $abc = 1 when /^abc/;
139 $just_a = 1 when /^a/;
140 $other = 1;
141 }
142
143 for (@names) {
144 admin($_) when [ qw/Alice Bob/ ];
145 regular($_) when [ qw/Chris David Ellen/ ];
146 }
147
148 The "foreach" modifier is an iterator: it executes the statement once
149 for each item in the LIST (with $_ aliased to each item in turn).
150
151 print "Hello $_!\n" foreach qw(world Dolly nurse);
152
153 "while" repeats the statement while the condition is true. "until"
154 does the opposite, it repeats the statement until the condition is true
155 (or while the condition is false):
156
157 # Both of these count from 0 to 10.
158 print $i++ while $i <= 10;
159 print $j++ until $j > 10;
160
161 The "while" and "until" modifiers have the usual ""while" loop"
162 semantics (conditional evaluated first), except when applied to a
163 "do"-BLOCK (or to the deprecated "do"-SUBROUTINE statement), in which
164 case the block executes once before the conditional is evaluated. This
165 is so that you can write loops like:
166
167 do {
168 $line = <STDIN>;
169 ...
170 } until $line eq ".\n";
171
172 See "do" in perlfunc. Note also that the loop control statements
173 described later will NOT work in this construct, because modifiers
174 don't take loop labels. Sorry. You can always put another block
175 inside of it (for "next") or around it (for "last") to do that sort of
176 thing. For "next", just double the braces:
177
178 do {{
179 next if $x == $y;
180 # do something here
181 }} until $x++ > $z;
182
183 For "last", you have to be more elaborate:
184
185 LOOP: {
186 do {
187 last if $x = $y**2;
188 # do something here
189 } while $x++ <= $z;
190 }
191
192 NOTE: The behaviour of a "my" statement modified with a statement
193 modifier conditional or loop construct (e.g. "my $x if ...") is
194 undefined. The value of the "my" variable may be "undef", any
195 previously assigned value, or possibly anything else. Don't rely on
196 it. Future versions of perl might do something different from the
197 version of perl you try it out on. Here be dragons.
198
199 Compound Statements
200 In Perl, a sequence of statements that defines a scope is called a
201 block. Sometimes a block is delimited by the file containing it (in
202 the case of a required file, or the program as a whole), and sometimes
203 a block is delimited by the extent of a string (in the case of an
204 eval).
205
206 But generally, a block is delimited by curly brackets, also known as
207 braces. We will call this syntactic construct a BLOCK.
208
209 The following compound statements may be used to control flow:
210
211 if (EXPR) BLOCK
212 if (EXPR) BLOCK else BLOCK
213 if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
214 unless (EXPR) BLOCK
215 unless (EXPR) BLOCK else BLOCK
216 unless (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
217 LABEL while (EXPR) BLOCK
218 LABEL while (EXPR) BLOCK continue BLOCK
219 LABEL until (EXPR) BLOCK
220 LABEL until (EXPR) BLOCK continue BLOCK
221 LABEL for (EXPR; EXPR; EXPR) BLOCK
222 LABEL foreach VAR (LIST) BLOCK
223 LABEL foreach VAR (LIST) BLOCK continue BLOCK
224 LABEL BLOCK continue BLOCK
225
226 Note that, unlike C and Pascal, these are defined in terms of BLOCKs,
227 not statements. This means that the curly brackets are required--no
228 dangling statements allowed. If you want to write conditionals without
229 curly brackets there are several other ways to do it. The following
230 all do the same thing:
231
232 if (!open(FOO)) { die "Can't open $FOO: $!"; }
233 die "Can't open $FOO: $!" unless open(FOO);
234 open(FOO) or die "Can't open $FOO: $!"; # FOO or bust!
235 open(FOO) ? 'hi mom' : die "Can't open $FOO: $!";
236 # a bit exotic, that last one
237
238 The "if" statement is straightforward. Because BLOCKs are always
239 bounded by curly brackets, there is never any ambiguity about which
240 "if" an "else" goes with. If you use "unless" in place of "if", the
241 sense of the test is reversed. Like "if", "unless" can be followed by
242 "else". "unless" can even be followed by one or more "elsif"
243 statements, though you may want to think twice before using that
244 particular language construct, as everyone reading your code will have
245 to think at least twice before they can understand what's going on.
246
247 The "while" statement executes the block as long as the expression is
248 true. The "until" statement executes the block as long as the
249 expression is false. The LABEL is optional, and if present, consists
250 of an identifier followed by a colon. The LABEL identifies the loop
251 for the loop control statements "next", "last", and "redo". If the
252 LABEL is omitted, the loop control statement refers to the innermost
253 enclosing loop. This may include dynamically looking back your call-
254 stack at run time to find the LABEL. Such desperate behavior triggers
255 a warning if you use the "use warnings" pragma or the -w flag.
256
257 If there is a "continue" BLOCK, it is always executed just before the
258 conditional is about to be evaluated again. Thus it can be used to
259 increment a loop variable, even when the loop has been continued via
260 the "next" statement.
261
262 Extension modules can also hook into the Perl parser to define new
263 kinds of compound statement. These are introduced by a keyword which
264 the extension recognises, and the syntax following the keyword is
265 defined entirely by the extension. If you are an implementor, see
266 "PL_keyword_plugin" in perlapi for the mechanism. If you are using
267 such a module, see the module's documentation for details of the syntax
268 that it defines.
269
270 Loop Control
271 The "next" command starts the next iteration of the loop:
272
273 LINE: while (<STDIN>) {
274 next LINE if /^#/; # discard comments
275 ...
276 }
277
278 The "last" command immediately exits the loop in question. The
279 "continue" block, if any, is not executed:
280
281 LINE: while (<STDIN>) {
282 last LINE if /^$/; # exit when done with header
283 ...
284 }
285
286 The "redo" command restarts the loop block without evaluating the
287 conditional again. The "continue" block, if any, is not executed.
288 This command is normally used by programs that want to lie to
289 themselves about what was just input.
290
291 For example, when processing a file like /etc/termcap. If your input
292 lines might end in backslashes to indicate continuation, you want to
293 skip ahead and get the next record.
294
295 while (<>) {
296 chomp;
297 if (s/\\$//) {
298 $_ .= <>;
299 redo unless eof();
300 }
301 # now process $_
302 }
303
304 which is Perl short-hand for the more explicitly written version:
305
306 LINE: while (defined($line = <ARGV>)) {
307 chomp($line);
308 if ($line =~ s/\\$//) {
309 $line .= <ARGV>;
310 redo LINE unless eof(); # not eof(ARGV)!
311 }
312 # now process $line
313 }
314
315 Note that if there were a "continue" block on the above code, it would
316 get executed only on lines discarded by the regex (since redo skips the
317 continue block). A continue block is often used to reset line counters
318 or "?pat?" one-time matches:
319
320 # inspired by :1,$g/fred/s//WILMA/
321 while (<>) {
322 ?(fred)? && s//WILMA $1 WILMA/;
323 ?(barney)? && s//BETTY $1 BETTY/;
324 ?(homer)? && s//MARGE $1 MARGE/;
325 } continue {
326 print "$ARGV $.: $_";
327 close ARGV if eof(); # reset $.
328 reset if eof(); # reset ?pat?
329 }
330
331 If the word "while" is replaced by the word "until", the sense of the
332 test is reversed, but the conditional is still tested before the first
333 iteration.
334
335 The loop control statements don't work in an "if" or "unless", since
336 they aren't loops. You can double the braces to make them such,
337 though.
338
339 if (/pattern/) {{
340 last if /fred/;
341 next if /barney/; # same effect as "last", but doesn't document as well
342 # do something here
343 }}
344
345 This is caused by the fact that a block by itself acts as a loop that
346 executes once, see "Basic BLOCKs".
347
348 The form "while/if BLOCK BLOCK", available in Perl 4, is no longer
349 available. Replace any occurrence of "if BLOCK" by "if (do BLOCK)".
350
351 For Loops
352 Perl's C-style "for" loop works like the corresponding "while" loop;
353 that means that this:
354
355 for ($i = 1; $i < 10; $i++) {
356 ...
357 }
358
359 is the same as this:
360
361 $i = 1;
362 while ($i < 10) {
363 ...
364 } continue {
365 $i++;
366 }
367
368 There is one minor difference: if variables are declared with "my" in
369 the initialization section of the "for", the lexical scope of those
370 variables is exactly the "for" loop (the body of the loop and the
371 control sections).
372
373 Besides the normal array index looping, "for" can lend itself to many
374 other interesting applications. Here's one that avoids the problem you
375 get into if you explicitly test for end-of-file on an interactive file
376 descriptor causing your program to appear to hang.
377
378 $on_a_tty = -t STDIN && -t STDOUT;
379 sub prompt { print "yes? " if $on_a_tty }
380 for ( prompt(); <STDIN>; prompt() ) {
381 # do something
382 }
383
384 Using "readline" (or the operator form, "<EXPR>") as the conditional of
385 a "for" loop is shorthand for the following. This behaviour is the
386 same as a "while" loop conditional.
387
388 for ( prompt(); defined( $_ = <STDIN> ); prompt() ) {
389 # do something
390 }
391
392 Foreach Loops
393 The "foreach" loop iterates over a normal list value and sets the
394 variable VAR to be each element of the list in turn. If the variable
395 is preceded with the keyword "my", then it is lexically scoped, and is
396 therefore visible only within the loop. Otherwise, the variable is
397 implicitly local to the loop and regains its former value upon exiting
398 the loop. If the variable was previously declared with "my", it uses
399 that variable instead of the global one, but it's still localized to
400 the loop. This implicit localisation occurs only in a "foreach" loop.
401
402 The "foreach" keyword is actually a synonym for the "for" keyword, so
403 you can use "foreach" for readability or "for" for brevity. (Or
404 because the Bourne shell is more familiar to you than csh, so writing
405 "for" comes more naturally.) If VAR is omitted, $_ is set to each
406 value.
407
408 If any element of LIST is an lvalue, you can modify it by modifying VAR
409 inside the loop. Conversely, if any element of LIST is NOT an lvalue,
410 any attempt to modify that element will fail. In other words, the
411 "foreach" loop index variable is an implicit alias for each item in the
412 list that you're looping over.
413
414 If any part of LIST is an array, "foreach" will get very confused if
415 you add or remove elements within the loop body, for example with
416 "splice". So don't do that.
417
418 "foreach" probably won't do what you expect if VAR is a tied or other
419 special variable. Don't do that either.
420
421 Examples:
422
423 for (@ary) { s/foo/bar/ }
424
425 for my $elem (@elements) {
426 $elem *= 2;
427 }
428
429 for $count (10,9,8,7,6,5,4,3,2,1,'BOOM') {
430 print $count, "\n"; sleep(1);
431 }
432
433 for (1..15) { print "Merry Christmas\n"; }
434
435 foreach $item (split(/:[\\\n:]*/, $ENV{TERMCAP})) {
436 print "Item: $item\n";
437 }
438
439 Here's how a C programmer might code up a particular algorithm in Perl:
440
441 for (my $i = 0; $i < @ary1; $i++) {
442 for (my $j = 0; $j < @ary2; $j++) {
443 if ($ary1[$i] > $ary2[$j]) {
444 last; # can't go to outer :-(
445 }
446 $ary1[$i] += $ary2[$j];
447 }
448 # this is where that last takes me
449 }
450
451 Whereas here's how a Perl programmer more comfortable with the idiom
452 might do it:
453
454 OUTER: for my $wid (@ary1) {
455 INNER: for my $jet (@ary2) {
456 next OUTER if $wid > $jet;
457 $wid += $jet;
458 }
459 }
460
461 See how much easier this is? It's cleaner, safer, and faster. It's
462 cleaner because it's less noisy. It's safer because if code gets added
463 between the inner and outer loops later on, the new code won't be
464 accidentally executed. The "next" explicitly iterates the other loop
465 rather than merely terminating the inner one. And it's faster because
466 Perl executes a "foreach" statement more rapidly than it would the
467 equivalent "for" loop.
468
469 Basic BLOCKs
470 A BLOCK by itself (labeled or not) is semantically equivalent to a loop
471 that executes once. Thus you can use any of the loop control
472 statements in it to leave or restart the block. (Note that this is NOT
473 true in "eval{}", "sub{}", or contrary to popular belief "do{}" blocks,
474 which do NOT count as loops.) The "continue" block is optional.
475
476 The BLOCK construct can be used to emulate case structures.
477
478 SWITCH: {
479 if (/^abc/) { $abc = 1; last SWITCH; }
480 if (/^def/) { $def = 1; last SWITCH; }
481 if (/^xyz/) { $xyz = 1; last SWITCH; }
482 $nothing = 1;
483 }
484
485 Such constructs are quite frequently used, because older versions of
486 Perl had no official "switch" statement.
487
488 Switch statements
489 Starting from Perl 5.10, you can say
490
491 use feature "switch";
492
493 which enables a switch feature that is closely based on the Perl 6
494 proposal.
495
496 The keywords "given" and "when" are analogous to "switch" and "case" in
497 other languages, so the code above could be written as
498
499 given($_) {
500 when (/^abc/) { $abc = 1; }
501 when (/^def/) { $def = 1; }
502 when (/^xyz/) { $xyz = 1; }
503 default { $nothing = 1; }
504 }
505
506 This construct is very flexible and powerful. For example:
507
508 use feature ":5.10";
509 given($foo) {
510 when (undef) {
511 say '$foo is undefined';
512 }
513 when ("foo") {
514 say '$foo is the string "foo"';
515 }
516 when ([1,3,5,7,9]) {
517 say '$foo is an odd digit';
518 continue; # Fall through
519 }
520 when ($_ < 100) {
521 say '$foo is numerically less than 100';
522 }
523 when (\&complicated_check) {
524 say 'a complicated check for $foo is true';
525 }
526 default {
527 die q(I don't know what to do with $foo);
528 }
529 }
530
531 "given(EXPR)" will assign the value of EXPR to $_ within the lexical
532 scope of the block, so it's similar to
533
534 do { my $_ = EXPR; ... }
535
536 except that the block is automatically broken out of by a successful
537 "when" or an explicit "break".
538
539 Most of the power comes from implicit smart matching:
540
541 when($foo)
542
543 is exactly equivalent to
544
545 when($_ ~~ $foo)
546
547 Most of the time, "when(EXPR)" is treated as an implicit smart match of
548 $_, i.e. "$_ ~~ EXPR". (See "Smart matching in detail" for more
549 information on smart matching.) But when EXPR is one of the below
550 exceptional cases, it is used directly as a boolean:
551
552 · a subroutine or method call
553
554 · a regular expression match, i.e. "/REGEX/" or "$foo =~ /REGEX/", or
555 a negated regular expression match ("!/REGEX/" or "$foo !~
556 /REGEX/").
557
558 · a comparison such as "$_ < 10" or "$x eq "abc"" (or of course "$_
559 ~~ $c")
560
561 · "defined(...)", "exists(...)", or "eof(...)"
562
563 · a negated expression "!(...)" or "not (...)", or a logical
564 exclusive-or "(...) xor (...)".
565
566 · a filetest operator, with the exception of "-s", "-M", "-A", and
567 "-C", that return numerical values, not boolean ones.
568
569 · the ".." and "..." flip-flop operators.
570
571 In those cases the value of EXPR is used directly as a boolean.
572
573 Furthermore:
574
575 · If EXPR is "... && ..." or "... and ...", the test is applied
576 recursively to both arguments. If both arguments pass the test,
577 then the argument is treated as boolean.
578
579 · If EXPR is "... || ...", "... // ..." or "... or ...", the test is
580 applied recursively to the first argument.
581
582 These rules look complicated, but usually they will do what you want.
583 For example you could write:
584
585 when (/^\d+$/ && $_ < 75) { ... }
586
587 Another useful shortcut is that, if you use a literal array or hash as
588 the argument to "given", it is turned into a reference. So
589 "given(@foo)" is the same as "given(\@foo)", for example.
590
591 "default" behaves exactly like "when(1 == 1)", which is to say that it
592 always matches.
593
594 Breaking out
595
596 You can use the "break" keyword to break out of the enclosing "given"
597 block. Every "when" block is implicitly ended with a "break".
598
599 Fall-through
600
601 You can use the "continue" keyword to fall through from one case to the
602 next:
603
604 given($foo) {
605 when (/x/) { say '$foo contains an x'; continue }
606 when (/y/) { say '$foo contains a y' }
607 default { say '$foo does not contain a y' }
608 }
609
610 Switching in a loop
611
612 Instead of using "given()", you can use a "foreach()" loop. For
613 example, here's one way to count how many times a particular string
614 occurs in an array:
615
616 my $count = 0;
617 for (@array) {
618 when ("foo") { ++$count }
619 }
620 print "\@array contains $count copies of 'foo'\n";
621
622 At the end of all "when" blocks, there is an implicit "next". You can
623 override that with an explicit "last" if you're only interested in the
624 first match.
625
626 This doesn't work if you explicitly specify a loop variable, as in "for
627 $item (@array)". You have to use the default variable $_. (You can use
628 "for my $_ (@array)".)
629
630 Smart matching in detail
631
632 The behaviour of a smart match depends on what type of thing its
633 arguments are. The behaviour is determined by the following table: the
634 first row that applies determines the match behaviour (which is thus
635 mostly determined by the type of the right operand). Note that the
636 smart match implicitly dereferences any non-blessed hash or array ref,
637 so the "Hash" and "Array" entries apply in those cases. (For blessed
638 references, the "Object" entries apply.)
639
640 Note that the "Matching Code" column is not always an exact rendition.
641 For example, the smart match operator short-circuits whenever possible,
642 but "grep" does not.
643
644 $a $b Type of Match Implied Matching Code
645 ====== ===== ===================== =============
646 Any undef undefined !defined $a
647
648 Any Object invokes ~~ overloading on $object, or dies
649
650 Hash CodeRef sub truth for each key[1] !grep { !$b->($_) } keys %$a
651 Array CodeRef sub truth for each elt[1] !grep { !$b->($_) } @$a
652 Any CodeRef scalar sub truth $b->($a)
653
654 Hash Hash hash keys identical (every key is found in both hashes)
655 Array Hash hash keys intersection grep { exists $b->{$_} } @$a
656 Regex Hash hash key grep grep /$a/, keys %$b
657 undef Hash always false (undef can't be a key)
658 Any Hash hash entry existence exists $b->{$a}
659
660 Hash Array hash keys intersection grep { exists $a->{$_} } @$b
661 Array Array arrays are comparable[2]
662 Regex Array array grep grep /$a/, @$b
663 undef Array array contains undef grep !defined, @$b
664 Any Array match against an array element[3]
665 grep $a ~~ $_, @$b
666
667 Hash Regex hash key grep grep /$b/, keys %$a
668 Array Regex array grep grep /$b/, @$a
669 Any Regex pattern match $a =~ /$b/
670
671 Object Any invokes ~~ overloading on $object, or falls back:
672 Any Num numeric equality $a == $b
673 Num numish[4] numeric equality $a == $b
674 undef Any undefined !defined($b)
675 Any Any string equality $a eq $b
676
677 1 - empty hashes or arrays will match.
678 2 - that is, each element smart-matches the element of same index in the
679 other array. [3]
680 3 - If a circular reference is found, we fall back to referential equality.
681 4 - either a real number, or a string that looks like a number
682
683 Custom matching via overloading
684
685 You can change the way that an object is matched by overloading the
686 "~~" operator. This may alter the usual smart match semantics.
687
688 It should be noted that "~~" will refuse to work on objects that don't
689 overload it (in order to avoid relying on the object's underlying
690 structure).
691
692 Note also that smart match's matching rules take precedence over
693 overloading, so if $obj has smart match overloading, then
694
695 $obj ~~ X
696
697 will not automatically invoke the overload method with X as an
698 argument; instead the table above is consulted as normal, and based in
699 the type of X, overloading may or may not be invoked.
700
701 See overload.
702
703 Differences from Perl 6
704
705 The Perl 5 smart match and "given"/"when" constructs are not absolutely
706 identical to their Perl 6 analogues. The most visible difference is
707 that, in Perl 5, parentheses are required around the argument to
708 "given()" and "when()" (except when this last one is used as a
709 statement modifier). Parentheses in Perl 6 are always optional in a
710 control construct such as "if()", "while()", or "when()"; they can't be
711 made optional in Perl 5 without a great deal of potential confusion,
712 because Perl 5 would parse the expression
713
714 given $foo {
715 ...
716 }
717
718 as though the argument to "given" were an element of the hash %foo,
719 interpreting the braces as hash-element syntax.
720
721 The table of smart matches is not identical to that proposed by the
722 Perl 6 specification, mainly due to the differences between Perl 6's
723 and Perl 5's data models.
724
725 In Perl 6, "when()" will always do an implicit smart match with its
726 argument, whilst it is convenient in Perl 5 to suppress this implicit
727 smart match in certain situations, as documented above. (The difference
728 is largely because Perl 5 does not, even internally, have a boolean
729 type.)
730
731 Goto
732 Although not for the faint of heart, Perl does support a "goto"
733 statement. There are three forms: "goto"-LABEL, "goto"-EXPR, and
734 "goto"-&NAME. A loop's LABEL is not actually a valid target for a
735 "goto"; it's just the name of the loop.
736
737 The "goto"-LABEL form finds the statement labeled with LABEL and
738 resumes execution there. It may not be used to go into any construct
739 that requires initialization, such as a subroutine or a "foreach" loop.
740 It also can't be used to go into a construct that is optimized away.
741 It can be used to go almost anywhere else within the dynamic scope,
742 including out of subroutines, but it's usually better to use some other
743 construct such as "last" or "die". The author of Perl has never felt
744 the need to use this form of "goto" (in Perl, that is--C is another
745 matter).
746
747 The "goto"-EXPR form expects a label name, whose scope will be resolved
748 dynamically. This allows for computed "goto"s per FORTRAN, but isn't
749 necessarily recommended if you're optimizing for maintainability:
750
751 goto(("FOO", "BAR", "GLARCH")[$i]);
752
753 The "goto"-&NAME form is highly magical, and substitutes a call to the
754 named subroutine for the currently running subroutine. This is used by
755 "AUTOLOAD()" subroutines that wish to load another subroutine and then
756 pretend that the other subroutine had been called in the first place
757 (except that any modifications to @_ in the current subroutine are
758 propagated to the other subroutine.) After the "goto", not even
759 "caller()" will be able to tell that this routine was called first.
760
761 In almost all cases like this, it's usually a far, far better idea to
762 use the structured control flow mechanisms of "next", "last", or "redo"
763 instead of resorting to a "goto". For certain applications, the catch
764 and throw pair of "eval{}" and die() for exception processing can also
765 be a prudent approach.
766
767 PODs: Embedded Documentation
768 Perl has a mechanism for intermixing documentation with source code.
769 While it's expecting the beginning of a new statement, if the compiler
770 encounters a line that begins with an equal sign and a word, like this
771
772 =head1 Here There Be Pods!
773
774 Then that text and all remaining text up through and including a line
775 beginning with "=cut" will be ignored. The format of the intervening
776 text is described in perlpod.
777
778 This allows you to intermix your source code and your documentation
779 text freely, as in
780
781 =item snazzle($)
782
783 The snazzle() function will behave in the most spectacular
784 form that you can possibly imagine, not even excepting
785 cybernetic pyrotechnics.
786
787 =cut back to the compiler, nuff of this pod stuff!
788
789 sub snazzle($) {
790 my $thingie = shift;
791 .........
792 }
793
794 Note that pod translators should look at only paragraphs beginning with
795 a pod directive (it makes parsing easier), whereas the compiler
796 actually knows to look for pod escapes even in the middle of a
797 paragraph. This means that the following secret stuff will be ignored
798 by both the compiler and the translators.
799
800 $a=3;
801 =secret stuff
802 warn "Neither POD nor CODE!?"
803 =cut back
804 print "got $a\n";
805
806 You probably shouldn't rely upon the "warn()" being podded out forever.
807 Not all pod translators are well-behaved in this regard, and perhaps
808 the compiler will become pickier.
809
810 One may also use pod directives to quickly comment out a section of
811 code.
812
813 Plain Old Comments (Not!)
814 Perl can process line directives, much like the C preprocessor. Using
815 this, one can control Perl's idea of filenames and line numbers in
816 error or warning messages (especially for strings that are processed
817 with "eval()"). The syntax for this mechanism is the same as for most
818 C preprocessors: it matches the regular expression
819
820 # example: '# line 42 "new_filename.plx"'
821 /^\# \s*
822 line \s+ (\d+) \s*
823 (?:\s("?)([^"]+)\2)? \s*
824 $/x
825
826 with $1 being the line number for the next line, and $3 being the
827 optional filename (specified with or without quotes).
828
829 There is a fairly obvious gotcha included with the line directive:
830 Debuggers and profilers will only show the last source line to appear
831 at a particular line number in a given file. Care should be taken not
832 to cause line number collisions in code you'd like to debug later.
833
834 Here are some examples that you should be able to type into your
835 command shell:
836
837 % perl
838 # line 200 "bzzzt"
839 # the `#' on the previous line must be the first char on line
840 die 'foo';
841 __END__
842 foo at bzzzt line 201.
843
844 % perl
845 # line 200 "bzzzt"
846 eval qq[\n#line 2001 ""\ndie 'foo']; print $@;
847 __END__
848 foo at - line 2001.
849
850 % perl
851 eval qq[\n#line 200 "foo bar"\ndie 'foo']; print $@;
852 __END__
853 foo at foo bar line 200.
854
855 % perl
856 # line 345 "goop"
857 eval "\n#line " . __LINE__ . ' "' . __FILE__ ."\"\ndie 'foo'";
858 print $@;
859 __END__
860 foo at goop line 345.
861
862
863
864perl v5.12.4 2011-06-07 PERLSYN(1)