However, if you need the emulation, then it already exists in and is exported by perl58.dll under the name win32_flock. It can be access through Win32::API::Prototype as follows:

Update: Changed use to require + import.

#! perl -slw use strict; use Fcntl qw[ :flock ]; $|=1; if( $^O eq 'MSWin32' ) { { local $^W; require Win32::API::Prototype; Win32::API::Prototype->import( 'ApiLink' ); } ApiLink( 'perl58', 'int win32_flock(int fd, int oper)' ) or die $^ +E; our $flock; *flock = *win32_flock = *win32_flock; } die 'Already running' unless flock( DATA, LOCK_EX ); sleep 10; flock( DATA, LOCK_UN ) or die $!; __DATA__ stuff

However, in my tests just now, when running a second copy of the above script, the call to flock( LOCK_EX ), causes the script to silently terminate, so that may not be so useful after all.

I'm not sure whether

  1. I'm simply using the call incorrectly (which is possible as I've never needed flock());
  2. or if there is a bug in the implementation (at win32.c line 2246 in the 5.8.8 sources);
  3. Or there is a bug in the transition between perl and C via Win32::API(::Prototype);

but maybe this and the pointer to the source will let someone who knows what they are doing work out which and, either point out my stupidity or raise a bug against the responsible component?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Why no LockFile() in Win32API::File ? by BrowserUk
in thread Why no LockFile() in Win32API::File ? by renodino

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.