CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate: Re: [uri] Remove a line without reading whole file into memory: Edit Log


May 23, 2002, 12:59 PM

Views: 7639
Re: [uri] Remove a line without reading whole file into memory

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.

unlink(TEMP) or die "Can't unlink temp: $!";


Yet Another Perl Programmer

~~> [url=] <~~
More then 3500 X-Forum [url=]Downloads! Cool

(This post was edited by yapp on May 23, 2002, 1:03 PM)

Edit Log:
Post edited by yapp (User) on May 23, 2002, 1:03 PM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives