Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Understanding the benefit of Given/When ...

by JavaFan (Canon)
on Mar 04, 2010 at 15:14 UTC ( #826721=note: print w/ replies, xml ) Need Help??


in reply to Understanding the benefit of Given/When ...

I don't understand why you think a 'continue-block' is missing from given/when. 'continue-block' is associated with loops; give/when isn't a loop. There's no 'continue-block' for if/elsif/else either. But given that empty blocks are loops, you could write:

{ given ($foo) { when (/bar/) {...} when (/baz/) {...} } } continue { ... always executed ... }
But I don't see what that buys you over:
given ($foo) { when (/bar/) {...} when (/baz/) {...} } ... always executed ...
So, why even want a continue block on given/when? It's not needed, and isn't consistent.
Nice ...but how can I take profit from this smart match if the parameter passed into given can only be a scalar?
To give one example:
my $foo = "bar"; my @foo = qw [foo bar baz]; given ($foo) { when (@foo) {say "Match"} }
will print Match.


Comment on Re: Understanding the benefit of Given/When ...
Select or Download Code
Re^2: Understanding the benefit of Given/When ...
by LanX (Canon) on Mar 04, 2010 at 16:06 UTC
    So, why even want a continue block on given/when? It's not needed, and isn't consistent.

    I see what you mean, maybe I'm still too attached to the concepts of the workaround...(which is easier to understand for me)

    Anyway I think you have a plus because you can use last for leaving without executing the continue block.

    # using continue-block my @res; for (@test){ push (@res, "abc") if (/abc/); push (@res, "def") && last if (/def/); push (@res, "xyz") && next if (/xyz/); push (@res, "default"); } continue { print "\nFOR/CONT($_):",@res; @res=(); }

    OUTPUT:

    === For/Continue FOR/CONT(abc): abc default

    Cheers Rolf

      my @test = qw[abc def ghi xyz]; my @res; for (@test) { when (/abc/) {push @res, "abc"; continue} when (/def/) {push @res, "def"; last} when (/xyz/) {push @res, "xyz"; next} default {push @res, "default"} } continue { print "FOR/CONT($_): @res\n"; @res = (); } __END__ FOR/CONT(abc): abc default
        Sure that works, because one can also combine For/When!

        Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2015-07-03 23:59 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 (57 votes), past polls