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..."

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... :) )

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 :)

OP - what didn't work about Tie::File?



-- 
David Precious ("bigpresh") <dav...@preshweb.co.uk>
http://www.preshweb.co.uk/     www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedin    www.preshweb.co.uk/facebook
www.preshweb.co.uk/cpan        www.preshweb.co.uk/github



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to