hash - Perl: Type of argument to keys on reference must be unblessed hashref or arrayref -


i have perl hash (from legacy code) unable print out keys.

  if (ref $val eq ref {}) {     print "keys: " . keys $val . "\n"; 

e.g. here's output get:

val: hash(0x7ff0898eda70) type of argument keys on reference must unblessed hashref or arrayref 

i've read type of argument keys on reference must unblessed hashref or arrayref not sure how apply in case.

is there way of fixing this?

====

update

i've tried:

    print "keys: " . keys %$val . "\n"; 

but still type of argument keys on reference must unblessed hashref or arrayref

update 2

i can see have key a_key i'm unable print out values. e.g. debugging carp::repl get:

$ print $val; 1$ hash(0x7fb1e0828f00)     $ print %$val; 1$ a_keyarray(0x7fb1e0828e28) $ print %$val{'a_key'} compile error: syntax error @ (eval 412) line 63, near "$val{" begin not safe after errors--compilation aborted @ (eval 412) line 63, <fin> line 6. $ print $val{'a_key'} use of uninitialized value in print @ (eval 413) line 63, <fin> line 7. 1 

update 3

using data::dumper in repl get:

$ print dumper( $val ); $var1 = {           'a_key' => [                      'long_value'                    ]         }; 1$ print dumper( %$val ); $var1 = 'a_key'; $var2 = [           'long_value'         ]; 1$ print %$val[1] compile error: syntax error @ (eval 450) line 63, near "$val[" begin not safe after errors--compilation aborted @ (eval 450) line 63, <fin> line 44. $ print %$val{'a_key'} compile error: syntax error @ (eval 451) line 63, near "$val{" begin not safe after errors--compilation aborted @ (eval 451) line 63, <fin> line 45. $ print $val[1]      use of uninitialized value in print @ (eval 452) line 63, <fin> line 46. 

first of all, have precedence problem. doing

keys($val . "\n") 

instead of

keys($val) . "\n" 

secondly, syntax keys[1] is

keys hash 

meaning need

keys(%$val) 

finally, calling keys in scalar context, returns number of keys in hash. call in list context keys of hash. example,

say "keys: ", join ", ", keys(%$val); 

  1. there few versions perl experimented allowing keys $ref, experiment discontinued in 5.24. avoid that!

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -