8 ms·
Except that Perl is filter mode. In Perl while (<FILE>) { print $_; } is line by line and efficient. Perl programmers avoid my @entire_file=<$yourh
by Terretta 13y ago
Except that Perl is filter mode. In Perl
while (<FILE>) { print $_; }
is line by line and efficient. Perl programmers avoid
my @entire_file=<$yourhandle>;
But even that loads the array line by line.
As a side note, good Perl has always done things while there are things to do, in a list processing kind of approach feeling more functional than imperative.
// Slurping the whole file into a single thing is actually a chore: http://stackoverflow.com/questions/206661/what-is-the-best-way-to-slurp-a-file-into-a-string-in-perl http://stackoverflow.com/questions/206661/what-is-the-best-w...
- mct 13y agoIf you're writing a filter, an even better pattern is: while (<>) { ... } By omitting the file handle, "<>" will go line by line through each filename specified on the command line. If no filename arguments were passed, it reads from STDIN.
- shabble 13y agoI'm not sure if it's still the case (I recall some discussion about optimising it) but using the more obvious[1] idiom of foreach my $line (<$FH>) { ... } slurps the entire file because the foreach is forcing list context on the diamond operator. The while(<$FH>) { ... } that you describe works as desired though. http://stackoverflow.com/questions/585341/ http://stackoverflow.com/questions/585341/ appears to cover it in some detail. [1] To me, anyway.