in reply to Re: Optimization, side-effects and wrong code
in thread Optimization, side-effects and wrong code

Your code creates an infinite loop, though, if $self->{$k}->{blahblah} never evaluates to true. (whereas the OP's falls through to "return undef;")

perl -Mstrict -lw my %self; $self{$_}{blahblah} = 0 for qw/1 2 3/; while (my $k = each %self || each %self) { print "testing:$k"; print "true for $k" and last if $self{$k}{blahblah}; } __OUTPUT__ testing:1 testing:3 testing:2 testing:1 testing:3 testing:2 testing:1 ... etc. ...

Replies are listed 'Best First'.
Re^2: Optimization, side-effects and wrong code
by bpphillips (Friar) on Oct 01, 2004 at 19:54 UTC
    agreed, I've added exists which causes it to work correctly (albeit a bit different test than the OP's). Could maybe even add a check to make sure our while loop doesn't run past scalar(keys %$self) but then we're back to using keys again... :-)