Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

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

To expand a little on what has already been said: flock will wait, until it gets a lock. If it does not get one, it is not likely to the next time you try either. Your code might be useful if it was used for open instead of flock, then, once the open was successful, do the flock. Once. If it's going to fail, it's going to fail here. Also, you should only use LOCK_EX when you want to write to the file, and then do your writing as fast as possible, and unlock it as soon as possible. The problem with LOCK_EX is that it not only locks your file, but creates a bottleneck, as only one process can access the file at a time. With LOCK_SH, many processes can read the file simultaneously, although they cannot write to it.

Also, you might want to write

my $file = shift; ## instead of my $file = pop();
That way, if you ever add more parameters, (the kind of lock perhaps?) the filename can remain the first parameter, which reads better and fits in with general layout of most significant to least significant in an argument list (i.e. perl functions always have their optional arguments at the end)


In reply to RE: Flock Subroutine by turnstep
in thread Flock Subroutine by mt2k

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 drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-06-22 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.