Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

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


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

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

See provided code examples.

update

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)


Comment on Re^5: perldelta unclear on 'given's fate
Select or Download Code
Re^6: perldelta unclear on 'given's fate
by Tommy (Chaplain) on Jan 05, 2014 at 21:15 UTC

    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 @_ };

    Tommy
    A mistake can be valuable or costly, depending on how faithfully you pursue correction
Re^6: perldelta unclear on 'given's fate
by Bloodnok (Vicar) on Jan 06, 2014 at 14:18 UTC
    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 :-))

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2015-07-02 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (25 votes), past polls