Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^3: perldelta unclear on 'given's fate

by LanX (Canon)
on Jan 04, 2014 at 15:35 UTC ( #1069294=note: print w/ replies, xml ) Need Help??

in reply to Re^2: perldelta unclear on 'given's fate
in thread perldelta unclear on 'given's fate

Hello morelenmir

maybe of interest:

Understanding the benefit of Given/When ...

HTH! =)

Cheers Rolf

( addicted to the Perl Programming Language)

Comment on Re^3: perldelta unclear on 'given's fate
Re^4: perldelta unclear on 'given's fate
by morelenmir (Sexton) on Jan 04, 2014 at 15:47 UTC

    Going from that discussion it looks like a good part of 'given's usefulness is centred on smartmatch... Which I don't use in any other way, therefore perhaps 'given' is not all that important after all.

    At the end of the day I suppose I would just like a perl 'switch/case/default' because I am very used to that way of thinking and controlling execution.

      It's essentially the same with For-If just the fall trough is inverse.

      See provided code examples.


      i.e. you need to terminate with explicit next statements to avoid fall through.

      for ($var){ if (/abc/) { print "abc"} # falls through if (/def/) { print "def" ;next} if (/xyz/) { print "xyz" ;next} print "default"; }

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        I actually think that's pretty cool, despite my desire to see a native switch. I have often use hash constructs to accomplish switch-like behavior, and I think that the for ( $var ) ... approach is much nicer...

        # Example of the hash-construct "switch", which is limited and has no +"default". # The output of this code is "abc" use Moo; my $val = 'c'; my @args = qw( abc ); my $obj = __PACKAGE__->new(); { a => sub { say 'a' }, b => \&b, c => sub { $obj->c( @_ ) }, }->{ $val }->( @args ); sub b { say 'b' }; sub c { shift; say for @_ };

        A mistake can be valuable or costly, depending on how faithfully you pursue correction
        Hmmm LanX, not unlike the construct I found when and have used ever since, I was looking for an alternative to the Switch modules available at the time - viz...
        for ($var){ SWITCH: { /abc/ && do { print "abc"} # falls through /def/ && do { print "def" ; last SWITCH } /xyz/ && do { print "xyz" ; last SWITCH } print "default"; }; . . }
        A construct I much prefer (for readability if nothing else) to what sometimes appear to extend to seemingly interminable if-elif-else constructs.

        A user level that continues to overstate my experience :-))

      If you want to use some kind of switch statement, you could always try searching CPAN for a switch module.

      One module that I've recently used was Switch::Plain. If I understand the documentation correctly, it does not use smartmatch. However, it is limited to "simple string or numeric equality tests". The module's author also includes in the documentation a comparison between this module's implementation of switch and C's switch. Depending on your needs, this module might provide what you're looking for since it does provide a "switch/case/default" construct.

        Also consider Switcheroo. :-)

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1069294]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-08-01 22:37 GMT
Find Nodes?
    Voting Booth?

    Who would be the most fun to work for?

    Results (50 votes), past polls