Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Exiting eval via next: is that so bad?

by Hofmator (Curate)
on Aug 14, 2001 at 19:26 UTC ( #104801=note: print w/replies, xml ) Need Help??


in reply to Exiting eval via next: is that so bad?

Well, you should be aware of the fact that the next from within the eval jumps to the end of an enclosing loop, which might be a little bit surprising. Example:

my $var = 5; { eval {$var = 3; next}; $var = 4; } print $var; # 3 !!
The block after eval does not count as a loop block (same as do), so the loop commands next, redo and last work on the outer block. To make the eval block a loop block (so that you can redo it e.g.) double the braces like this:
eval {{ $i++; redo unless do_sth($i); }}

-- Hofmator

Replies are listed 'Best First'.
Re^2: Exiting eval via next: is that so bad?
by dd-b (Monk) on Sep 23, 2011 at 19:36 UTC

    Please forgive necromancy.

    In the case I just hit, I'm doing this:

    CLIENT: while (!exitflag) { eval { # Various things that might throw exceptions if (blah blah) { next CLIENT; } }; if ($@) { # Exception handling } }

    So, I'm not just exiting the eval, I'm exiting considerably more. AND I'm doing it by an explicit label, not implicitly, so I really shouldn't be confused about what I'm doing. I'd really prefer not to get this warning in the case when I'm using a loop label to exit a particular loop.

    (The big while loop is handling client connects as they come in, the eval inside it is to prevent unexpected errors from killing the whole server; they just abort the one connection. This is test code, the full version will fork of course, once it can handle the simple case.)

        In my mind, this wasn't a new question, it was a continuation of a discussion I'd found. Isn't the idea of searching first to avoid starting new questions when reasonably possible?

        Anyway, thanks for your suggestion of the "no warnings" pragma.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2020-09-26 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (142 votes). Check out past polls.

    Notices?