The next, last and redo keywords are designed for jumping around within a loop structure. However, if other control structures (like subs and evals) are used within a loop, those three keywords can have the side-effect of jumping out of the other control control structures. This can sometimes be counter-intuitive, so if warnings are enabled, Perl will warn you about it.

use strict; use warnings; # We're defining the "Foo" package inline here, but # imagine it's actually defined in a different file and # loaded with `use Foo`; { package Foo; sub foo { my $val = shift; # what on earth is this following line doing? next if $val < 3; } } # imagine that there are hundreds of other lines here for my $i (0..10) { Foo::foo($i); # why don't 0, 1 and 2 get printed?? print $i, "\n"; }

The thing with the warnings pragma, is you shouldn't take the warnings it generates as absolute "do not ever do this" prohibitions. Instead, decide for yourself, on a case by case basis, about whether to rewrite the code to something better, thereby getting rid of the warning, or maybe deciding that actually your code is fine, and you can just put a no warnings "exiting" somewhere to disable the loop.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

In reply to Re: perl exit eval via next in a loop by tobyink
in thread perl exit eval via next in a loop by david2008

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.