5 ms·
Can someone explain why given/when is better than switch/case? From the examples in the link, I don't see an essential difference or benefit to the developer.
by sendos 16y ago
Can someone explain why given/when is better than switch/case?
From the examples in the link, I don't see an essential difference or benefit to the developer.
- pyre 16y agoAs it says, the 'use Switch;' version is a source filter as opposed to a feature built directly into the interpreter. For those unfamiliar, a source filter is something that modifies the source code before it actually is interpreted, and you have to be very careful with source filters. If your source filter injects code that blows up, or just isn't careful to preserve things like line numbers, then it can be a mess trying to track down problems because the source that you're looking at isn't exactly the once that's being run.
- draegtun 16y agoPerhaps this is a better example of why it is better than the switch/case? http://news.ycombinator.com/item?id=1735590 http://news.ycombinator.com/item?id=1735590
- simcop2387 16y agoOne of the biggest differences from a C style switch/case is that you can use regular expressions and a few other niceties in each of the when() clauses. if you're comparing it to the older Switch module then the biggest difference is that you're not likely to want to shoot the author of the module. Switch being a source filter can be very dangerous (not because it's a security risk but because combining them can cause all kinds of heisenbugs)
- ekiru 16y agogiven/when isn't a switch statement. given and when are two separate constructs that happen to work well together for switch-statement-style purposes. 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.
- tene 16y agogiven and when are actually separate constructs, useful individually. given sets the topic ($_) to its argument for the scope of the block, and when smartmatches its argument against the topic (in most cases, mostly when it looks like the argument is a single value). This means that you can use when in any construct with $_ set. The other common place I use this is with iteration. 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-detail http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detai... http://perldoc.perl.org/perlsyn.html#Switch-statements http://perldoc.perl.org/perlsyn.html#Switch-statements I hope this answered your question.