I was going to guess that you might have windows line-endings in your hostfile2 - which would also give you problems - but I think moritz has probably nailed it with his comment.
Replace the four instances in getNetworkInfo where you globally undef $/ to do so with just local effect:
# undef $/; # global effect!
# instead:
local $/ = undef; # local effect
It also appears that you're not running under strict or warnings (unless you've snipped that from your post). You really should look into that.