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)
-
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.
|