Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

In Net::DNS::RR what is the 'can' method?

by Lotus1 (Vicar)
on Sep 19, 2018 at 18:01 UTC ( [id://1222664]=perlquestion: print w/replies, xml ) Need Help??

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

This is from the examples section of Net::DNS :

use Net::DNS; my $res = Net::DNS::Resolver->new; my $reply = $res->search("www.example.com", "A"); if ($reply) { foreach my $rr ($reply->answer) { print $rr->address, "\n" if $rr->can("address"); } } else { warn "query failed: ", $res->errorstring, "\n"; }

The search method in the resolver returns a packet object. Then the 'answer' method of that (from Net::DNS::Packet) "Returns a list of Net::DNS::RR objects representing the answer section of the packet." I've looked through the documentation for Net::DNS::RR and the module sourch but can't find the description of what the 'can' method is. The example runs without errors while using strict and warnings. Any suggestions?

Replies are listed 'Best First'.
Re: In Net::DNS::RR what is the 'can' method? (updated)
by haukex (Archbishop) on Sep 19, 2018 at 18:15 UTC

    It's probably UNIVERSAL::can.

    Update: Yep, looks like it. UNIVERSAL is Perl's implicit base class for all objects. can queries whether the object has that method (including via inheritance). It looks like the Net::DNS docs are suggesting to use duck typing here to tell whether the Net::DNS::RR subclass has the address method. An alternative would be to use isa (also from UNIVERSAL) to see if an object is of a certain class (or is one of its subclasses) and therefore supports that class's methods. The thing about that is though that you'd have to test against a whole list of Net::DNS::RR subclasses. As long as all of the subclasses' address methods behave the same, duck typing is safe here, and easier.

      Thanks! I didn't know about that one.

      Edit: Other examples I've seen just test if the response object is true since it will be 'undef' if there were no responses.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-25 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found