phpman > man > Switch(3pm)

Markdown | JSON | MCP    

Found in /usr/share/perl/5.34/pod/perlfaq7.pod
  How do I create a switch or case statement?
    There is a given/when statement in Perl, but it is experimental and
    likely to change in future. See perlsyn for more details.

    The general answer is to use a CPAN module such as Switch::Plain:

        use Switch::Plain;
        sswitch($variable_holding_a_string) {
            case 'first': { }
            case 'second': { }
            default: { }
        }

    or for more complicated comparisons, "if-elsif-else":

        for ($variable_to_test) {
            if    (/pat1/)  { }     # do something
            elsif (/pat2/)  { }     # do something else
            elsif (/pat3/)  { }     # do something else
            else            { }     # default
        }

    Here's a simple example of a switch based on pattern matching, lined up
    in a way to make it look more like a switch statement. We'll do a
    multiway conditional based on the type of reference stored in
    $whatchamacallit:

        SWITCH: for (ref $whatchamacallit) {

            /^$/           && die "not a reference";

            /SCALAR/       && do {
                            print_scalar($$ref);
                            last SWITCH;
                          };

            /ARRAY/        && do {
                            print_array(@$ref);
                            last SWITCH;
                          };

            /HASH/        && do {
                            print_hash(%$ref);
                            last SWITCH;
                          };

            /CODE/        && do {
                            warn "can't print function ref";
                            last SWITCH;
                          };

            # DEFAULT

            warn "User defined type skipped";

        }

    See perlsyn for other examples in this style.

    Sometimes you should change the positions of the constant and the
    variable. For example, let's say you wanted to test which of many
    answers you were given, but in a case-insensitive way that also allows
    abbreviations. You can use the following technique if the strings all
    start with different characters or if you want to arrange the matches so
    that one takes precedence over another, as "SEND" has precedence over
    "STOP" here:

        chomp($answer = <>);
        if    ("SEND"  =~ /^\Q$answer/i) { print "Action is send\n"  }
        elsif ("STOP"  =~ /^\Q$answer/i) { print "Action is stop\n"  }
        elsif ("ABORT" =~ /^\Q$answer/i) { print "Action is abort\n" }
        elsif ("LIST"  =~ /^\Q$answer/i) { print "Action is list\n"  }
        elsif ("EDIT"  =~ /^\Q$answer/i) { print "Action is edit\n"  }

    A totally different approach is to create a hash of function references.

        my %commands = (
            "happy" => \&joy,
            "sad",  => \&sullen,
            "done"  => sub { die "See ya!" },
            "mad"   => \&angry,
        );

        print "How are you? ";
        chomp($string = <STDIN>);
        if ($commands{$string}) {
            $commands{$string}->();
        } else {
            print "No such command: $string\n";
        }

    Starting from Perl 5.8, a source filter module, "Switch", can also be
    used to get switch and case. Its use is now discouraged, because it's
    not fully compatible with the native switch of Perl 5.10, and because,
    as it's implemented as a source filter, it doesn't always work as
    intended when complex syntax is involved.

Generated by phpman v3.7.12 Author: Che Dong Under GNU General Public License
2026-06-13 11:09 @216.73.216.28
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 TransitionalValid CSS!

^_back to top