FAQ 7.23 How do I create a switch or case statement?

Discussion in 'Perl Misc' started by PerlFAQ Server, Mar 8, 2011.

  1. This is an excerpt from the latest version perlfaq7.pod, which
    comes with the standard Perl distribution. These postings aim to
    reduce the number of repeated questions as well as allow the community
    to review and update the answers. The latest version of the complete
    perlfaq is at http://faq.perl.org .


    7.23: How do I create a switch or case statement?

    In Perl 5.10, use the "given-when" construct described in perlsyn:

    use 5.010;

    given ( $string ) {
    when( 'Fred' ) { say "I found Fred!" }
    when( 'Barney' ) { say "I found Barney!" }
    when( /Bamm-?Bamm/ ) { say "I found Bamm-Bamm!" }
    default { say "I don't recognize the name!" }

    If one wants to use pure Perl and to be compatible with Perl versions
    prior to 5.10, the general answer is to use "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

    SWITCH: for (ref $whatchamacallit) {

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

    /SCALAR/ && do {
    last SWITCH;

    /ARRAY/ && do {
    last SWITCH;

    /HASH/ && do {
    last SWITCH;

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


    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}) {
    } 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.


    The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
    are not necessarily experts in every domain where Perl might show up,
    so please include as much information as possible and relevant in any
    corrections. The perlfaq-workers also don't have access to every
    operating system or platform, so please include relevant details for
    corrections to examples that do not work on particular platforms.
    Working code is greatly appreciated.

    If you'd like to help maintain the perlfaq, see the details in
    PerlFAQ Server, Mar 8, 2011
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vic

    switch case statement

    vic, Mar 3, 2004, in forum: Java
  2. Joe Stevenson

    case/switch statement?

    Joe Stevenson, Jun 11, 2005, in forum: Python
    Roy Smith
    Jun 23, 2005
  3. Guy Noir
    Guy Noir
    Nov 2, 2006
  4. Phoe6
    Gordon Burditt
    Feb 28, 2005
  5. PerlFAQ Server
    PerlFAQ Server
    Jan 26, 2011

Share This Page