显示对象数组中的数据
我正在尝试显示使用另一家公司的 API 获得的对象数组中的数据,但是当我尝试使用 foreach 循环时出现错误。
我正在使用 Dumper 显示数组中的所有内容。
print Dumper($object);
Dumper 的部分输出:
'enable_dha_thresholds' => 'false', 'members' => [ bless( { 'ipv4addr' => '192.168.1.67', 'name' => 'name.something.com' }, 'Something::Network::Member' ), bless( { 'ipv4addr' => '192.168.1.68', 'name' => 'name.something.com' }, 'Something::Network::Member' ) ], 'comment' => 'This is a comment',
我正在尝试提取似乎是双数组的“成员”:
//this works
print $members->enable_dha_thresholds();
//this works
print $members[0][0]->ipv4addr;
//does not work
foreach my $member ($members[0]){
print "IP". $member->ipv4addr()."\n";
}
我收到此错误: 无法在 ./script.pl 第 12 行的 unblessed 引用上调用方法“ipv4addr”。
我不确定我是否完全理解 Perl 中的“blessed”与“unblessed”,因为我是该语言的新手。
I'm trying to display data from an array of objects obtained using another company's API, but I am getting errors when I attempt to using a foreach loop.
I'm using Dumper to display everything in the array.
print Dumper($object);
Partial output from Dumper:
'enable_dha_thresholds' => 'false', 'members' => [ bless( { 'ipv4addr' => '192.168.1.67', 'name' => 'name.something.com' }, 'Something::Network::Member' ), bless( { 'ipv4addr' => '192.168.1.68', 'name' => 'name.something.com' }, 'Something::Network::Member' ) ], 'comment' => 'This is a comment',
I'm trying to extract the "members" which appears to be a double array:
//this works
print $members->enable_dha_thresholds();
//this works
print $members[0][0]->ipv4addr;
//does not work
foreach my $member ($members[0]){
print "IP". $member->ipv4addr()."\n";
}
I receive this error:
Can't call method "ipv4addr" on unblessed reference at ./script.pl line 12.
I'm not sure I entirely understand "blessed" vs "unblessed" in Perl since I am new to the language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此 $members[0] 是一个数组引用。
您必须取消引用该数组:
引用“未受祝福的引用”的错误告诉您没有使用对象;相反,您提供了一个数组引用,它不一样:)
HTH,
保罗
so $members[0] is an array reference.
You have to dereference the array:
The error refering to an "unblessed reference" tells you you aren't using an object; rather you provide an array-reference, which isn't the same :)
HTH,
Paul
这是“数组引用”与“数组”的问题。
$members[0]
是一个数组引用; foreach 运算符适用于数组(或迂腐的列表)。您可能想说要迭代
$members[0]
引用的元素。语法很棘手,在使用这些东西的过程中您可能会犯更多错误。帮助您加快速度的相关文档位于
perlref
(或perlreftut
),perllol
,以及perldsc
和perlobj
。顺便说一句,“有福”意味着引用“知道”它是什么类型的对象以及它应该查找哪个包以查看它可以运行哪些方法。当您收到“未受祝福的引用”警告或错误时,通常意味着您在需要对象的地方传递了一些不是对象的东西 - 在本例中,
$members[0]
是未受祝福的引用而您打算传递受祝福的引用$members[0][0]
、$members[0][1]
等。It's an issue of "array reference" vs. "array".
$members[0]
is an array reference; theforeach
operator works with arrays (or lists, to be pedantic). You will want to sayto iterate over the elements that
$members[0]
refers to.The syntax is tricky, and you will probably make a few more mistakes along the way with this stuff. The relevant docs to get you up to speed are in
perlref
(orperlreftut
),perllol
, and alsoperldsc
andperlobj
."blessed" by the way, means that a reference "knows" what kind of object it is and what package it should look in to see what methods it can run. When you get an "unblessed reference" warning or error, that usually means you passed something that was not an object somewhere that expected an object -- in this case,
$members[0]
is the unblessed reference while you intended to pass the blessed references$members[0][0]
,$members[0][1]
, etc.