As joost said originally, it's all explained with example in the official docs he linked to.
You don't need to learn about threading.
Having said that, I always implement the file open/local/write (or read)/unlock/close sequence in one sub.
The correct sequence is as per prev sentence. Don't worry about having the file open in multiple instances.
One note re the Perl docs though, lock is now a keyword (used in threaded programs). Prob best not to use it as a sub name to avoid confusion.
Here's one I wrote :
#********************************************************************* +********* # # Function : log_msg # # Description : Log a msg in shared log file ie 1 file for all serve +r # instances. # # Params : $_[0] log msg # # Returns : none # #********************************************************************* +********* sub log_msg { my ( $log_msg, # msg to be logged $datestamp, # Timestamp in format: Tue Oct 26 12:07:54 2004 $logfile, # final logfile name $filename, # filename sans extension $filext, # filename extension $filedir, # current dir $error_msg # error if any ); # Assign input params $log_msg = $_[0]; # Get Timestamp in format: Tue Oct 26 12:07:54 2004 $datestamp = localtime; # Get filename components ($filename, $filedir, $filext) = fileparse($0, '\..*'); # Create filename, open with Exclusive lock $logfile = "${cfg::params{'LOG_DIR'}}/${filename}${filext}.log"; open(CLIENT_LOG, ">>$logfile") or $error_msg = "log_msg(): Can't open $logfile: $!"; if( !$error_msg ) { flock(CLIENT_LOG, LOCK_EX) or $error_msg = "log_msg(): Can't lock $logfile: $!"; } # If ok, log msg contents if( !$error_msg ) { print CLIENT_LOG "$datestamp : $$ : $log_msg\n"; # Release lock & close flock(CLIENT_LOG, LOCK_UN) or $error_msg = "log_msg(): Can't unlock $logfile: $!"; close(CLIENT_LOG) or $error_msg = "log_msg(): Can't close $logfile: $!"; } # Log & email admin if an error occurred if( $error_msg ) { print "$error_msg\n"; # desperation / belt & braces send_email($error_msg); } }
Cheers
Chris

In reply to Re: File locking by chrism01
in thread File locking by Nalina

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.