Did you copy and paste that code? Do you know that when calling can() you are using $ojb instead of $obj?
-------- Paul Anderson -- VE3HOP On 2012-09-12, at 9:17 AM, pangj <pa...@riseup.net> wrote: > Hi, > > Today I wrote a script for validate a dns record from the specified DNS > server, the subroutine is: > > sub validate_dns { > my $host = shift; # a host, for example, www.google.com > my $value = shift; # the host's DNS value, may be a CNAME or an iP > address > my $dns = shift; # DNS server, for example, 8.8.8.8 > > my $method = $value =~ /^\d+\.\d+\.\d+\.\d+$/ ? "address" : "cname"; > my $res = Net::DNS::Resolver->new(nameservers => [$dns]); > my $answer = $res->query($host); > > if (defined $answer) { > return 1 if ($answer->answer)[0]->$method eq $value; > } > > return 0; > } > > Sometime the $host's value is an A record, thus it has a address() method. > Sometime the $host's value is a CNAME record, it has a cname() method. > When calling the subroutine abve with the wrong arguments, it will cause > warnings. > For example, > validate_dns("www.nsbeta.info","1.2.3.4","8.8.8.8"); > This will throw up warnings, because www.nsbeta.info is a CNAME but the > subroutine calls address() method which belongs to the A record. > > So I modified the subroutine as: > > sub validate_dns { > my $host = shift; > my $value = shift; > my $dns = shift; > > my $res = Net::DNS::Resolver->new(nameservers => [$dns]); > my $answer = $res->query($host); > > if (defined $answer) { > my $obj = ($answer->answer)[0]; > if ($ojb->can("address") ) { > return 1 if $obj->address eq $value; > } elsif ($ojb->can("cname") ) { > return 1 if $obj->cname eq $value; > } > } > > return 0; > } > > This totally can't work, it seems $obj->can() method has no effect. > What's wrong with my code? thanks. > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/