我有一個Perl散列(來自一些遺留代碼),但無法打印出鍵。Perl:引用鍵參數的類型必須是未散列的hashref或arrayref
if (ref $val eq ref {}) {
print "Keys: " . keys $val . "\n";
例如,這裏的輸出我得到:
VAL: HASH(0x7ff0898eda70)
Type of argument to keys on reference must be unblessed hashref or arrayref
我讀過這Type of argument to keys on reference must be unblessed hashref or arrayref,但不知道如何在這種情況下應用它。
有沒有辦法解決這個問題?
====
UPDATE
我也試過:
print "Keys: " . keys %$val . "\n";
但仍獲得Type of argument to keys on reference must be unblessed hashref or arrayref
更新2
我可以看到我有鍵a_key
但我無法打印出它的值。例如。與Carp::REPL
調試,我得到:
$ print $val;
1$ HASH(0x7fb1e0828f00)
$ print %$val;
1$ a_keyARRAY(0x7fb1e0828e28)
$ print %$val{'a_key'}
Compile error: syntax error at (eval 412) line 63, near "$val{"
BEGIN not safe after errors--compilation aborted at (eval 412) line 63, <FIN> line 6.
$ print $val{'a_key'}
Use of uninitialized value in print at (eval 413) line 63, <FIN> line 7.
1
更新3
在REPL我得到使用Data::Dumper
:
$ 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 at (eval 450) line 63, near "$val["
BEGIN not safe after errors--compilation aborted at (eval 450) line 63, <FIN> line 44.
$ print %$val{'a_key'}
Compile error: syntax error at (eval 451) line 63, near "$val{"
BEGIN not safe after errors--compilation aborted at (eval 451) line 63, <FIN> line 45.
$ print $val[1]
Use of uninitialized value in print at (eval 452) line 63, <FIN> line 46.
@ikegami你能詳細點嗎? – Snowcrash
我剛剛重新運行代碼並得到相同的錯誤消息。 – Snowcrash
'.'設置標量上下文,但是當你修復這個'print'鍵時:「,鍵(%$ href),」\ n「;'你會將所有鍵連接成一個單詞。您可以爲鍵%$ href;執行'print「$ _」。打印「\ n」;',或每行一個'說鍵%$ href;' – zdim