harp harp - 5 months ago 34
Perl Question

Perl: Accessing and handling objects in an array

I try to handle the following Array which was generated by

get_pool_member_object_status_($pool)


from http://search.cpan.org/~ltp/BigIP-iControl/lib/BigIP/iControl.pm#get_pool_member_object_status_($pool)

$VAR1 = [
[
bless( [
bless( {
'member' => bless( {
'address' => '192.168.100.141',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.142',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.143',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.144',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' )
], 'LocalLB::PoolMember::MemberObjectStatus[]' )
]
];


I couldn't figure out how this should work. I tried several examples from perldsc. The examples there mostly concern ARRAYS OF HASHES. But the above array is...? I don't even know what kind of array this is. Can someone point me in the right direction?

So for example how do I access the
"object_status"->"availability_status"
of a
"member"->"address"


Thx simbabque. This is the output I get from
Data::Printer:


[
[0] [
[0] LocalLB::PoolMember::MemberObjectStatus[] {
public methods (0)
private methods (0)
internals: [
[0] LocalLB::PoolMember::MemberObjectStatus,
[1] LocalLB::PoolMember::MemberObjectStatus,
[2] LocalLB::PoolMember::MemberObjectStatus,
[3] LocalLB::PoolMember::MemberObjectStatus
]
}
]


]

Answer

So you are trying to access the 'availability_status' and 'address'
I didn't traverse this whole object but with this code I am able to print the required fields.

#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;


my $obj = [
      [
        bless( [
                 bless( {
                          'member' => bless( {
                                             'address' => '192.168.100.141',
                                             'port' => '80'
                                           }, 'Common::IPPortDefinition' ),
                          'object_status' => bless( {
                                                    'availability_status' => 'AVAILABILITY_STATUS_GREEN',
                                                    'status_description' => 'Pool member is available',
                                                    'enabled_status' => 'ENABLED_STATUS_ENABLED'
                                                  }, 'LocalLB::ObjectStatus' )
                        }, 'LocalLB::PoolMember::MemberObjectStatus' ),
                 bless( {
                          'member' => bless( {
                                             'address' => '192.168.100.142',
                                             'port' => '80'
                                           }, 'Common::IPPortDefinition' ),
                          'object_status' => bless( {
                                                    'availability_status' => 'AVAILABILITY_STATUS_GREEN',
                                                    'status_description' => 'Pool member is available',
                                                    'enabled_status' => 'ENABLED_STATUS_ENABLED'
                                                  }, 'LocalLB::ObjectStatus' )
                        }, 'LocalLB::PoolMember::MemberObjectStatus' ),
                 bless( {
                          'member' => bless( {
                                             'address' => '192.168.100.143',
                                             'port' => '80'
                                           }, 'Common::IPPortDefinition' ),
                          'object_status' => bless( {
                                                    'availability_status' => 'AVAILABILITY_STATUS_GREEN',
                                                    'status_description' => 'Pool member is available',
                                                    'enabled_status' => 'ENABLED_STATUS_ENABLED'
                                                  }, 'LocalLB::ObjectStatus' )
                        }, 'LocalLB::PoolMember::MemberObjectStatus' ),
                 bless( {
                          'member' => bless( {
                                             'address' => '192.168.100.144',
                                             'port' => '80'
                                           }, 'Common::IPPortDefinition' ),
                          'object_status' => bless( {
                                                    'availability_status' => 'AVAILABILITY_STATUS_GREEN',
                                                    'status_description' => 'Pool member is available',
                                                    'enabled_status' => 'ENABLED_STATUS_ENABLED'
                                                  }, 'LocalLB::ObjectStatus' )
                        }, 'LocalLB::PoolMember::MemberObjectStatus' )
               ], 'LocalLB::PoolMember::MemberObjectStatus[]' )
      ]
    ];

print Dumper($obj->[0]->[0]->[0]->{member}->{address});
print Dumper($obj->[0]->[0]->[0]->{object_status}->{availability_status});
print Dumper($obj->[0]->[0]->[1]->{member}->{address});
print Dumper($obj->[0]->[0]->[1]->{object_status}->{availability_status});
print Dumper($obj->[0]->[0]->[2]->{member}->{address});
print Dumper($obj->[0]->[0]->[2]->{object_status}->{availability_status});
print Dumper($obj->[0]->[0]->[3]->{member}->{address});
print Dumper($obj->[0]->[0]->[3]->{object_status}->{availability_status});

Output:

$VAR1 = '192.168.100.141';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
$VAR1 = '192.168.100.142';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
$VAR1 = '192.168.100.143';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
$VAR1 = '192.168.100.144';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';

Hope this helps and now you should be able to traverse this data structure with loops.