R
Ronny
Assuming the following (the code should be compatible to Perl 5.8.3 AND
Perl 6):
use Switch 'perl6';
use constant { FOO => 1, BAR => 2, BAZ => 3 };
my $var = BAR;
...
Now I would like to write a "switch" expression, where one of the cases
shoulb
be executed if $var is either BAR or BAZ:
given($var) {
when(FOO) { handle_foo() }
when(??? what do I put here ???) { handle_ba() }
else { warn "Illegal value: $var\n"; }
}
So the question is, how do I express "either BAR or BAZ" in the second
"when"?
I found one solution to this, but I don't like it: Since the argument
of when is allowed
to be a regexp, I could use
when(/^(@{[BAR]}|@{[BAZ]}$/) { handle_ba() }
but this is slightly ugly IMO. Has someone a better solution for this?
Note that I look for a solution using given...when. I'm aware that one
could skin this
cat in completely different way too, but that's not the point here.
Ronald
Perl 6):
use Switch 'perl6';
use constant { FOO => 1, BAR => 2, BAZ => 3 };
my $var = BAR;
...
Now I would like to write a "switch" expression, where one of the cases
shoulb
be executed if $var is either BAR or BAZ:
given($var) {
when(FOO) { handle_foo() }
when(??? what do I put here ???) { handle_ba() }
else { warn "Illegal value: $var\n"; }
}
So the question is, how do I express "either BAR or BAZ" in the second
"when"?
I found one solution to this, but I don't like it: Since the argument
of when is allowed
to be a regexp, I could use
when(/^(@{[BAR]}|@{[BAZ]}$/) { handle_ba() }
but this is slightly ugly IMO. Has someone a better solution for this?
Note that I look for a solution using given...when. I'm aware that one
could skin this
cat in completely different way too, but that's not the point here.
Ronald