[LA.pm] locking a directory

Ofer Nave ofer at netapt.com
Thu Feb 17 14:03:40 PST 2005


Does anyone know how to lock a directory?  I tried the standard flock 
call, and it didn't like it:

my $dir = "/netapp/ofer/somedir";
print "opening $dir\n";
opendir( SOMEDIR, $dir ) or die( $! );
print "locking $dir\n";
flock( SOMEDIR, LOCK_EX ) or die( $! );
print "locked! sleeping\n";
sleep( 999 );
print "unlocking $dir\n";
flock( SOMEDIR, LOCK_UN ) or die( $! );
print "closing $dir\n";
closedir( SOMEDIR ) or die( $! );
print "done\n";

[onave at trioval01 ~] ./foo.pl 
opening /netapp/ofer/somedir
locking /netapp/ofer/somedir
flock() on unopened filehandle SOMEDIR at ./foo.pl line 19.
        (Are you trying to call flock() on dirhandle SOMEDIR?)
Bad file descriptor at ./foo.pl line 19.

I've done some searching on CPAN and google with no luck so far.

-ofer



More information about the Losangeles-pm mailing list