in reply to flock with FileHandle.pm?

The object is also a valid filehandle. So you just call:
flock($fh, $mode) or die "Cannot flock file in $mode: $!";
If this bothers you, you can just add the following:
package FileHandle; use Carp; sub lock { my $self = shift; flock($self, 8) or confess("Cannot lock file: $!"); }
and now you can just call:
$fh->lock();
Note that I did not create an unlock method. That is intentional. Files should never be unlocked. Instead you close or drop the filehandle, and they will be unlocked for you by the OS.

Replies are listed 'Best First'.
Re: Re (tilly) 1: flock with FileHandle.pm?
by DBX (Pilgrim) on Aug 24, 2001 at 11:29 UTC
    Cool, thanks. This helped a lot. Just out of curiosity, why not unlock the filehandle? I am used to doing:
    flock($fh,2); ...process stuff here... $flock($fh,8);
        Yes!! That whole discussion is fascinating and I something I had never come across. Thanks!