in reply to Optimization, side-effects and wrong code

you could change your while loop to do:
while(my $k = each %$self || each %$self){ return $k if(exists $self->{$k}->{blahblah}); }
and avoid the keys %$self thing altogether. In my 5 minutes of playing with it, it appears to work
Update:
benizi pointed out the loop becomes endless if $self->{$k}->{blahblah} doesn't return a true value. This is solved by changing it to use exists

Replies are listed 'Best First'.
Re: Optimization, side-effects and wrong code
by benizi (Hermit) on Oct 01, 2004 at 18:59 UTC

    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. ...

      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... :-)