
yapp
User
May 23, 2002, 12:59 PM
Post #5 of 8
(2369 views)
|
|
Re: [uri] Remove a line without reading whole file into memory
[In reply to]
|
Can't Post
|
|
I always use the following thing.. Is that better/worst is just different? Well, actually I use my File::PlainIO module, which has an update(coderef) method. That chooses the best approach (slurping or file buffers) and receives an update function as first parameter, so it becomes $file->update($mysub)., where $mysub = sub {...}; #!/usr/bin/perl -w use strict; use Fcntl qw(:DEFAULT :flock); my $file = 'somefile.txt'; my $temp = "$file~"; sysopen(FH, $file, O_CREAT | O_RDWR) or die "Can't rdwr $file: $!"; flock(FH, LOCK_EX); # Or use new_tmpfile IO::File sysopen(TEMP, $temp, O_CREAT | O_RDWR) or die "Can't make temp: $!"; flock(TEMP, LOCK_EX); while(my $line = <FH>) { my $remove = (..some logic...); print TEMP $line unless $remove; } seek(TEMP, 0, 0) or die "Can't seen begin of temp: $!"; seek(FH, 0,0) or die "Can't seen begin of $file: $!"; truncate(FH, 0) or die "can't truncate: $!"; print TEMP while <FH>; # Mostly I use a read(..) with 4096 bytes at once. close(TEMP); unlink(TEMP) or die "Can't unlink temp: $!"; close(FH); Yet Another Perl Programmer _________________________________ ~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~ More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads!
(This post was edited by yapp on May 23, 2002, 1:03 PM)
|