in reply to This "each" goes to endless loop each time...
The problem is evaluating e for each iteration of the while loop. If you make the ref assignment outside the loop things work as expected:
use warnings; use strict; my %env= %{e ()}; $|=1; while (my ($var, $val) = each %env) { print "(hash) $var=$val\n"; } my $ref = e (); while (my ($var,$val) = each %$ref) { print "(ref) $var=$val\n"; } sub e { my %result=(A=>1,B=>2); wantarray ? each %result : \%result; }
Prints:
(hash) A=1 (hash) B=2 (ref) A=1 (ref) B=2
As an aside: prototyping subs in Perl is only very occasionally required and should not be done as a matter of course. Likewise, calling subs using & is magical and should only be used when required.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: This "each" goes to endless loop each time...
by rovf (Priest) on Jun 19, 2008 at 11:27 UTC | |
by GrandFather (Saint) on Jun 19, 2008 at 20:03 UTC | |
by rovf (Priest) on Jun 20, 2008 at 07:50 UTC |