On 01/02/2014 11:48 AM, David Precious wrote:
On Thu, 02 Jan 2014 11:18:31 -0500
Uri Guttman <u...@stemsystems.com> wrote:
On 01/02/2014 10:39 AM, David Precious wrote:
Secondly - do you need to work on the file as a whole, or can you
just loop over it, making changes, and writing them back out? In
other words, do you *need* to hold the whole file in memory at one
time? More often than not, you don't.
If it's per-line changes, then File::Slurp::edit_file_lines should
work
- for e.g.:
use File::Slurp qw(edit_file_lines);
my $filename = '/tmp/foo';
edit_file_lines(sub { s/badger/mushroom/g }, $filename);
The above would of course replace every occurrence of 'badger' with
'mushroom' in the file.
if there is a size issue, that would be just as bad as slurping in
the whole file and it would use even more storage as it will be an
array of all the lines internally.
Oh - my mistake, I'd believed that edit_file_lines edited the file
line-by-line, writing the results to a temporary file and then
renaming the temporary file over the original at the end.
In that case, I think the docs are a little unclear:
"These subs read in a file into $_, execute a code block which should
modify $_ and then write $_ back to the file. The difference between
them is that edit_file reads the whole file into $_ and calls the code
block one time. With edit_file_lines each line is read into $_ and the
code is called for each line..."
good point. i should emphasize that it does slurp in the file. tie::file
only reads in chunks and moves around as you access elements.
edit_file_lines slurps into an array and loops over those elements
aliasing each one to $_. it definitely eats its own dog food!
and
"These subs are the equivalent of the -pi command line options of
Perl..."
... to me, that sounds like edit_file_lines reads a line at a time
rather than slurping the whole lot - but looking at the code, it does
indeed read the entire file contents into RAM. (I probably should have
expected anything in File::Slurp to, well, slurp the file... :) )
as i said, dog food is good! :)
i wrote edit_file and edit_file_lines as interesting wrappers around
read_file and write_file. i assumed it was obvious they used those slurp
functions.
Part of me wonders if File::Slurp should provide an in-place (not
slurping into RAM) editing feature which works like edit_file_lines but
line-by-line using a temp file, but that's probably feature creep :)
that IS tie::file which i didn't want for efficiency reasons. it has to
read/write back and forth every time you modify an element. edit_file
(and _lines) are meant to be fast and simple to use for common editing
of files. as with slurping, i didn't expect them to be used on .5GB
files! :)
uri
--
Uri Guttman - The Perl Hunter
The Best Perl Jobs, The Best Perl Hackers
http://PerlHunter.com
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/