user391986
user391986

Reputation: 30906

perl stacking switch case statements

In php I can stack case statements, can I do equivalent with perl?

        switch($myValue)             
        {            
            case "one":
            case "two":
            break;
            case "three":
            break;
            default:
            break;
         }

Upvotes: 3

Views: 2672

Answers (5)

Ragu Kattinakere
Ragu Kattinakere

Reputation: 11

The following is working for me:

use Switch;

Upvotes: -1

Jurgen Pletinckx
Jurgen Pletinckx

Reputation: 108

If you need to fall through, there is the continue statement, which lets you do so explicitly

given($foo) {
  when (/x/) { say '$foo contains an x'; continue }
  when (/y/) { say '$foo contains a y' }
  default    { say '$foo does not contain a y' }
}

rather than implicitly (as by omitting the break statement in the OP's example).

Sample snipped from perlsyn.

Upvotes: 1

Axeman
Axeman

Reputation: 29854

In Perl, that would be:

use 5.010;
given( $myValue )             
{            
    when ( [qw<one two>] ) { say; }
    when ( "three" )       { say "I'm $_";  }
    default                { say qq{DEFAULT for "$_"};  }
 }

And to test it:

use 5.010;

foreach my $myValue ( qw<one two three four> ) 
{
    given( $myValue )             
    {            
        when ( [qw<one two>] ) { say; }
        when ( "three" )       { say "I'm $_";  }
        default                { say qq{DEFAULT for "$_"};  }
     }
}

And it prints:

one
two
I'm three
DEFAULT for "four"

Perl allows another type of fall-through, with the continue statement. But the criteria of both cases have to be satisfied. Thus:

given( $myValue )
{
    when ( 'one' ) { say 'one!'; continue; }
    when ( 'two' ) { say 'two!'; }
}

makes no sense, because it will never be a case where $s ~~ 'one' && $s ~~ 'two', but if you made the second case:

    when ( /\bo|o\b/ ) { say q/has 'o'!/; }

the fall-through condition is capable of being true. One caveat: since everything complies with default, if you have a default case the continue will hit it if nothing else. continue seems to say "Ok, now look for what other predicates apply."

I mention this simply because it looks deceptively like a "fall-through operator of sorts", but it's really a "look again" operator.

Upvotes: 5

frezik
frezik

Reputation: 2316

In Perl < 5.10, you can fake it with subrefs in a hash:

my %switch = (
    foo => sub { do_something() },
    bar => sub { do_something_else() },
);
exists $switch{$check_var} ? $switch{$check_var}->() : do_the_default_case();

It doesn't give you fall-through (no Duff's Device), although that's a dubious feature.

Upvotes: 4

Alan Haggai Alavi
Alan Haggai Alavi

Reputation: 74252

Switch statements in Perl>= 5.10 are implemented using given and when constructs. It is highly flexible due to smart-matching of the value passed to given.

use feature qw( switch );
use strict;
use warnings;

given ($value) {
    when ( [qw/one two/] ) {
        # do something
    }
    when ('three') {
        # do some other thing
    }
    default {
        # default to something
    }
}

You could even write when ( [qw/one two/] ) as when ( /one|two/ ).

Upvotes: 15

Related Questions