Gorby has asked for the wisdom of the Perl Monks concerning the following question:
sub get_lock { open(SEM, ">$semaphore_file") || die "Cannot create semaphore $semap +hore_file: $!"; flock(SEM, LOCK_EX) || die "Lock failed: $!"; } sub release_lock { close(SEM); } sub readdata { my @filedata; @filedata=(); open(MFILE, ">>$completeadd") || die "file open2 failed at $complete +add: $!\n"; close(MFILE); open(MFILE, "$completeadd") || die "file open81 failed: $!\n"; @filedata=<MFILE>; chomp @filedata; close(MFILE); return(@filedata); } sub writedata { my @filedata; @filedata=(); @filedata=@_; open(MFILE, ">$completeadd") || die "file open3 failed: $!\n"; foreach(@filedata) { print MFILE "$_\n"; } close(MFILE); } $completeadd = "counter"; $semaphore_file = "counterLOCK"; get_lock(); if (-e $completeadd) { open(MFILE, "$completeadd") || die "file open4 failed: $!\n"; @filedata1=<MFILE>; chomp @filedata1; close(MFILE); } else { @filedata1=readdata(); } $hitcount=$filedata1[0]; if ($hitcount) { $hitcount=$hitcount + 1; } else { $hitcount = 1; } $filedata1[0]=$hitcount; writedata(@filedata1); release_lock(); close(CNTLCK);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: Disappearing File
by merlyn (Sage) on Jan 13, 2004 at 02:28 UTC | |
|
Re: Disappearing File
by Zaxo (Archbishop) on Jan 13, 2004 at 02:45 UTC | |
by Gorby (Monk) on Jan 13, 2004 at 03:11 UTC | |
by Zaxo (Archbishop) on Jan 13, 2004 at 03:47 UTC | |
|
Re: Disappearing File
by pg (Canon) on Jan 13, 2004 at 04:11 UTC | |
by Gorby (Monk) on Jan 13, 2004 at 07:17 UTC | |
by merlyn (Sage) on Jan 13, 2004 at 16:50 UTC |