Reputation: 109
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.
Upvotes: 2
Views: 1838
Reputation: 37136
Each item stored in @search
is an arrayref itself.
# If arrayref contains only one | # If multiple objects expected
# Infoblox::DHCP::FixedAddr object | # inside @search
|
|
foreach ( @search ) { | foreach my $item ( @search ) {
|
my $obj = shift @$_; | foreach my $obj ( @$item ) {
my $mac = $obj->mac; |
} | my $mac = $obj->mac;
| }
| }
Upvotes: 1
Reputation: 34612
The first element in the array is the blessed reference.
$_->[0]->mac
You can directly access elements in array references with the ->
operator (as above) or completely dereference them to a list: @list = @{$array_reference}
.
Upvotes: 2