From the examples in the link, I don't see an essential difference or benefit to the developer.
From the examples in the link, I don't see an essential difference or benefit to the developer.
for (@hosts) {
when (@special) {
...
}
when (/primary/) {
...
}
when (/standby/) {
...
}
default {
...
}
}
As well, when uses a value-based comparison called 'smartmatch', so it's more than a simple equality test like switch/case in C. Smartmatching a scalar against a hash tests for 'exists $hash{$_}'. Smartmatching a scalar against an array does a grep, returning all the items in the array that smartmatch against the value. Smartmatching an array against an array does a recursive smartmatch against pairs of array values. You can also customize smartmatch behaviour for your own classes, by overriding the ~~ operator.Check the 'perlsyn' man page for the full set of smartmatch behaviours for core types.
http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detai... http://perldoc.perl.org/perlsyn.html#Switch-statements
I hope this answered your question.
given temporarily sets the topic($_) to an expression. when smart-matches against topic and executes some code (followed by an implicit "next") if the match is successful.
given can be used without when and vice-versa.
given is useful for situations where you need to perform several operations on the result of an expression but don't want to name it, or in some cases, for when you want to emphasize an operation instead of the operand. For example, instead of 'say (some-long-complicated-expression.with-lots-of-calls-and-operators).perl', 'say .perl given the-expression'.
when can be used in given, but it can also be used in CATCH blocks (where $_ is set to the exception), or in for loops or other blocks where you are taking $_ as a parameter.