Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by JavaFan (Canon)
on Mar 04, 2010 at 16:56 UTC ( [id://826742]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Understanding the benefit of Given/When ...
in thread Understanding the benefit of Given/When ...

Which means you can have your continue block - just spell given as for - you'd save 2 keystrokes as well.

Replies are listed 'Best First'.
Re^6: Understanding the benefit of Given/When ...
by LanX (Saint) on Mar 04, 2010 at 17:12 UTC
    which should also mean that one can mix when andif/unless within a for-block -one would save keystroke for continue and next statements! 8)

    Cheers Rolf

      You can put whatever code you want between various when blocks. You can also have more than one default - a default is just a when with a condition that's always true:
      my @test = qw [abc def ghi xyz jkl mno]; for (@test) { say "Testing '$_'"; when (/abc/) {say " Got 'abc'"; continue} say " After when /abc/"; when (/def/) {say " Got 'def'"; next;} say " After when /def/"; when (/ghi/) {say " Got 'ghi'";} say " After when /ghi/"; when (/jkl/) {say " Got 'jkl'"; last;} say " After when /jkl/"; default {say " Default 1"; continue} say " After first default"; default {say " Default 2";} say " After second default" } continue { say " Continue for '$_'"; } __END__ Testing 'abc' Got 'abc' After when /abc/ After when /def/ After when /ghi/ After when /jkl/ Default 1 After first default Default 2 Continue for 'abc' Testing 'def' After when /abc/ Got 'def' Continue for 'def' Testing 'ghi' After when /abc/ After when /def/ Got 'ghi' Continue for 'ghi' Testing 'xyz' After when /abc/ After when /def/ After when /ghi/ After when /jkl/ Default 1 After first default Default 2 Continue for 'xyz' Testing 'jkl' After when /abc/ After when /def/ After when /ghi/ Got 'jkl'
        What I meant was that phrase in the docs to for/when:

        On exit from the when  block, there is an implicit next.

        so you can use a simple if where you wanted to write  when () { ...; continue} and vice versa you can write when where you wanted to write if () {...; next} ! (as long you don't need the implicit smart match)

        In other words: This next in your code

        ... say " After when /abc/"; when (/def/) {say " Got 'def'"; next;} say " After when /def/"; ...
        is a tautology...

        Cheers Rolf

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-20 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found