in reply to Found bug: while()

It means you've already started iterating over the hash.

sub _split_options { my ($opts) = @_; while (my ($k, $v) = each %$opts) { print "$k = $v\n"; } print("\n"); while (my ($k, $v) = each %$opts) { print "$k = $v\n"; } } my %opts = qw( a 1 b 2 c 3 d 4 ); scalar each %opts; _split_options(\%opts);
a = 1 b = 2 d = 4 c = 3 a = 1 b = 2 d = 4

So no, it's not a bug in while, or even with each. It's a bug in your use of each. Whenever you use each, you should reset the iterator first. You can do that efficiently by calling keys in void context first.

sub _split_options { my ($opts) = @_; keys %$opts; # Reset iterator while (my ($k, $v) = each %$opts) { print "$k = $v\n"; } print("\n"); #keys %$opts; # Previous loop reset iterator while (my ($k, $v) = each %$opts) { print "$k = $v\n"; } } my %opts = qw( a 1 b 2 c 3 d 4 ); scalar each %opts; _split_options(\%opts);
c = 3 a = 1 b = 2 d = 4 c = 3 a = 1 b = 2 d = 4

Replies are listed 'Best First'.
Re^2: Found bug: while()
by Anonymous Monk on Oct 13, 2009 at 03:49 UTC
    Ok :-)

    Thank you for your fast feedback!