Smoot Carl-Mitchell wrote:
On Wed, 07 Apr 2004 13:51:03 -0500 "JupiterHost.Net" <[EMAIL PROTECTED]> wrote:
perldoc -f flock says
"If LOCK_NB is bitwise-or'ed with LOCK_SH or LOCK_EX then "flock" will return immediately rather than blocking waiting for the lock (check the return status to see if you got it)."
So that would mean:
use Fcntl ':flock';
flock(FH, LOCK_EX || LOCK_NB) or die "Lock failed $!";
So flock(FH, LOCK_EX | LOCK_NB) then?
... flock FH, LOCK_UN;
Not quite. The bitwise or operator is '|' not '||'. What you have sets the 2nd argument to true or 1 which is not what you want.
If so, does the numeric values work the same way?
The numeric values work the same, but I strongly encourage use of the manifest constants. It makes the code more portable and easier to read.
Right On.
lock(FH, 1 || 4) or die "Lock failed $!";... flock FH, 8;
correct?
If you do the (LOCK_EX || LOCK_NB) or (1 || 4) is the return code different depending on the type of lock received?
IE my $rc = flock(FH, LOCK_EX || LOCK_NB); if($rc == 4) { warn "rats! Exclusive lock not granted, oh well..."; } if(!$rc) { die "Could not get lock no how mr flock guy!"; }
The return code with LOCK_NB is false if the file is locked by another process, true is you got the lock. $! holds the appropriate error message.
So: my $rc = flock(FH, LOCK_EX | LOCK_NB);
if($rc == 0) { warn "rats! Exclusive lock not granted, oh well..."; } if(!defined $rc) { die "Could not get lock no how mr flock guy!"; }
or
if(!$rc) { $! =~ m/No Lock/ ? warn "not exclusive but locked - $!" : die "no lock - $!"; }
or ??
If the second way then what would "No Lock" be?
(IE what is $! set to if LOCK_NB is used instead of LOCK_EX and what is it set to if no lock is made?)
Thanks for the info BTW I really appreciate it! :)
TIA
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>