Is it possible to get a value from a given-when statement?
Suppose I have a long comparison in which all branches affect
the same variable. Here's a short example (minor variation
from"perlsyn"):
given($something) {
when (/^abc/) { $x = 1; }
when (/^def/) { $x = 2; }
when (/^xyz/) { $x = 3; }
default { $x=-1; }
}
All the explicit assignments to the same variable are pretty ugly;
With an "if" statement, I can do something like:
my $x=do {
$_=$something;
if (/^abc/) { 1; }
elsif (/^def/) { 2; }
elsif (/^xyz/) { 3; }
else { -1; }
};
but the same thing using the new (well, relatively;)
"given-when" statement does not seem to work:
$x=do {
given($something) {
when (/^abc/) { 1; }
when (/^def/) { 2; }
when (/^xyz/) { 3; }
default { -1; }
}
};
Any ideas?
Peter
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/