in reply to Would you use a switch instead?

There is the Switch module, but it's a source filter and considered evil. Perl 5.10 has the given/when statements, that are the equivalent of Switch.

If you're stuck on an earlier perl, you could also use something like:

for ($value) { /aaa/ && do { $foo = 'wanna'; last }; /bbb/ && do { $foo = 'be'; last }; /ccc/ && do { $foo = 'startin'; last }; $foo = 'somethin'; }

Tested with dodgy eyes only!

Replies are listed 'Best First'.
Re^2: Would you use a switch instead?
by AnomalousMonk (Archbishop) on Jul 04, 2009 at 16:15 UTC
    And, of course, foreach-loop topicalization can also be combined with the original nested ternary expression, although definition and initialization of the variable  $foo are no longer so closely tied together:
    >perl -wMstrict -le "my $value = 'bbb'; my $foo; for ($value) { $foo = /aaa/ ? 'wanna' : /bbb/ ? 'be' : /ccc/ ? 'startin' : 'somethin' ; } print $foo; " be

      but of course we always use an explicit lexical as a loop variable so there's no gain there is there? ;)


      True laziness is hard work