in reply to Re^2: improving the aesthetics of perl code
in thread improving the aesthetics of perl code
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") || 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; } } }
Here's something similar but using a line-at-a-time approach:open CFGFILE, "< ship.cfg" or die "read ship.cfg: $!"; @hosts = map { /^([\w.]+)$/ ? $1 : () } grep !/#/, <CFGFILE>; } close CFGFILE;
hth.open CFGFILE, "< ship.cfg" or die "read ship.cfg: $!"; while (<CFGFILE>) { /#/ and next; /^([\w.]+)$/ or next; push @hosts, $1; } close CFGFILE;
|
---|