Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

print a hash reference

by vedas (Novice)
on Jan 10, 2019 at 18:51 UTC ( #1228332=perlquestion: print w/replies, xml ) Need Help??

vedas has asked for the wisdom of the Perl Monks concerning the following question:

I'm still learning perl. It trying to pull IPs from a infoblox system using their API. According to their API Documentation the "get" method:

"Return an array in list context containing all matching objects. For failures and objects not found, it returns an empty array in list context and undefined in scalar context. When an operation fails, the error code and error message are reported in the status_code() and status_detail() attributes."

So, I'm expecting to get an array of objects (IPs) back after call the get method, however this is not what I am getting. Below is the relevant code and output

CODE : : : my @retrieved_objs = $session->get( object => "Infoblox::DNS::Record::A", name => "" ); print "retrieved_objs[0]: " . $retrieved_objs[0] . "\n"; print "retrieved_objs[1]: " . $retrieved_objs[1] . "\n"; : : :
OUTPUT Infoblox::DNS::Record::A=HASH(0x55d0d1517aa0) retrieved_objs[0]: Infoblox::DNS::Record::A=HASH(0x55d0d1517aa0) retrieved_objs[1]:

It appears to be giving the memory reference to hash, I was expecting ip objects. Any one have any thoughts on where I'm going wrong?

Replies are listed 'Best First'.
Re: print a hash reference
by haukex (Chancellor) on Jan 10, 2019 at 19:46 UTC

    Just to clarify this part in addition to trippledubs's answer:

    It appears to be giving the memory reference to hash, I was expecting ip objects.

    The output "Infoblox::DNS::Record::A=HASH(0x55d0d1517aa0)" tells you that it is in fact an object of type Infoblox::DNS::Record::A, and it happens to be implemented as a hash internally, hence the HASH(0x...) part - but normally you shouldn't try to use it as a hash reference, because this would probably break the object's encapsulation. This kind of output is the default behavior when you try to use a Perl object as a string. Some Perl objects support "overloaded stringification", which means that when you try to use them as a string, they will return a useful string, but Infoblox::DNS::Record::A apparently doesn't implement that, which is why you have to use the methods on the object to get its properties.

Re: print a hash reference
by trippledubs (Deacon) on Jan 10, 2019 at 19:22 UTC

    You do have an array of objects, but an object is not an IP. @retrieved_objs is a list of Infoblox::DNS::Record::A objects.

    Infoblox::DNS::Record::A provides you some methods to do what you want.

    print $_->ipv4addr(),"\n" for (@retrieved_objs);
      Thanks guys, I think I understand what is going on now. I appreciate your assistance.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1228332]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2019-10-16 16:40 GMT
Find Nodes?
    Voting Booth?