http://www.perlmonks.org?node_id=1162617

Preceptor has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I'm hoping this isn't too stupid a question. I have been trying to test (for a new system) locking files on NFS. My problem is - I'm having a real difficulty untangling what is a good way of accomplishing it - I've tracked down fcntl, and flock.

So is anyone able to give me a pointer in how this is done 'right'? All I really need is to be able to create and lock a test file (twice, so I can check if the lock is 'working'), and can run on multiple instances/hosts. I am aware that this is implementation dependent - but this is as much to test if I _can_ on our implementation, as anything else.

Thus far, I've got as my tests:

#!/usr/bin/env perl use strict; use warnings; use Fcntl qw ( :flock ); open ( my $lock_this, '>', 'lockfile_flock' ) or die $!; print "Enter to lock\n"; <>; print "Requesting EX lock\n"; flock ( $lock_this, LOCK_EX ); print "Lock acquired\n"; print "Enter to release\n"; <>; flock ( $lock_this, LOCK_UN ); print "Lock released\n";

I'm trying to do the same with fcntl, but am being flummoxed a bit by references to pack and flags. Is anyone able to give me an example of how using "fcntl" to lock a file is supposed to look in perl?