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
|