Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

access object values

by averlon (Sexton)
on Jun 05, 2023 at 09:43 UTC ( #11152644=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am strugling to access the values of an object.
Net::DNS::Packet=HASH(0x560a2c51be98) 'additional' => ARRAY(0x560a2c643678) empty array 'answer' => ARRAY(0x560a2c4ea600) 0 Net::DNS::RR::PTR=HASH(0x560a2c650780) 'class' => 1 'owner' => Net::DNS::DomainName1035=HASH(0x560a2c6504f8) 'label' => ARRAY(0x560a2c6506d8) empty array 'origin' => Net::DNS::DomainName=HASH(0x560a2c6507b0) 'label' => ARRAY(0x560a2c650798) 0 20 1 171 2 191 3 185 4 'in-addr' 5 'arpa' 'ptrdname' => Net::DNS::DomainName1035=HASH(0x560a2c650960) 'label' => ARRAY(0x560a2c650948) 0 20 1 'bl' 2 'bot' 3 'semrush' 4 'com' 'rdlength' => 23 'ttl' => 273 'type' => 12 'authority' => ARRAY(0x560a2c63e350) empty array 'count' => ARRAY(0x560a2c6503c0) 0 1 1 1 2 0 3 0 'id' => 31152 'question' => ARRAY(0x560a2c51c270) 0 Net::DNS::Question=HASH(0x560a2c6438e8) 'qclass' => 1 'qname' => Net::DNS::DomainName1035=HASH(0x560a2c63edd0) 'label' => ARRAY(0x560a2c650480) 0 20 1 171 2 191 3 185 4 'in-addr' 5 'arpa' 'qtype' => 12 'replyfrom' => '127.0.0.1' 'replysize' => 80 'status' => 33152 'xedns' => Net::DNS::RR::OPT=HASH(0x560a2c650a50) 'owner' => Net::DNS::DomainName1035=HASH(0x560a2c4c81c0) 'label' => ARRAY(0x560a2c4c8160) empty array 'type' => 41
As you might imagine, I look for a way to grab answer-ptrdname-label. I have tried:
@$obj{answer)
which at least gives me a part of the object. The same as the method $obj->answer(). But then I am stuck! Help appreciated!
Regards Kallewirsch

Replies are listed 'Best First'.
Re: access object values
by haj (Vicar) on Jun 05, 2023 at 10:44 UTC

    The best way to drill that down is to follow the classes of the objects.

    1. A Net::DNS::Packet object has the object method answer (You've already figured that out).
    2. According to the docs, that answer is a list of Net::DNS::RR objects. These have a type method, which will provide you with 'PTR' (I'm assuming that from your dump).
    3. Finally, Net::DNS::RR::PTR objects have a method ptrdname, which provides you with a the string '20.bl.bot.semrush.com'. You get the same result from calling $answer->rdstring, which works for all types of RR records.

    In code:

    use 5.020; my @answers = $obj->answer; for my $answer (@answers) { if ($answer->type eq 'PTR') { $ptrdname = $answer->ptrdname; } } say $ptrdname;
      Hi haj, thanks - I will try that way!

      Problem is, that I did not understand the docu in the right way that this is a kind of drill down through different objects and methods.

      Beside this, this also gives me the array. But your solution is much better!
      $obj{answer}->[0]{ptrdname}{label}
      Regards Kallewirsch
Re: access object values
by Fletch (Bishop) on Jun 05, 2023 at 09:59 UTC

    Your approach is breaking encapsulation rather than using the objects returned. The answer method returns an array of Net::DNS::RR::PTR instances; you should call methods on the instances in that arrayref rather than pulling values out directly (e.g $result->answer->[0]->ptrdname->name (untested, but hopefully gives the right direction)).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: access object values
by 1nickt (Canon) on Jun 05, 2023 at 10:03 UTC

    Hi, I think you want

    my $aref = $obj->answer->[0]->ptrdname->label;
    To get the second label:
    my $aref = $obj->answer->[0]->ptrdname->label->[1];

    Hope this helps!


    The way forward always starts with a minimal test.
Re: access object values
by kcott (Archbishop) on Jun 05, 2023 at 13:54 UTC

    G'day Kallewirsch,

    There are a number of modules that will give you a snapshot of an object. Here's a selection (all of these are configurable; see doco for details):

    Here's a quick example of each:

    $ perl -E ' use strict; use warnings; use Net::DNS::Packet; my $obj = Net::DNS::Packet::->new(qw{example.com MX IN}); say "\n*** Data::Dumper"; use Data::Dumper; print Dumper $obj; say "\n*** Data::Dump"; use Data::Dump; dd $obj; say "\n*** Data::Printer"; use Data::Printer; p $obj; ' *** Data::Dumper $VAR1 = bless( { 'status' => 0, 'authority' => [], 'answer' => [], 'additional' => [], 'question' => [ bless( { 'qtype' => 15, 'qclass' => 1, 'qname' => bless( { 'label' +=> [ + 'example', + 'com' + ] }, 'Net::D +NS::DomainName1035' ) }, 'Net::DNS::Question' ) ] }, 'Net::DNS::Packet' ); *** Data::Dump bless({ additional => [], answer => [], authority => [], question => [ bless({ qclass => 1, qname => bless({ label => ["example", "com"] }, " +Net::DNS::DomainName1035"), qtype => 15, }, "Net::DNS::Question"), ], status => 0, }, "Net::DNS::Packet") *** Data::Printer Net::DNS::Packet { public methods (38): additional, answer, answerfrom, answersize, authority, data, d +ecode, dump, edns, encode, from, header, HEADER_LENGTH, new, pop, pre +, prerequisite, print, push, question, reply, sign_sig0, sign_tsig, s +igrr, size, string, truncate, UDPSZ, unique_push, update, verify, ver +ifyerr, zone Carp: carp, confess, croak Net::DNS::Parameters: dsotypebyname, dsotypebyval private methods (1): _section internals: { additional [], answer [], authority [], question [ [0] Net::DNS::Question ], status 0 } }

    See also:

    — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11152644]
Approved by 1nickt
Front-paged by 1nickt
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2023-09-22 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?