I suppose I was uneasy about the possibility of the when (/[ab]/) { ... } tail clause 'getting lost', dependent as it is on a continue statement in other, possibly distant, clauses, and on the relative positions of those clauses (also a dependency of Neighbour's approach). Taking apl's suggestion, I am more comfortable with something like the following, which encapsulates the tail of each 'tailed' clause within the clause itself:
>perl -wMstrict -lE
"for my $s qw(a b c d) {
given ($s) {
my $tail_a_and_b = sub { say qq{ after a or b, was '$_' } };
when ('a') { say 'a'; $tail_a_and_b->(); }
when ('b') { say 'b'; $tail_a_and_b->(); }
when ('d') { say 'd'; }
default { say qq{other: '$_'}; }
}
}
"
a
after a or b, was 'a'
b
after a or b, was 'b'
other: 'c'
d
|