How can I write output to a new external file with Perl?
Posted
by Structure
on Stack Overflow
See other posts from Stack Overflow
or by Structure
Published on 2010-05-20T03:45:04Z
Indexed on
2010/05/20
3:50 UTC
Read the original article
Hit count: 149
perl
Maybe I am searching with the wrong keywords or this is a very basic question but I cannot find the answer to my question. I am having trouble writing the result of my whois command to a new external file.
My code is below. It takes $readfilename
, which is a file name which has a list of IPs, and $writefilename
, which is the destination file for output. Both are user-specified. For my tests, $readfilename
contains three IP addresses on three separate lines so there should be three separate whois results in the user specified output file.
if ($readfilename) {
open (my $inputfile, "<", $readfilename) || die "\n Cannot open the specified file. Please double check your file name and path.\n\n";
open (my $outputfile, ">", $writefilename) || die "\n Could not create write file.\n\n";
while (<$inputfile>) {
my $iplookupresult = `whois $_`;
print $outputfile $iplookupresult;
}
close $outputfile;
close $inputfile;
}
I can execute this script and end up with a new external file, but over half of the file has binary garbage data (running on CentOS) and only one (or a portion of one) of the whois lookups is readable.
I have no idea how half of my file is ending up binary... but my approach must be incorrect. Is there a better way to achieve the same result?
© Stack Overflow or respective owner