Given/when - The Perl switch statement
transfixedbutnotdead.com
transfixedbutnotdead.com
Useless in practice for me; I write for CPAN, so I have to stay backward-compatible at least to 5.6. But a nice taste of what I'll start doing in 2015 or so.
I would only aim to support 5.8.9, 5.10.1, and 5.12.2, and at this point I don't know of anyone still using 5.8, so I just stick with the two most modern versions.
When 5.14 is released, it will really be time to kiss 5.8 goodbye.
Anyone who hasn't upgraded Perl 5 in eight and a half years seems unlikely to install new software.
Could your audience also be limited by the fact that your modules are IRC bots and bindings to XML parsers that already have bindings? :)
I would draw the line at 5.10 for "serious production stuff" and 5.12 for anything else. If you can't use the second oldest version, you can write your own code.
It's almost like someone took Gherkin (the Cucumber BDD parser) and got it working inline.
Almost but not quite. given/when originates from perl6 and has been in the spec from at least 2006 and probably a lot earlier [1]. Its also been part of perl5 since 5.10 (2007) [2].
I believe both of these predate Cucumber/Gherkin.
refs:
[1] http://web.archive.org/web/20061011073242/http://perlcabal.o...
[2] http://perldoc.perl.org/perl5100delta.html#Switch-and-Smart-...
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.
Beyond that, might as well make fun of any differences between languages or just devolve into bickering over indentation/coding style/which line the opening brace should go on/etc.