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

I have a script that uses the Windows Task Scheduler on my Windows NT server to copy an Excel Spreadsheet to another Server and it works. I would like to have exclusive lock on the Excel file to make sure no one else is writing in it at the same time I am copying it.
use strict; my $listFile = "C:\\theData.xls"; my $listFile2 = "\\\\AnotherServer\\dir\\"; open (IN, "$listFile") || die "Cant open $listFile for reading: $!"; flock(IN, LOCK_EX) || die "Cant get LOCK_SH on $listFile: $!"; print(IN); system("copy $listFile $listFile2 > nul") == 0 || die "Copy failed."; print "Copy completed"; close IN || die "Cant close $listFile: $!";
I keep getting error message: Bareword "LOCK_EX" not allowed while "strict subs"

If I use this: flock(IN, 1) instead of this flock(IN, LOCK_EX)
it works.

Please advise how I can get the Lock Exclusive to work?

Replies are listed 'Best First'.
Re: Lock on file
by duff (Parson) on Feb 10, 2006 at 15:34 UTC

    You need to include the line: use Fcntl qw/:flock/; in your code to import the proper constants. (This is explained in the documentation for flock. See flock)

    Are locks mandatory on Windows? If they're only advisory, as under unixish OSes, an exclusive lock may not help you.

Re: Lock on file
by Fletch (Bishop) on Feb 10, 2006 at 15:36 UTC

    Aside from the missing use Fcntl, locking with flock is just advisory. Stuff that doesn't attempt to flock won't be stopped from modifying the file in any way.

      Thanks, it now works.
      From your inputs it sounds like I should not use any kind of Flock and just do the copy?

      If I was copying a small Access database the Flock would be needed? And if so what kind?

        It's highly unlikely Access is going to honor flock, but you're definitely welcome to try. And if it doesn't then you don't really gain anything by trying to lock it as it's free to ignore it, hence "advisory locking".

        Think of it as a "Wet Paint" sign on a bench. The sign itself just tells you, "Hey, sit here and you're going to get covered in paint." You're not prevented from sitting and getting paint on your tuckus, but if you ignore the advisory bad things can happen. Access, more than likely, is going to blithely sit on your freshly painted bench.

Re: Lock on file
by swampyankee (Parson) on Feb 10, 2006 at 18:25 UTC

    You may have to dive into the innards of the Windows API to solve your problem. There is a Windows API function (which does not seem to be implemented in the version of the Win32 module in ActiveStates' 5.8.7 build), LockFileEx which implements strict file locking.

    I believe that Excel grabs exclusive access to a file it has open, but this may not be honored when a file is being pulled from a different machine, so you shouldn't be able to copy it while the file is open in Excel. Testing this should be easy; have somebody open the file in Excel and try running your copy job while it's open.

    There are also ways -- using the Windows API -- to check to see if a file is being used by another process. One I've used is to try to open the file for exclusive access with _lopen (this is via a C++ routine). If you cannot open it exclusively, somebody else currently has it open.

    emc

    " When in doubt, use brute force." — Ken Thompson