in reply to Learning Perl by Doing

Hi raywood,

Welcome to the Monastery and to Perl, the One True Religion.

1. That old discussion mentioned RAM concerns when slurping. My system has 16GB RAM. The files I am working on are small. But I may adapt the solution to other, larger files. When does RAM become an issue?

When the file is too big.

The better way is to read the file line by line, then it doesn't matter how big it is or how much RAM you have. In that example, instead of slurping in all the data with:

my $data = do { local $/; <DATA> }; @array = split 'on something', $data;
You would rather do:
while ( my $line = <DATA> ) { # handle the line here instead of as an element of an array }

How would I adapt this solution to refer to a separate input file?

See open, or use a module like Path::Tiny (which handles errors and encoding for you):

use Path::Tiny 'path'; my $file_path = '/foo/bar/baz.txt'; # if no memory concern, slurp: my @lines = path( $file_path )->lines_utf8({chomp => 1}); # or read from a filehandle line-by-line: my $fh = path( $file_path )->openr_utf8; while ( my $line = <$fh> ) { chomp $line; ... }

Hope this helps!


The way forward always starts with a minimal test.