Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

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

> In addition, I imagine that the flocked file is
> automatically un-flocked when it is closed, but
> is there any benefit(s) to explicitly calling
> flock(TEST, 8) before closing the file?

In general, no, there is no reason to call flock, especially if you are about to close the file. Perl will unlock the file for you. If you exit the script without closing the file, perl will unlock it and close it for you.

I can think of only two times where one might want to explicitly unlock a file through flock:

First, you may want to check the return code and see whether or not the unlock succeeded or failed. In reality, this is a very rare event. File locking, on the other hand, can fail for many reasons, and the value should always be checked. Once a file has been locked, in other words, it is very unlikely that an unlock will fail. I would only add this in after a problem had been found, as part of an advanced debugging process. Then again, checking the status doesn't really make your code any slower, so whatever you are comfortable with.

The second reason to unlock a file is if there is going to be a long pause in which you are not going to use the file, but you do not want to close it yet. For example, take this script that adds a timestamp to a logfile at a random interval (perhaps it picks a random visitor to win something?)

my $logfile = "/foo/bar.log"; my $loops=0; open(FOO, ">>$logfile") || die "Could not open $logfile: $!\n"; flock(FOO, 1) or die "Could not lock $logfile: $!\n"; print FOO "**Begin timestamping**\n"; { print FOO "**", scalar localtime, "**\n"; flock(FOO, 8) or die "Could not unlock $logfile: $!\n"; $loops>500 and last; sleep(60*(rand(6)+5)); flock(FOO, 1) or die "Could not lock $logfile: $!\n"; redo; } close(FOO); exit;

Not a very useful script, but it illustrates the point. I'd probably open and close it every time.


In reply to RE: RE: Re: This is weird... by turnstep
in thread This is weird... by SYbeginner

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: (5)
As of 2024-04-25 12:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found