in reply to the manuals fail me

use Fcntl qw( LOCK_EX O_RDWR O_CREAT SEEK_SET ); my $qfn = '/tmp/myfile.index'; sysopen(my $fh, $qfn, O_RDWR|O_CREAT, 0666) or die("Can't create or open \"$qfn\": $!\n"); flock($fh, LOCK_EX); my $index = <$fh>; ++$index; seek($fh, 0, SEEK_SET); print($fh "$index\n"); #truncate($fh, tell($fh));

You could also do it without locking:

my $qfn = '/tmp/myfile.index'; open(my $fh, '<', $qfn) or die("Can't open \"$qfn\": $!\n"); my $index = <$fh>; ++$index; open($fh, '>', $qfn) or die("Can't create \"$qfn\": $!\n"); print($fh "$index\n");

Replies are listed 'Best First'.
Re^2: the manuals fail me
by Anonymous Monk on Mar 29, 2012 at 19:24 UTC
    The locking method appeals to me. Could you tell me what LOCK_EX should do, and which version of fcntl you use with it? My manpage does not mention it, and google is not helpful.
      ...and Perl only provides one flock, so no idea what "version of fcntl" means.
      LOCK_EX is an exlusive lock. See man flock(2)