in reply to Dereference a hash constant

Try it this way:

use constant foo => { a .. z };; [0] Perl> print "$k : $v" while ($k,$v) = each %{ foo() };; w : x e : f a : b m : n s : t y : z u : v c : d k : l q : r g : h i : j o : p

Or this:

[0] Perl> print "$k : $v" while ($k,$v) = each %{ +foo };; w : x e : f a : b m : n s : t y : z u : v c : d k : l q : r g : h i : j o : p

The 'trick' is to ensure that foo is parsed as a sub not a string or bareword.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP PCW It is as I've been saying!(Audio until 20090817)

Replies are listed 'Best First'.
Re^2: Dereference a hash constant
by wrinkles (Pilgrim) on Oct 22, 2009 at 06:45 UTC
    By the same reasoning:
    perl -e 'use constant foo => { a .. z }; print "$k : $v \n" while ($k +,$v) = each %{&foo};'
    And this works, but misses the point I guess:
    perl -e 'use constant foo => { a..z }; *foo_h=foo; print "$k : $v \n" +while ($k,$v) = each %foo_h;'
Re^2: Dereference a hash constant
by Anonymous Monk on Oct 22, 2009 at 00:45 UTC
    Excellent solution. Thanks!!