develooper Front page | perl.perl5.porters | Postings from July 2001

RE: [ID 20010720.006] each() fails when passed a subroutine that returns a hashref

Thread Previous
From:
Wilson, Doug
Date:
July 20, 2001 12:27
Subject:
RE: [ID 20010720.006] each() fails when passed a subroutine that returns a hashref
Message ID:
35A280DF784CD411A06B0008C7B130ADB5508C@sdex04.sd.intuit.com

> When each() is passed a subroutine that returns a reference 
> to a hash, it
> causes an infinite loop.  Tested on 5.00503 and 5.6.1.
> sub makeref {
> 	my $foo = {
> 		bar => [1, 2, 3],
> 		blah => [qw/a b c/],
> 	};
> 	return $foo;
> }
> while (my ($k, $v) = each %{makeref()}) {
> 	print "$k - @$v\n";
> }

Not a bug, IMHO.

makeref is returning a reference to a brand new hash
with a brand new iterator. And its the hash that
has an iterator assigned to it, not the call to each().
each() cannot know that a hash with the same value will
be returned.

Imagine doing this:

my $href1 = { aa => 1, cc => 3 };
my $href2 = { bb => 3, dd => 4 };
my $flip_flop;
sub return_ref {
 $flip_flop = ! $flip_flop;
 $flip_flop ? $href1 : $href2;
}

while (my ($k, $v) = each %{return_ref()}) {
 print "$k $v\n";
}

'Fixing' the 'bug' would break this example.

----
Doug

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About