Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How To Read Hosts File Into a Hash

by jwkrahn (Abbot)
on Sep 16, 2011 at 00:58 UTC ( [id://926273]=note: print w/replies, xml ) Need Help??


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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://926273]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2025-11-10 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (67 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.