in reply to Slurping file into array VS Process file line-by-line
Your general coding style is reminiscent of Perl 4. So I hope you don't mind if I make a few general style suggestions.
$filename = @ARGV[0];
I think you meant to type $ARGV[0]. Writing @ARGV[0] is a slice and is almost never what you want.
open (FILE,"$filename") || die "Can't Open $filename: $!\n";
Prefer the low precedence or to ||. Also, you don't need the quotes around $filename. Finally, unless you are stuck with an ancient perl, employ the modern three argument form of open with a lexical file handle (see example code below).
Also, don't forget the option of slurping into a string rather than an array. Quite often, it's more convenient to process the file contents with regexes on a string, rather than line by line from an array. For example.
use strict; my $filename = shift; open(my $fh, '<', $filename) or die "Can't open $filename: $!"; my $text = do { local $/; <$fh> }; # slurping into a string # do something with $text here
Update: for more information on file slurping idioms, see Load file into a scalar without File::Slurp, especially merlyn's response.
|
---|