Perl:数组取消引用
我试图弄清楚如何取消引用数组中的值,但已经陷入了死胡同,我试图研究这个问题,但需要一些帮助。
我正在从 infoblox 数据库中获取一些数据,并尝试在结果中搜索以查找主机条目的 mac 地址,数据以由以下代码生成的哈希形式存在于数组中
use strict;
use Data::Dumper;
my @results = $session->get(
object => "Infoblox::DNS::Host",
name => "test.com.au",
ipv4addrs => ".*.",
view => "external"
);
:使用“$_->ipv4addrs”的高级数据结构,然后使用以下代码循环输出:
foreach (@results) {
my @search = $_->ipv4addrs;
foreach (@search) {
print Dumper($_) . "\n";
}
}
使用 Data::Dumper 打印以下输出:
$VAR1 = [
bless( {
'network' => '111.111.111.0/25',
'options' => [],
'dynamic' => 'false',
'__version' => '4.2r5-5-68691',
'VIEW_FUNCTION' => {
'remove' => '.com.infoblox.perl_api.Infoblox.DHCP.remove_fixed_address',
'search' => '.com.infoblox.perl_api.Infoblox.DHCP.search_fixed_address',
'add' => '.com.infoblox.perl_api.Infoblox.DHCP.insert_fixed_address',
'add_using_template' => '.com.infoblox.perl_api.Infoblox.DHCP.insert_fixed_address_using_template',
'get' => '.com.infoblox.perl_api.Infoblox.DHCP.get_fixed_address',
'modify' => '.com.infoblox.perl_api.Infoblox.DHCP.modify_fixed_address'
},
'ipv4addr' => '111.111.111.111',
'match_client' => 'MAC',
'mac' => '00:11:00:11:00:11',
'disable' => 'false',
'__type' => '.com.infoblox.perl_api.Infoblox.DHCP.FixedAddr'
}, 'Infoblox::DHCP::FixedAddr' )
];
但是如果我尝试使用以下命令调用引用当我在“foreach (@searh)”循环中使用“$_->mac”时,出现错误:
"Can't call method "mac" on unblessed reference at ./connect_test.pl line nn."
我的编码技能此时失败,任何信息或方向将不胜感激。
I'm trying to figure out how to de-reference a value in an array but have hit a dead end, I've attempted to research the issue but need some help.
I'm grabbing some data from an infoblox database and trying to search within the results to find the mac address of a host entry, the data exists in the array as a hash generate by the following code:
use strict;
use Data::Dumper;
my @results = $session->get(
object => "Infoblox::DNS::Host",
name => "test.com.au",
ipv4addrs => ".*.",
view => "external"
);
I'm de-referencing at a high level of the data structure using '$_->ipv4addrs' then looping over the output using the following code:
foreach (@results) {
my @search = $_->ipv4addrs;
foreach (@search) {
print Dumper($_) . "\n";
}
}
which prints the following output using Data::Dumper:
$VAR1 = [
bless( {
'network' => '111.111.111.0/25',
'options' => [],
'dynamic' => 'false',
'__version' => '4.2r5-5-68691',
'VIEW_FUNCTION' => {
'remove' => '.com.infoblox.perl_api.Infoblox.DHCP.remove_fixed_address',
'search' => '.com.infoblox.perl_api.Infoblox.DHCP.search_fixed_address',
'add' => '.com.infoblox.perl_api.Infoblox.DHCP.insert_fixed_address',
'add_using_template' => '.com.infoblox.perl_api.Infoblox.DHCP.insert_fixed_address_using_template',
'get' => '.com.infoblox.perl_api.Infoblox.DHCP.get_fixed_address',
'modify' => '.com.infoblox.perl_api.Infoblox.DHCP.modify_fixed_address'
},
'ipv4addr' => '111.111.111.111',
'match_client' => 'MAC',
'mac' => '00:11:00:11:00:11',
'disable' => 'false',
'__type' => '.com.infoblox.perl_api.Infoblox.DHCP.FixedAddr'
}, 'Infoblox::DHCP::FixedAddr' )
];
But if I try and call a reference using '$_->mac' within the 'foreach (@searh)' loop, I get an error:
"Can't call method "mac" on unblessed reference at ./connect_test.pl line nn."
My coding skills fail at this point, any info or direction would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数组中的第一个元素是受祝福的引用。
您可以使用
->
运算符直接访问数组引用中的元素(如上所述),或者将它们完全取消引用到列表:@list = @{$array_reference}
。The first element in the array is the blessed reference.
You can directly access elements in array references with the
->
operator (as above) or completely dereference them to a list:@list = @{$array_reference}
.@search
中存储的每个项目本身就是一个 arrayref。Each item stored in
@search
is an arrayref itself.