I invite you, tear me a new one.
This code gets the job done. It takes a .txt file containing a list of IPs and writes a file containing their respective fully qualified domain names.
I want to know in what ways is this code poorly written. What bad habits are here?
I am a perl and programming newbie. I managed to put this together using google and trail and error. Getting it to work was satisfying but please tell me how I can improve.
use strict;
use warnings;
use Socket;
use autodie;
my $filename = 'IPsForFQDN.txt';
#File with list of IPs to lookup.
#One IP address per line like so:
#10.10.10.10
#10.10.10.11
#10.10.10.12
#etc...
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not opne file '$filename' $!";
my $fqdn = '';
while (my $row = <$fh>) {
chomp $row;
print "$row\n";
$fqdn = gethostbyaddr(inet_aton($row), AF_INET);
print $fqdn;
print "\n";
open FILE, ">>fqdn.txt" or die $!;
print FILE $fqdn;
print FILE "\n";
close FILE;
}
print "done\n";
For instance is the {chomp $row;} line needed? I have NO IDEA what it does.
I am equally mystified by the whole {or die $!;} thing.