in reply to Safe Counter Follow Up
in thread Safe Counter
You'll want something like this:
#!/usr/bin/perl -w $| = 1; use strict; use Fcntl qw(:flock); my $count_file = 'counter.txt'; open( my $fh, '+<', $count_file ) or die( "open failed: $!" ); flock( $fh, LOCK_EX ) or die( "flock failed: $!" ); ++ ( my $count = <$fh> ); seek( $fh, 0, 0 ) or die( "seek failed: $!" ); truncate( $fh, 0 ) or die( "truncate failed: $!" ); print $fh $count; close( $fh ) or die( "close failed: $!" );
|
|---|