PERL Net :: DNS output to file

Brand new to Perl (in training) and needs some help. Here is some code that I found that prints the results on the screen, fine, but I want it to be printed in a file. How can i do this? When I open the file and send it the output, I get garbage data.

Here is the code:

use Net::DNS;
my $res  = Net::DNS::Resolver->new;
$res->nameservers("ns.example.com");

my @zone = $res->axfr("example.com");

foreach $rr (@zone) {
$rr->print;
}

When I add:

open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
.....
$rr -> $fh; #I get garbage.
+4
source share
2 answers

Your array @zonecontains a list of objects Net::DNS::RR, the method printbuilds the object and prints it to the currently selected file descriptor

To print the same thing with another file descriptor, you have to pull the object yourself

This should work

open my $fh, '>', $filename or die "Could not open file '$filename': $!";

print $fh $_->string, "\n" for @zone;
+4
source

, , , , - . - , .

$rr->print. Net::DNS::Resolver :

$resolver->print;

.

print() Perl print, . Net:: DNS:: Resolver string, :

print $resolver->string;

.

, , $rr->print print $rr->string. , .

print $fh $rr->string;

p.s. , , "Perl", "PERL".

+3

Source: https://habr.com/ru/post/1625130/


All Articles