Documentation seems sparse regarding when
statements outside of given
blocks. It is said the when
can be used when a 'topic' has been set, but when exactly is a topic considered set? Consider these cases:
for (@arr) {
when { }
}
Seems this is the default, basic case, but I couldn't get even this working on my perl 5.14.2 - Turned out this was just another bash quoting issue.$ perl -Mfeature=switch -e 'foreach (qw(a b c)) { when (/a/) {print 'boom'} }'
prints nothing. What am I doing wrong?
for my $elem (@arr) {
when { }
}
Would this work? Will $elem automatically become the topic for when
to use?
for (@arr) {
$_ = some_expression($_);
when { }
}
Would this work? Can the topic be set inside the loop?
Also, is there any difference when each of the above code segments uses foreach
instead of for
?
Basically, I'm very unclear on the topic of topics, so please enlighten me.