in reply to How To Read Hosts File Into a Hash

open FILE, "<", "$hostfile" || die "Cannot open $hostfile $!";

You shouldn't quote variables like that:

What's wrong with always quoting "$vars"?

The high precedence of the || operator means that die will only execute if $hostfile contains the string "" or the string "0".    You need to either use parentheses with open:

open( FILE, "<", $hostfile ) || die "Cannot open $hostfile $!";

Or use the low precedence or operator:

open FILE, "<", $hostfile or die "Cannot open $hostfile $!";


while (<FILE>) { chomp; my ($key, $value) = split (" ", $_); $hosts{$key} = $hosts{$value}; }

That should probably be:

while (<FILE>) { my ($key, $value) = split; $hosts{$key} = $value; }

Or if you want all the hosts associated with each IP address:

while (<FILE>) { my ($key, @values) = split; push @{$hosts{$key}} = @values; }