GivenWhen, is arguably the most awesome feature in PerlSix. (Arguably because others say it's the only awesome feature in PerlSix :D)
GivenWhen allows you to write SwitchCase statements in an elegant, expressive way:
given($foo) {
when any(1, 3, 5) {
say '$foo is an odd number less than 6!';
}
when 6..100 {
say '$foo is between 6 and 100'
}
when any() {
say '$foo is "bar" or "baz" or "quux"'
}
when /foo/ & /bar/ & /bazzoo/ {
say '$foo contains "foo", "bar", and "bazzoo", too!'
}
when &complicated_check {
say 'complicated_check($foo) returns true!'
}
when $_ < 0 {
say '$foo is negative!'
}
default {
say 'I give up, you decide. $foo is: ' ~ $foo;
}
}
The when statement translates like so:
when x {blah}
becomes
if $_ ~~ x {blah; succeed}
As such, when can also be used in foreach loops which pass in the parameter $_, like so:
for (@array) {
when Array {
print "Multidimensional!";
}
when any(1, 2, 3) {
print "1, 2, or 3!";
}
}