Lines Matching defs:case

15 # CATCH ATTEMPTS TO CALL case OUTSIDE THE SCOPE OF ANY switch
17 $::_S_W_I_T_C_H = sub { croak "case/when statement not in switch/given block" };
89 return $source unless $Perl5 && $source =~ /case|switch/
145 elsif ($Perl5 && $source =~ m/\G(\s*)(case\b)(?!\s*=>)/gc
152 : "if (Switch::case");
372 sub case($) { local $SIG{__WARN__} = \&carp;
514 case 1 { print "number 1" }
515 case "a" { print "string a" }
516 case [1..10,42] { print "number in list" }
517 case (@array) { print "number in list" }
518 case /\w+/ { print "pattern" }
519 case qr/\w+/ { print "pattern" }
520 case (%hash) { print "entry in hash" }
521 case (\%hash) { print "entry in hash" }
522 case (\&sub) { print "arg to subroutine" }
523 else { print "previous case not true" }
531 In seeking to devise a "Swiss Army" case mechanism suitable for Perl,
534 between the switch value and the various case values need not be
540 Table 1: Matching a switch value ($s) with a case value ($c)
594 As L<perltodo> observes, a Perl case mechanism must support all these
600 The Switch.pm module implements a generalized case mechanism that covers
601 the numerous possible combinations of switch and case values described above.
604 statements: C<switch> and C<case>. The C<switch> statement takes a
609 Perl statements (including the C<case> statement described below).
613 A C<case> statement takes a single scalar argument (in mandatory
617 respective types of the switch value and the C<case> argument, as
619 block associated with the C<case> statement is executed.
621 In most other respects, the C<case> statement is semantically identical
625 However, when a C<case> block has been executed control is automatically
628 words, the success of any C<case> statement prevents other cases in the
631 Together these two new statements provide a fully generalized case
643 case (%special) { print "homer\n"; } # if $special{$_}
644 case /a-z/i { print "alpha\n"; } # if $_ =~ /a-z/i
645 case [1..9] { print "small num\n"; } # if $_ in [1..9]
647 case { $_[0] >= 10 } { # if $_ >= 10
651 case 20 { print "teens\n"; } # if 20 < $age
652 case 30 { print "twenties\n"; } # if 30 < $age
657 print "must be punctuation\n" case /\W/; # if $_ ~= /\W/
660 Note that C<switch>es can be nested within C<case> (or any other) blocks,
661 and a series of C<case> statements can try different types of matches
670 switch ($_[0]) { case 0 { return 'zero' }
671 case [2,4,6,8] { return 'even' }
672 case [1,3,4,7,9] { return 'odd' }
673 case /[A-F]/i { return 'hex' }
680 Fall-though (trying another case after one has already succeeded)
684 If a C<case> block executes an untargetted C<next>, control is
685 immediately transferred to the statement I<after> the C<case> statement
686 (i.e. usually another case), rather than out of the surrounding
692 case 1 { handle_num_1(); next } # and try next case...
693 case "1" { handle_str_1(); next } # and try next case...
694 case [0..9] { handle_num_any(); } # and we're done
695 case /\d/ { handle_dig_any(); next } # and try next case...
696 case /.*/ { handle_str_any(); next } # and try next case...
700 first three C<handle_...> subroutines, jumping to the next case test
701 each time it encountered a C<next>. After the thrid C<case> block
712 case [0..9] { handle_num_any(); next if $val < 7; }
713 case /\d/ { handle_dig_any(); }
716 If an untargetted C<last> statement is executed in a case block, this
719 normal C<case> block). Thus the previous example could also have been
723 case [0..9] { handle_num_any(); last if $val >= 7; next; }
724 case /\d/ { handle_dig_any(); }
730 In situations where case fall-through should be the norm, rather than an
739 case 1 { handle_num_1(); }
740 case "1" { handle_str_1(); }
741 case [0..9] { handle_num_any(); last }
742 case /\d/ { handle_dig_any(); }
743 case /.*/ { handle_str_any(); }
747 behaviour of the third case.
756 C<case> will be pronounced C<when>. In addition, the C<when> statement
757 will not require switch or case values to be parenthesized.
784 One situation in which C<switch> and C<case> do not provide a good
791 case sub { $_[0] < 10 } { return 'milk' }
792 case sub { $_[0] < 20 } { return 'coke' }
793 case sub { $_[0] < 30 } { return 'beer' }
794 case sub { $_[0] < 40 } { return 'wine' }
795 case sub { $_[0] < 50 } { return 'malt' }
796 case sub { $_[0] < 60 } { return 'Moet' }
815 With C<__>, the previous ugly case statements can be rewritten:
817 case __ < 10 { return 'milk' }
818 case __ < 20 { return 'coke' }
819 case __ < 30 { return 'beer' }
820 case __ < 40 { return 'wine' }
821 case __ < 50 { return 'malt' }
822 case __ < 60 { return 'Moet' }
831 boolean operators C<&&> and C<||> to be overloaded. So a case statement
834 case 0 <= __ && __ < 10 { return 'digit' }