in reply to Reading a file before clobbering it for output...
Update: Instead of the reopen, use truncate with the other open "+<"#!/usr/bin/perl use strict; use warnings; my $logfile = "logfile"; my @entries; open(LOG, "<$logfile") || die "Could not open $logfile reading\n"; flock(LOG, LOCK_EX) || die "Could not lock file $!\n"; for (1..10) { chomp($_ = <LOG>); push @entries, $_; } #Do something with @entries # open(LOG, ">$logfile") || die "Could not open $logfile for writing\ +n"; truncate(LOG, 0); print LOG join("\n", @entries), "\n"; close(LOG);
Further Update: Fixed that before bluto's good advice after a bit of looking up some info. Knew the close would happen, wasn't positive it would kill the lock though. Hence, my warning up above. Also means the unlocking flock is unnessasry since the lock is released on close.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Reading a file before clobbering it for output...
by bluto (Curate) on Jun 20, 2001 at 22:50 UTC |