Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Exiting subroutine via next

by geekphilosopher (Friar)
on Dec 07, 2006 at 05:56 UTC ( [id://588264]=note: print w/replies, xml ) Need Help??


in reply to Exiting subroutine via next

I'm not sure, but pg's comment in the referenced thread talks about how semantically a sub is a block in the scope of where it was called (with differences of course, such as not seeing lexicals of the containing scope). Since next just looks up the scope change for the nearest encasing block, which in this case is the foreach.

I don't really know how to explain the quoted documentation though. I just edited your function to include a return statement, and it still worked the same way.

Replies are listed 'Best First'.
Re^2: Exiting subroutine via next
by ikegami (Patriarch) on Dec 07, 2006 at 06:38 UTC

    I don't really know how to explain ["next" cannot be used to exit a block which returns a value such as "eval {}", "sub {}" or "do {}"...]

    It means next, last and redo ignore do blocks, eval blocks, etc. They will only cause loop blocks and bare blocks to repeat, exit, etc. It makes more sense when you read that same line in the documentation for last, since next doesn't cause a loop to exit.

    foreach my $var (1, 2, 3, "skip", 4) { eval { # This block is ignored. if ($var eq "skip") { next; } }; print $var; } print("\n"); foreach my $var (1, 2, 3, "skip", 4) { { # This block exits. if ($var eq "skip") { next; } } print $var; } print("\n");

    outputs

    1234 123skip4
      Ah, that makes sense. Thanks!
Re^2: Exiting subroutine via next
by ysth (Canon) on Dec 07, 2006 at 06:32 UTC
    I don't really know how to explain the quoted documentation though. I just edited your function to include a return statement, and it still worked the same way.
    The (poorly worded) documentation doesn't mean next won't leave that scope - it just means you can't use next to resume execution just after of the current eval {}, etc. when there's no loop in the eval {}, etc.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-03-29 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found