in reply to flock problem

You're going to kick yourself, you need to change "function;" to "function();"(We've all done this at one time or another).

You also shouldn't use an exclusive lock when reading from a file, you should use a shared lock.
This is how I got the code to work.

#start of login.cgi open(FILE, ">file.log"); flock(FILE, 2); # 2 is an exclusive lock print FILE "$a:$b:$c\n"; close FILE; #call function function (); # return 0; #end of login.cgi ------------------- ;#start of login.pl sub function { open(FILE, "<file.log"); flock(FILE, 1); # 1 is a shared lock $a = <FILE>; print "line: $a\n"; close FILE; #end of login.pl 1; }

Replies are listed 'Best First'.
(chromatic) RE: Re: flock problem
by chromatic (Archbishop) on Jul 14, 2000 at 22:37 UTC
    The bareword function call may not be a problem, if login.pl has been required before the code we see. I always use the parenthesis on function calls, though, just to make things clear.

    I prefer to use the symbolic constants from Fcntl rather than the numbers, for the lock type argument to flock. It's just safer:

    use Fcntl qw(:DEFAULT :flock); # from the Perl Cookbook open(FILE, ">file.log") or die "Can't open and truncate file: $!"; flock(FILE, LOCK_EX) or die "Can't obtain exclusive lock: $!"; print FILE "$a:$b:$c\n"; close FILE;
    That'll clobber file.log, by the way. (if you don't want to do that, see mikfire's post here.)

    The code for login.pl is similar, except it's LOCK_SH there.