Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Reading in an entire file in a single <> operation is colloquially known as "slurping". But the considerations of memory allocation discussed in the previous section mean that slurping the contents of a file and then manipulating those contents monolithically, like so:
# Slurp the entire file (see the next guideline)...my $text = do { local $/; <> };# Wash its mouth out...$text =~ s/$EXPLETIVE/[DELETED]/gxms;# Print it all back out...print $text;
is generally slower, less robust, and less scalable than processing the contents a line at a time:
while (my $line = <>) {$line =~ s/$expletive/[DELETED]/gxms;print $line;}
Reading an entire file into memory makes sense only when the file is unstable in some way, or is being updated asynchronously and you need a "snapshot", or if your planned text processing is likely to cross line boundaries: