| 1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "Switch 3" |
| 132 | .TH Switch 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | Switch \- A switch statement for Perl |
| 135 | .SH "VERSION" |
| 136 | .IX Header "VERSION" |
| 137 | This document describes version 2.10 of Switch, |
| 138 | released Dec 29, 2003. |
| 139 | .SH "SYNOPSIS" |
| 140 | .IX Header "SYNOPSIS" |
| 141 | .Vb 1 |
| 142 | \& use Switch; |
| 143 | .Ve |
| 144 | .PP |
| 145 | .Vb 1 |
| 146 | \& switch ($val) { |
| 147 | .Ve |
| 148 | .PP |
| 149 | .Vb 11 |
| 150 | \& case 1 { print "number 1" } |
| 151 | \& case "a" { print "string a" } |
| 152 | \& case [1..10,42] { print "number in list" } |
| 153 | \& case (@array) { print "number in list" } |
| 154 | \& case /\ew+/ { print "pattern" } |
| 155 | \& case qr/\ew+/ { print "pattern" } |
| 156 | \& case (%hash) { print "entry in hash" } |
| 157 | \& case (\e%hash) { print "entry in hash" } |
| 158 | \& case (\e&sub) { print "arg to subroutine" } |
| 159 | \& else { print "previous case not true" } |
| 160 | \& } |
| 161 | .Ve |
| 162 | .SH "BACKGROUND" |
| 163 | .IX Header "BACKGROUND" |
| 164 | [Skip ahead to \*(L"\s-1DESCRIPTION\s0\*(R" if you don't care about the whys |
| 165 | and wherefores of this control structure] |
| 166 | .PP |
| 167 | In seeking to devise a \*(L"Swiss Army\*(R" case mechanism suitable for Perl, |
| 168 | it is useful to generalize this notion of distributed conditional |
| 169 | testing as far as possible. Specifically, the concept of \*(L"matching\*(R" |
| 170 | between the switch value and the various case values need not be |
| 171 | restricted to numeric (or string or referential) equality, as it is in other |
| 172 | languages. Indeed, as Table 1 illustrates, Perl |
| 173 | offers at least eighteen different ways in which two values could |
| 174 | generate a match. |
| 175 | .PP |
| 176 | .Vb 1 |
| 177 | \& Table 1: Matching a switch value ($s) with a case value ($c) |
| 178 | .Ve |
| 179 | .PP |
| 180 | .Vb 3 |
| 181 | \& Switch Case Type of Match Implied Matching Code |
| 182 | \& Value Value |
| 183 | \& ====== ===== ===================== ============= |
| 184 | .Ve |
| 185 | .PP |
| 186 | .Vb 2 |
| 187 | \& number same numeric or referential match if $s == $c; |
| 188 | \& or ref equality |
| 189 | .Ve |
| 190 | .PP |
| 191 | .Vb 3 |
| 192 | \& object method result of method call match if $s->$c(); |
| 193 | \& ref name match if defined $s->$c(); |
| 194 | \& or ref |
| 195 | .Ve |
| 196 | .PP |
| 197 | .Vb 3 |
| 198 | \& other other string equality match if $s eq $c; |
| 199 | \& non-ref non-ref |
| 200 | \& scalar scalar |
| 201 | .Ve |
| 202 | .PP |
| 203 | .Vb 1 |
| 204 | \& string regexp pattern match match if $s =~ /$c/; |
| 205 | .Ve |
| 206 | .PP |
| 207 | .Vb 3 |
| 208 | \& array scalar array entry existence match if 0<=$c && $c<@$s; |
| 209 | \& ref array entry definition match if defined $s->[$c]; |
| 210 | \& array entry truth match if $s->[$c]; |
| 211 | .Ve |
| 212 | .PP |
| 213 | .Vb 5 |
| 214 | \& array array array intersection match if intersects(@$s, @$c); |
| 215 | \& ref ref (apply this table to |
| 216 | \& all pairs of elements |
| 217 | \& $s->[$i] and |
| 218 | \& $c->[$j]) |
| 219 | .Ve |
| 220 | .PP |
| 221 | .Vb 2 |
| 222 | \& array regexp array grep match if grep /$c/, @$s; |
| 223 | \& ref |
| 224 | .Ve |
| 225 | .PP |
| 226 | .Vb 3 |
| 227 | \& hash scalar hash entry existence match if exists $s->{$c}; |
| 228 | \& ref hash entry definition match if defined $s->{$c}; |
| 229 | \& hash entry truth match if $s->{$c}; |
| 230 | .Ve |
| 231 | .PP |
| 232 | .Vb 2 |
| 233 | \& hash regexp hash grep match if grep /$c/, keys %$s; |
| 234 | \& ref |
| 235 | .Ve |
| 236 | .PP |
| 237 | .Vb 2 |
| 238 | \& sub scalar return value defn match if defined $s->($c); |
| 239 | \& ref return value truth match if $s->($c); |
| 240 | .Ve |
| 241 | .PP |
| 242 | .Vb 2 |
| 243 | \& sub array return value defn match if defined $s->(@$c); |
| 244 | \& ref ref return value truth match if $s->(@$c); |
| 245 | .Ve |
| 246 | .PP |
| 247 | In reality, Table 1 covers 31 alternatives, because only the equality and |
| 248 | intersection tests are commutative; in all other cases, the roles of |
| 249 | the \f(CW$s\fR and \f(CW$c\fR variables could be reversed to produce a |
| 250 | different test. For example, instead of testing a single hash for |
| 251 | the existence of a series of keys (\f(CW\*(C`match if exists $s\->{$c}\*(C'\fR), |
| 252 | one could test for the existence of a single key in a series of hashes |
| 253 | (\f(CW\*(C`match if exists $c\->{$s}\*(C'\fR). |
| 254 | .PP |
| 255 | As perltodo observes, a Perl case mechanism must support all these |
| 256 | \&\*(L"ways to do it\*(R". |
| 257 | .SH "DESCRIPTION" |
| 258 | .IX Header "DESCRIPTION" |
| 259 | The Switch.pm module implements a generalized case mechanism that covers |
| 260 | the numerous possible combinations of switch and case values described above. |
| 261 | .PP |
| 262 | The module augments the standard Perl syntax with two new control |
| 263 | statements: \f(CW\*(C`switch\*(C'\fR and \f(CW\*(C`case\*(C'\fR. The \f(CW\*(C`switch\*(C'\fR statement takes a |
| 264 | single scalar argument of any type, specified in parentheses. |
| 265 | \&\f(CW\*(C`switch\*(C'\fR stores this value as the |
| 266 | current switch value in a (localized) control variable. |
| 267 | The value is followed by a block which may contain one or more |
| 268 | Perl statements (including the \f(CW\*(C`case\*(C'\fR statement described below). |
| 269 | The block is unconditionally executed once the switch value has |
| 270 | been cached. |
| 271 | .PP |
| 272 | A \f(CW\*(C`case\*(C'\fR statement takes a single scalar argument (in mandatory |
| 273 | parentheses if it's a variable; otherwise the parens are optional) and |
| 274 | selects the appropriate type of matching between that argument and the |
| 275 | current switch value. The type of matching used is determined by the |
| 276 | respective types of the switch value and the \f(CW\*(C`case\*(C'\fR argument, as |
| 277 | specified in Table 1. If the match is successful, the mandatory |
| 278 | block associated with the \f(CW\*(C`case\*(C'\fR statement is executed. |
| 279 | .PP |
| 280 | In most other respects, the \f(CW\*(C`case\*(C'\fR statement is semantically identical |
| 281 | to an \f(CW\*(C`if\*(C'\fR statement. For example, it can be followed by an \f(CW\*(C`else\*(C'\fR |
| 282 | clause, and can be used as a postfix statement qualifier. |
| 283 | .PP |
| 284 | However, when a \f(CW\*(C`case\*(C'\fR block has been executed control is automatically |
| 285 | transferred to the statement after the immediately enclosing \f(CW\*(C`switch\*(C'\fR |
| 286 | block, rather than to the next statement within the block. In other |
| 287 | words, the success of any \f(CW\*(C`case\*(C'\fR statement prevents other cases in the |
| 288 | same scope from executing. But see \*(L"Allowing fall\-through\*(R" below. |
| 289 | .PP |
| 290 | Together these two new statements provide a fully generalized case |
| 291 | mechanism: |
| 292 | .PP |
| 293 | .Vb 1 |
| 294 | \& use Switch; |
| 295 | .Ve |
| 296 | .PP |
| 297 | .Vb 1 |
| 298 | \& # AND LATER... |
| 299 | .Ve |
| 300 | .PP |
| 301 | .Vb 1 |
| 302 | \& %special = ( woohoo => 1, d'oh => 1 ); |
| 303 | .Ve |
| 304 | .PP |
| 305 | .Vb 2 |
| 306 | \& while (<>) { |
| 307 | \& switch ($_) { |
| 308 | .Ve |
| 309 | .PP |
| 310 | .Vb 3 |
| 311 | \& case (%special) { print "homer\en"; } # if $special{$_} |
| 312 | \& case /a-z/i { print "alpha\en"; } # if $_ =~ /a-z/i |
| 313 | \& case [1..9] { print "small num\en"; } # if $_ in [1..9] |
| 314 | .Ve |
| 315 | .PP |
| 316 | .Vb 3 |
| 317 | \& case { $_[0] >= 10 } { # if $_ >= 10 |
| 318 | \& my $age = <>; |
| 319 | \& switch (sub{ $_[0] < $age } ) { |
| 320 | .Ve |
| 321 | .PP |
| 322 | .Vb 5 |
| 323 | \& case 20 { print "teens\en"; } # if 20 < $age |
| 324 | \& case 30 { print "twenties\en"; } # if 30 < $age |
| 325 | \& else { print "history\en"; } |
| 326 | \& } |
| 327 | \& } |
| 328 | .Ve |
| 329 | .PP |
| 330 | .Vb 2 |
| 331 | \& print "must be punctuation\en" case /\eW/; # if $_ ~= /\eW/ |
| 332 | \& } |
| 333 | .Ve |
| 334 | .PP |
| 335 | Note that \f(CW\*(C`switch\*(C'\fRes can be nested within \f(CW\*(C`case\*(C'\fR (or any other) blocks, |
| 336 | and a series of \f(CW\*(C`case\*(C'\fR statements can try different types of matches |
| 337 | \&\*(-- hash membership, pattern match, array intersection, simple equality, |
| 338 | etc. \*(-- against the same switch value. |
| 339 | .PP |
| 340 | The use of intersection tests against an array reference is particularly |
| 341 | useful for aggregating integral cases: |
| 342 | .PP |
| 343 | .Vb 8 |
| 344 | \& sub classify_digit |
| 345 | \& { |
| 346 | \& switch ($_[0]) { case 0 { return 'zero' } |
| 347 | \& case [2,4,6,8] { return 'even' } |
| 348 | \& case [1,3,4,7,9] { return 'odd' } |
| 349 | \& case /[A-F]/i { return 'hex' } |
| 350 | \& } |
| 351 | \& } |
| 352 | .Ve |
| 353 | .Sh "Allowing fall-through" |
| 354 | .IX Subsection "Allowing fall-through" |
| 355 | Fall-though (trying another case after one has already succeeded) |
| 356 | is usually a Bad Idea in a switch statement. However, this |
| 357 | is Perl, not a police state, so there \fIis\fR a way to do it, if you must. |
| 358 | .PP |
| 359 | If a \f(CW\*(C`case\*(C'\fR block executes an untargeted \f(CW\*(C`next\*(C'\fR, control is |
| 360 | immediately transferred to the statement \fIafter\fR the \f(CW\*(C`case\*(C'\fR statement |
| 361 | (i.e. usually another case), rather than out of the surrounding |
| 362 | \&\f(CW\*(C`switch\*(C'\fR block. |
| 363 | .PP |
| 364 | For example: |
| 365 | .PP |
| 366 | .Vb 7 |
| 367 | \& switch ($val) { |
| 368 | \& case 1 { handle_num_1(); next } # and try next case... |
| 369 | \& case "1" { handle_str_1(); next } # and try next case... |
| 370 | \& case [0..9] { handle_num_any(); } # and we're done |
| 371 | \& case /\ed/ { handle_dig_any(); next } # and try next case... |
| 372 | \& case /.*/ { handle_str_any(); next } # and try next case... |
| 373 | \& } |
| 374 | .Ve |
| 375 | .PP |
| 376 | If \f(CW$val\fR held the number \f(CW1\fR, the above \f(CW\*(C`switch\*(C'\fR block would call the |
| 377 | first three \f(CW\*(C`handle_...\*(C'\fR subroutines, jumping to the next case test |
| 378 | each time it encountered a \f(CW\*(C`next\*(C'\fR. After the thrid \f(CW\*(C`case\*(C'\fR block |
| 379 | was executed, control would jump to the end of the enclosing |
| 380 | \&\f(CW\*(C`switch\*(C'\fR block. |
| 381 | .PP |
| 382 | On the other hand, if \f(CW$val\fR held \f(CW10\fR, then only the last two \f(CW\*(C`handle_...\*(C'\fR |
| 383 | subroutines would be called. |
| 384 | .PP |
| 385 | Note that this mechanism allows the notion of \fIconditional fall-through\fR. |
| 386 | For example: |
| 387 | .PP |
| 388 | .Vb 4 |
| 389 | \& switch ($val) { |
| 390 | \& case [0..9] { handle_num_any(); next if $val < 7; } |
| 391 | \& case /\ed/ { handle_dig_any(); } |
| 392 | \& } |
| 393 | .Ve |
| 394 | .PP |
| 395 | If an untargeted \f(CW\*(C`last\*(C'\fR statement is executed in a case block, this |
| 396 | immediately transfers control out of the enclosing \f(CW\*(C`switch\*(C'\fR block |
| 397 | (in other words, there is an implicit \f(CW\*(C`last\*(C'\fR at the end of each |
| 398 | normal \f(CW\*(C`case\*(C'\fR block). Thus the previous example could also have been |
| 399 | written: |
| 400 | .PP |
| 401 | .Vb 4 |
| 402 | \& switch ($val) { |
| 403 | \& case [0..9] { handle_num_any(); last if $val >= 7; next; } |
| 404 | \& case /\ed/ { handle_dig_any(); } |
| 405 | \& } |
| 406 | .Ve |
| 407 | .Sh "Automating fall-through" |
| 408 | .IX Subsection "Automating fall-through" |
| 409 | In situations where case fall-through should be the norm, rather than an |
| 410 | exception, an endless succession of terminal \f(CW\*(C`next\*(C'\fRs is tedious and ugly. |
| 411 | Hence, it is possible to reverse the default behaviour by specifying |
| 412 | the string \*(L"fallthrough\*(R" when importing the module. For example, the |
| 413 | following code is equivalent to the first example in \*(L"Allowing fall\-through\*(R": |
| 414 | .PP |
| 415 | .Vb 1 |
| 416 | \& use Switch 'fallthrough'; |
| 417 | .Ve |
| 418 | .PP |
| 419 | .Vb 7 |
| 420 | \& switch ($val) { |
| 421 | \& case 1 { handle_num_1(); } |
| 422 | \& case "1" { handle_str_1(); } |
| 423 | \& case [0..9] { handle_num_any(); last } |
| 424 | \& case /\ed/ { handle_dig_any(); } |
| 425 | \& case /.*/ { handle_str_any(); } |
| 426 | \& } |
| 427 | .Ve |
| 428 | .PP |
| 429 | Note the explicit use of a \f(CW\*(C`last\*(C'\fR to preserve the non-fall-through |
| 430 | behaviour of the third case. |
| 431 | .Sh "Alternative syntax" |
| 432 | .IX Subsection "Alternative syntax" |
| 433 | Perl 6 will provide a built-in switch statement with essentially the |
| 434 | same semantics as those offered by Switch.pm, but with a different |
| 435 | pair of keywords. In Perl 6 \f(CW\*(C`switch\*(C'\fR will be spelled \f(CW\*(C`given\*(C'\fR, and |
| 436 | \&\f(CW\*(C`case\*(C'\fR will be pronounced \f(CW\*(C`when\*(C'\fR. In addition, the \f(CW\*(C`when\*(C'\fR statement |
| 437 | will not require switch or case values to be parenthesized. |
| 438 | .PP |
| 439 | This future syntax is also (largely) available via the Switch.pm module, by |
| 440 | importing it with the argument \f(CW"Perl6"\fR. For example: |
| 441 | .PP |
| 442 | .Vb 1 |
| 443 | \& use Switch 'Perl6'; |
| 444 | .Ve |
| 445 | .PP |
| 446 | .Vb 8 |
| 447 | \& given ($val) { |
| 448 | \& when 1 { handle_num_1(); } |
| 449 | \& when ($str1) { handle_str_1(); } |
| 450 | \& when [0..9] { handle_num_any(); last } |
| 451 | \& when /\ed/ { handle_dig_any(); } |
| 452 | \& when /.*/ { handle_str_any(); } |
| 453 | \& default { handle anything else; } |
| 454 | \& } |
| 455 | .Ve |
| 456 | .PP |
| 457 | Note that scalars still need to be parenthesized, since they would be |
| 458 | ambiguous in Perl 5. |
| 459 | .PP |
| 460 | Note too that you can mix and match both syntaxes by importing the module |
| 461 | with: |
| 462 | .PP |
| 463 | .Vb 1 |
| 464 | \& use Switch 'Perl5', 'Perl6'; |
| 465 | .Ve |
| 466 | .Sh "Higher-order Operations" |
| 467 | .IX Subsection "Higher-order Operations" |
| 468 | One situation in which \f(CW\*(C`switch\*(C'\fR and \f(CW\*(C`case\*(C'\fR do not provide a good |
| 469 | substitute for a cascaded \f(CW\*(C`if\*(C'\fR, is where a switch value needs to |
| 470 | be tested against a series of conditions. For example: |
| 471 | .PP |
| 472 | .Vb 2 |
| 473 | \& sub beverage { |
| 474 | \& switch (shift) { |
| 475 | .Ve |
| 476 | .PP |
| 477 | .Vb 9 |
| 478 | \& case sub { $_[0] < 10 } { return 'milk' } |
| 479 | \& case sub { $_[0] < 20 } { return 'coke' } |
| 480 | \& case sub { $_[0] < 30 } { return 'beer' } |
| 481 | \& case sub { $_[0] < 40 } { return 'wine' } |
| 482 | \& case sub { $_[0] < 50 } { return 'malt' } |
| 483 | \& case sub { $_[0] < 60 } { return 'Moet' } |
| 484 | \& else { return 'milk' } |
| 485 | \& } |
| 486 | \& } |
| 487 | .Ve |
| 488 | .PP |
| 489 | The need to specify each condition as a subroutine block is tiresome. To |
| 490 | overcome this, when importing Switch.pm, a special \*(L"placeholder\*(R" |
| 491 | subroutine named \f(CW\*(C`_\|_\*(C'\fR [sic] may also be imported. This subroutine |
| 492 | converts (almost) any expression in which it appears to a reference to a |
| 493 | higher-order function. That is, the expression: |
| 494 | .PP |
| 495 | .Vb 1 |
| 496 | \& use Switch '__'; |
| 497 | .Ve |
| 498 | .PP |
| 499 | .Vb 1 |
| 500 | \& __ < 2 + __ |
| 501 | .Ve |
| 502 | .PP |
| 503 | is equivalent to: |
| 504 | .PP |
| 505 | .Vb 1 |
| 506 | \& sub { $_[0] < 2 + $_[1] } |
| 507 | .Ve |
| 508 | .PP |
| 509 | With \f(CW\*(C`_\|_\*(C'\fR, the previous ugly case statements can be rewritten: |
| 510 | .PP |
| 511 | .Vb 7 |
| 512 | \& case __ < 10 { return 'milk' } |
| 513 | \& case __ < 20 { return 'coke' } |
| 514 | \& case __ < 30 { return 'beer' } |
| 515 | \& case __ < 40 { return 'wine' } |
| 516 | \& case __ < 50 { return 'malt' } |
| 517 | \& case __ < 60 { return 'Moet' } |
| 518 | \& else { return 'milk' } |
| 519 | .Ve |
| 520 | .PP |
| 521 | The \f(CW\*(C`_\|_\*(C'\fR subroutine makes extensive use of operator overloading to |
| 522 | perform its magic. All operations involving _\|_ are overloaded to |
| 523 | produce an anonymous subroutine that implements a lazy version |
| 524 | of the original operation. |
| 525 | .PP |
| 526 | The only problem is that operator overloading does not allow the |
| 527 | boolean operators \f(CW\*(C`&&\*(C'\fR and \f(CW\*(C`||\*(C'\fR to be overloaded. So a case statement |
| 528 | like this: |
| 529 | .PP |
| 530 | .Vb 1 |
| 531 | \& case 0 <= __ && __ < 10 { return 'digit' } |
| 532 | .Ve |
| 533 | .PP |
| 534 | doesn't act as expected, because when it is |
| 535 | executed, it constructs two higher order subroutines |
| 536 | and then treats the two resulting references as arguments to \f(CW\*(C`&&\*(C'\fR: |
| 537 | .PP |
| 538 | .Vb 1 |
| 539 | \& sub { 0 <= $_[0] } && sub { $_[0] < 10 } |
| 540 | .Ve |
| 541 | .PP |
| 542 | This boolean expression is inevitably true, since both references are |
| 543 | non\-false. Fortunately, the overloaded \f(CW'bool'\fR operator catches this |
| 544 | situation and flags it as a error. |
| 545 | .SH "DEPENDENCIES" |
| 546 | .IX Header "DEPENDENCIES" |
| 547 | The module is implemented using Filter::Util::Call and Text::Balanced |
| 548 | and requires both these modules to be installed. |
| 549 | .SH "AUTHOR" |
| 550 | .IX Header "AUTHOR" |
| 551 | Damian Conway (damian@conway.org). The maintainer of this module is now Rafael |
| 552 | Garcia-Suarez (rgarciasuarez@free.fr). |
| 553 | .SH "BUGS" |
| 554 | .IX Header "BUGS" |
| 555 | There are undoubtedly serious bugs lurking somewhere in code this funky :\-) |
| 556 | Bug reports and other feedback are most welcome. |
| 557 | .SH "LIMITATIONS" |
| 558 | .IX Header "LIMITATIONS" |
| 559 | Due to the heuristic nature of Switch.pm's source parsing, the presence |
| 560 | of regexes specified with raw \f(CW\*(C`?...?\*(C'\fR delimiters may cause mysterious |
| 561 | errors. The workaround is to use \f(CW\*(C`m?...?\*(C'\fR instead. |
| 562 | .PP |
| 563 | Due to the way source filters work in Perl, you can't use Switch inside |
| 564 | an string \f(CW\*(C`eval\*(C'\fR. |
| 565 | .PP |
| 566 | If your source file is longer then 1 million characters and you have a |
| 567 | switch statement that crosses the 1 million (or 2 million, etc.) |
| 568 | character boundary you will get mysterious errors. The workaround is to |
| 569 | use smaller source files. |
| 570 | .SH "COPYRIGHT" |
| 571 | .IX Header "COPYRIGHT" |
| 572 | .Vb 3 |
| 573 | \& Copyright (c) 1997-2003, Damian Conway. All Rights Reserved. |
| 574 | \& This module is free software. It may be used, redistributed |
| 575 | \& and/or modified under the same terms as Perl itself. |
| 576 | .Ve |