in reply to This "each" goes to endless loop each time...

The problem is that e() always returns a different hash reference, for which each always returns the "first" pair.

You can work around that by memoizing e(), but I don't know if that's applicable for what you want to do:

use warnings; use strict; use Memoize; memoize('e'); sub e() { my %result=(A=>1,B=>2); wantarray ? %result : \%result; } my %env=e; $|=1; while (my ($var,$val) = each %env) { print "(hash) $var=$val\n"; } while (my ($var,$val) = each %{ e() }) { print "(ref) $var=$val\n"; }

P.S. Please don't use &e to call a sub, use e() instead - you might get surprising results otherwise.

Update: or simply assign the ref to a temporary variable: my $e = e(); while (my ($k, $v) = each %$e){...}

Replies are listed 'Best First'.
Re^2: This "each" goes to endless loop each time...
by rovf (Priest) on Jun 19, 2008 at 10:57 UTC
    The problem is that e() always returns a different hash reference
    Ah, that's it! I understand.
    Please don't use &e to call a sub, use e() instead - you might get surprising results otherwise.
    Agreed. I had first %{ e }, but this was (of course) interpreted as hash variable %e, so I replaced it without much thinking by %{&e}. You are absolutely right that %{e()} would have been the better choice.
    -- 
    Ronald Fischer <ynnor@mm.st>