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