Regarding this:
open (CFGFILE, "ship.cfg") || die $!;
while (my @TMP=<CFGFILE>) {
next if ( /#/ );
# Lets untaint the data after making sure it only matches
# word characters... For more info read perldoc perlsec.
foreach $_(@TMP) {
if ($_ =~ /^([\w.]+)$/) {
$_ = $1;
push (@hosts, $1);
} else {
next;
}
}
}
That's totally wrong. Ordinarily, you either read from a file a line at a time, using
while, or you read the entire (rest of the) file by assigning to a list. You can't meaningfully combine the two approaches, which is what it looks like you've tried to do. Below is how to write your loop using the one-fell-swoop approach, with the other transformations included:
open CFGFILE, "< ship.cfg" or die "read ship.cfg: $!";
@hosts =
map { /^([\w.]+)$/ ? $1 : () }
grep !/#/,
<CFGFILE>;
}
close CFGFILE;
Here's something similar but using a line-at-a-time approach:
open CFGFILE, "< ship.cfg" or die "read ship.cfg: $!";
while (<CFGFILE>) {
/#/ and next;
/^([\w.]+)$/ or next;
push @hosts, $1;
}
close CFGFILE;
hth.