in reply to Search and replace within a file

On a side note, let me recommend you File::Slurp module, it allows you to replace the whole your example with:

use File::Slurp qw(edit_file); edit_file { s/$uname/$answer1/g; s/domainsuffix.net/$answer4/g; } $hosts;