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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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



yapp
User

May 23, 2002, 12:59 PM


Views: 3302
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 {...};


Code
 #!/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! 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