
yapp
User
Oct 10, 2001, 1:27 AM
Post #6 of 17
(6750 views)
|
Re: Ok, I'm stumped again. This time deals withDelete
[In reply to]
|
Can't Post
|
|
He're my quess. You properly need to check my code for some errors, since I typed it in the webbrowser directly. Can can combine my code with other samples above (in threaded view), where new files are created, to store the new data, and then overwrite the original file.
use strict; use Fcntl qw(:DEFAULT :flock); sysopen(FILE, $Yourfile, O_RDWR) or die "Can't open: $!"; flock(FILE, LOCK_EX); # Get the original data, and change it. my @FileContents = <FILE>; my $DeletedItem = splice(@FileContents, $ItemIndex, 1); # write the new data into the file seek(FILE, 0, 0) or die "Can't return to beginning: $!"; trucate(FILE, 0) or die "Can't truncate file to zero length: $!"; print FILE @FileContents; # Write the new data into the file. # unlock so other instances can use the file after out edits. flock(FILE, LOCK_UN); close(FILE); As you can see, the file is never closed, until we're done with it. Meanwhile it's locked, so other programs running at the same moment can't edit the same file at the same time, which results in corrupted data/files. Hope it helps.
|