c has asked for the wisdom of the Perl Monks concerning the following question:

The relevant code is:

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

I've read over flock and I seem to be using the exact syntax as in the description page, however when ran, my script returns:

Not enough arguments for lock at ./dnsmod.cgi line 232, near "lock()"

From the snippet above, line 232 is simply:

lock();

What additional arguments is it looking for?

humbly -c

Replies are listed 'Best First'.
Re: error stating flock requires more args
by tachyon (Chancellor) on Jul 31, 2001 at 19:30 UTC

    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

Re: error stating flock requires more args
by Hofmator (Curate) on Jul 31, 2001 at 18:35 UTC

    mmhh, interesting, there are two ways to fix that:

    • rename your lock routine
    • move the declaration of the lock-sub above the other subs
    it seems to me that there is a clash with the lock funtion (perldoc -f lock) from thread programming but I'm not sure on that. Can someone enlighten me??

    -- Hofmator

Re: error stating flock requires more args
by converter (Priest) on Jul 31, 2001 at 18:49 UTC

    I must be missing something here, this one is a bit confusing.

    lock is a perl built-in function, but perlfunc states that this is so only if perl is compiled with support for threads and if you 'use Threads;', and goes on to say that if thread support is not included, perl should call a user-defined subroutine with that name, but perl still seems to be calling the built-in and enforcing its prototype ($).

    The solution would seem to be:

    use subs qw/ lock /;
    This will ensure that your subroutine is called and that perl won't complain about the number of arguments. This gets rid of the error message with regard to the numer of arguments, but either I'm missing other important details, or there's an error in perl with regard to calls to lock when threading is not compiled in.

    conv

    Update: moving the definition of &lock above the call to it gets rid of the error, as does adding a declaration with empty prototype. I still don't understand why perl seems to be enforcing the built-in's prototype.

(tye)Re: error stating flock requires more args
by tye (Sage) on Jul 31, 2001 at 19:36 UTC

    If you are going to use all-lowercase names for your functions (or even all-uppercase names), then you need to call them like &this(...). See (tye)Re: A question of style for more info.

            - tye (but my friends call me "Tye")
Re: error stating flock requires more args
by physi (Friar) on Jul 31, 2001 at 18:24 UTC
    Try:
    lock(*FH); sub lock { my $fh = shift; flock($fh,LOCK_EX); }
    The reason is, that FH is not known to lock, cause it's only visible inside your add() sub.
    ----------------------------------- --the good, the bad and the physi-- -----------------------------------