Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Never lock $0 inside of a BEGIN block (Win32?)

by tye (Sage)
on May 21, 2003 at 22:16 UTC ( #259914=note: print w/replies, xml ) Need Help??

in reply to Never lock $0 inside of a BEGIN block

My main reaction to this is "I bet you are using Win32" and "This won't happen on Unix". Locking in Win32 can prevent reading and writing of data via other file handles (even within the same process). Locking in Unix only affects other locking (by default) and only those held by a different process [ well, at least for the types of locking that I'm most familiar with, fcntl locks; I won't swear that some flocks won't block a process from itself ].

So you could probably get away with flocking the DATA file handle in a BEGIN block, even on Win32, if Perl defined the DATA file handle inside BEGIN blocks (since it hasn't parsed the __END__ yet, it doesn't yet know whether it is supposed to give you a DATA file handle or not).

Also, I really think Perl could use a patch such that it reports when it gets an error reading the source code. I think you would have been much happier if you'd seen this:

Error reading script source, highlander: Permission denied
(if it also reported $^E and not just $! then you'd even be told "The process cannot access the file because another process has locked a portion of the file" of which the "another process" part might well be a lie, but that is Win32's fault, not Perl's).

                - tye

Replies are listed 'Best First'.
Re: Re: Never lock $0 inside of a BEGIN block (Win32?)
by zby (Vicar) on May 22, 2003 at 08:08 UTC
    For me the fact that this problem appeares only on Win32 machines makes it only worse. It obscures the view.

    By the way why should one need to do it in the BEGIN block? I mean for sure there must be some cases where it is needed, but for most cases it is just a automatic thought, something like that: It should be locked early, early is linked in your brain with the BEGIN block - so put it into it.

      Good points. I can stretch my imagination and come up with cases where wanting to lock out duplicates at compile time might be useful. But I agree that such seem quite unlikely and the appeal is mostly an "earlier is better" feeling that is misguided in this case.

      But this did inspire me to encapsulate a very simple, reusable method of locking out multiple instances of the same script:

      package Highlander; use strict; use warnings; use Carp qw( croak ); use Fcntl qw( :flock ); # LOCK_EX LOCK_NB sub import { croak( "The Highlander does not appreciate being 'use'd.", " Please simply 'require' the Highlander.\n" ); } if( ! defined fileno *main::DATA ) { croak( "No __END__ in sight. Please put an __END__", " to $0 (or 'require' the Highlander; don't 'use')\n" ); } flock( *main::DATA, LOCK_EX|LOCK_NB ) or die( "The Highlander feels the quickening.", " Another $0 is already here.\n" ); "There can be only one!";
      Simply save this to where you keep your Perl modules and 'require Highlander;' in any scripts that you don't want more than one instance of running at an given moment.

      Name stolen from •Highlander.

      Note that you can copy the script to a different name or directory if you want to run another instance of it (which you can consider a feature or a bug, depending). Also, on Win32 this still isn't a great solution because the exclusive lock causes Perl to simply act as if the script is an empty file so instead of the second instance of the script failing and giving the "quickening" message, it simply ends successfully after doing nothing.

      So for some cases it would be nice to make the lock file external. But that makes it more complicated and is left as an exercise for the reader. (:

                      - tye
Re: Re: Never lock $0 inside of a BEGIN block (Win32?)
by demerphq (Chancellor) on May 22, 2003 at 08:43 UTC

    "I bet you are using Win32" and "This won't happen on Unix".

    Yep. And I thought not.

    I think you would have been much happier if you'd seen ..... if it also reported $^E

    Double yep. I would have probably figured this one out immediately if any of your above suggestions had been implemented.


    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
      I really think Perl could use a patch

      ...and I think you should at least consider writing that patch. (:

                      - tye

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://259914]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2018-06-18 05:20 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (108 votes). Check out past polls.