我的代码中有一个哈希数组,从Config :: General模块导入,该模块包含多级子哈希数组。我正在尝试访问哈希数组子项中的变量,但它不断抱怨变量未定义。
如果我尝试将它们预先定义为哈希数组,则会抱怨内容未定义。我显然做了一些愚蠢的事,你能指出什么吗?
my $key;
my $val;
# allDevices should be a hash array istelf
my $allDevices = $cfgfile{'remote_hosts'}{'category:switch'}{'subcategory:brocade'};
while (($key, $val) = each $allDevices)
{
# This proves that the $val is a hash array also
INFO(" - $key => $val\n");
# This bit works ok
my @devDetails = split(/:/, $key);
my $thisDevice = $val;
my $thisDeviceType = $devDetails[0];
my $thisDeviceName = $devDetails[1];
INFO(" - Device Name: $thisDeviceName\n");
# This is where it complains - %thisDevice requires explicit package name
my $thisDeviceConnectProto = $thisDevice{'remote_device_connect_protocol'};
my $thisDeviceConnectIP = $thisDevice{'remote_device_connect_ipaddress'};
my $thisDeviceConnectUser = $thisDevice{'remote_device_connect_username'};
my $thisDeviceConnectPass = $thisDevice{'remote_device_connect_password'};
#########################################################################
# For each command specified in cli file #
#########################################################################
# CLI "for" loop
}
提前致谢
答案 0 :(得分:3)
请勿忘记代码中的use strict
和use warnings
。它们在编译阶段对您有很大帮助。例如:
# file: test.pl
use strict;
use warnings;
my $hash = {'one'=>1}; #hash reference
print $hash{'one'}, "\n"; #error: this refers to %hash, and it doesn't exists
print $hash->{'one'}, "\n"; #ok
perl解释器显示以下错误消息:
Global symbol "%hash" requires explicit package name at test.pl line 7
答案 1 :(得分:2)
您没有声明%thisDevice
哈希,$thisDevice
hashref完全是另一个变量。
更改
$thisDevice{'remote_device_connect_protocol'};
到
$thisDevice->{'remote_device_connect_protocol'};