Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: range comparison in given

by tobyink (Abbot)
on Jul 10, 2013 at 10:40 UTC ( #1043443=note: print w/replies, xml ) Need Help??

in reply to range comparison in given

given( $x ){ when ([2..5]) { ...; } when ([6..10]) { ...; } default { ...; } }

Bear in mind that as of Perl 5.18, given/when has been marked experimental (and issues warnings) because it may undergo major changes in a future version of Perl.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Replies are listed 'Best First'.
Re^2: range comparison in given
by mrguy123 (Hermit) on Jul 10, 2013 at 10:45 UTC
    Thanks for the tip, works perfectly!
    How much should I worry about given/when?
    I still have bad memories from Perl's switch

      when is considered to be particularly confusing, as it does two different things:

      when ("foo") {...} # means: if ($_ ~~ "foo") when ( constant() ) {...} # means: if ($_ ~~ constant()) when ( somefunc() ) {...} # means: if (somefunc())

      The rules for determining when there's an implicit "$_ ~~" in front of the condition usually do what you mean, but are very convoluted, so it's easy to shoot yourself in the foot.

      given has already changed in Perl 5.18 to use our $_ instead of my $_ (unless there's already a my $_ in the current lexical scope).

      Smart match itself is likely to undergo some simplification. If you take a look at "Smartmatch operator" in perlop, you'll see basically a dispatch table of all the different types of matches it can attempt. This is likely to be cut down to just a handful.

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      Be afraid; be very afraid! ... unless you're writing one-offs that will never be used again nor ever be used with a later version of Perl.

      If you didn't program your executable by toggling in binary, it wasn't really programming!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043443]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2018-02-20 22:09 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (274 votes). Check out past polls.