I would code it like this:

#!/usr/bin/perl -w use strict; use Fcntl ':flock'; sub add { open FH, ">>$file" or die "Cant open config file $!\n"; flock(FH,LOCK_EX); print FH "zone \"$formdata{domain}\" in \{\n\ttype slave\;\n\tfile\" +$client\/db.$formdata{domain}\"\;\n\tallow-query \{ any\; \}\; \}\;\n +\n"; close FH; }

I see very little advantage in 1 line subs - the lock sub is three lines where 1 will do. If you wanted to make a more useful lock sub that included a timeout/retry feature you would need to pass the file handle to the sub. May as well pass the lock type too:

#!/usr/bin/perl -w use strict; use Fcntl ':flock'; sub add { open FH, ">>$file" or die "Cant open config file $!\n"; lock_file(*FH, LOCK_EX); print FH "zone \"$formdata{domain}\" in \{\n\ttype slave\;\n\tfile\" +$client\/db.$formdata{domain}\"\;\n\tallow-query \{ any\; \}\; \}\;\n +\n"; close FH; } sub lock_file { local *FH = shift; my $lock = shift; my $count = 0; my $timeout = 15; until (flock FH, $lock) { sleep 1; die "Can't get lock\n" if ++$count > $timeout; } }

When you close a file you automatically release any locks so you can just drop the whole concept of unlocking provided you explicitly close.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: error stating flock requires more args by tachyon
in thread error stating flock requires more args by c

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.