Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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

by morelenmir (Beadle)
on Jan 04, 2014 at 10:31 UTC ( #1069293=note: print w/replies, xml ) Need Help??

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

Yep, sure enough. That method of using 'foreach' looks to be almost identical to 'given' and provides nearly the same results. Although I will say manhandling 'foreach' to work like this seems more of bodge than having a proper 'switch' analogue.

For now I'll follow you're advice toolic and swap over to 'foreach' until it is clear one way or the other if 'given' is going to make the cut.

Reading that "perlsym" page and indeed, other posts here and at "stackoverflow" I get the feeling there is a fair bit of hostility towards poor old 'given/when'. Can you fill me in on why that might be?

However... On trying it I still get warnings for 'when'. So... I guess the safest thing is just to trudge on with an ugly if/elsif construct for now.

  • Comment on Re^2: perldelta unclear on 'given's fate

Replies are listed 'Best First'.
Re^3: perldelta unclear on 'given's fate
by LanX (Cardinal) on Jan 04, 2014 at 10:35 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)

        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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2020-12-05 12:02 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (63 votes). Check out past polls.