Tag: "perl"

Here are all posts that have been tagged with "perl".

There is also an Atom feed for this tag.

Perl - how to get the keys of a constant hashref

How do you get the keys of a constant hashref in perl?

$> use constant A => { 1 => 100, 2 => 200, 3 => 300}
()
$> A
{ 1 => 100, 2 => 200, 3 => 300 }

$> keys A
ERROR: Type of arg 1 to keys must be hash (not constant item) at (eval
13) line 3, at EOF
$> keys %A
()
$> (keys %{A})
Ambiguous use of %{A} resolved to %A at (eval 18) line 1, <IN> line 8.
()

To get the keys of a constant hashref you first need to learn that a constant is actually a function. You can leave off the parentheses to call a function in perl, but in this case you have to call the function to make perl happy.

$> (keys %{A()})
(1, 3, 2)
Illustration of a grassy knoll