

Given/when - The Perl switch statement - prog
http://transfixedbutnotdead.com/2010/08/30/givenwhen-the-perl-switch-statement/

======
Vivtek
What is this, National Perl Appreciation Day or something? This is great!

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.

~~~
marcusramberg
Most of us draw the line around 5.8.6 or so, given that all major
distributions include a Perl at least that new. In fact most of them are at
5.10+ now, with a few notable exceptions.

~~~
Vivtek
I don't like it when my CPAN tests fail - and there are some test rigs out
there running on 5.6. In practice, this has actually only resulted in my
rewriting code once in a part of a module that is standard in 5.8 but wasn't
earlier (one of the Tie modules, I think), but still. Using a whole new
control structure isn't something I'll be doing soon, no matter how attractive
it is.

~~~
jrockway
Put "use 5.010" in your Makefile.PL and you won't see the failures from older
Perl installs.

~~~
Vivtek
Yes, because they won't be running there. Maybe you like to target one version
back, but there's a lot of systems out there running old Perls, and I don't
see why I shouldn't be writing for the largest audience possible.

~~~
jrockway
_I don't see why I shouldn't be writing for the largest audience possible_

Could your audience also be limited by the fact that your modules are IRC bots
and bindings to XML parsers that already have bindings? :)

~~~
Vivtek
Oh, snap, I guess you win that argument.

------
scorchin
Despite not knowing much Perl, seeing Given/When being used as executable code
is beautiful.

It's almost like someone took Gherkin (the Cucumber BDD parser) and got it
working inline.

~~~
draegtun
_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...](http://web.archive.org/web/20061011073242/http://perlcabal.org/syn/S03.html)

[2] [http://perldoc.perl.org/perl5100delta.html#Switch-and-
Smart-...](http://perldoc.perl.org/perl5100delta.html#Switch-and-Smart-Match-
operator)

~~~
colomon
I just checked the book Perl 6 Essentials (2003, sigh) and it has given / when
already, looking very close to the current standard.

------
samuel1604
it has been a long time since I have done any Perl but it's always a pleasure
like seeing a old friend!

------
sendos
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.

~~~
tene
given 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-
detai...](http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detail)
<http://perldoc.perl.org/perlsyn.html#Switch-statements>

I hope this answered your question.

------
xsltuser2010
Will 'while' be replaced with 'aslongas' ?

~~~
pyre
The switch statement isn't being replaced. Perl5 never had a switch statement.
Perl6 implemented on as given/when instead of switch/case. The Perl6 switch
statement was back-ported to Perl 5.10. The 'use Switch' example in the post
was a 3rd party work-around to the lack of a switch statement in the language.

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.

