in reply to error stating flock requires more args
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
|
|---|