Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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 Highlander.pm 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

In reply to Re^3: Never lock $0 inside of a BEGIN block (BEGIN?) by tye
in thread Never lock $0 inside of a BEGIN block by demerphq

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-23 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found