user-pic

martin.j.evans

  • Commented on Do not use each
    I only hit a problem with each recently (see http://www.martin-evans.me.uk/node/159). Using a hash in list context will use the iterator internally so simply assigning the hash you are iterating over to another hash will cause an infinite loop. I now...
Subscribe to feed Recent Actions from martin.j.evans

  • Timm Murray commented on Do not use each

    I've also seen situations where each was called in a loop that short-circuits:

    while( my ($key, $val) = each %hash ) {
        last if $key =~ /foo/;
    }
    

    Then later on, each is used again without resetting the internal state, even though the programmer expected to iterate over the entire hash. With a hash stored globally (yes, this is bad in itself, but it happens) in a persistent environment like mod_perl, this can even happen across different requests in the same process.

  • Tim Bunce commented on Do not use each

    I wonder if there's some way to make the each op warn if the iterator isn't where it's expected to be.

  • E. Choroba commented on Do not use each

    Unfortunately, glob is similarly broken:


    perl -E 'for my $x (qw(* )) { print "$x: ", scalar glob($x), "\n"}'

  • E. Choroba commented on Do not use each

    Sorry, should have been:

    perl -E 'for my $x (qw(* < >)) { print "$x: ", scalar glob($x), "\n"}'

Subscribe to feed Responses to Comments from martin.j.evans

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl with a graphic design donated by Six Apart, Ltd.