N
nntp
Counter 1: from Perldoc
use Fcntl qwDEFAULT :flock);
sysopen(FH, "numfile", O_RDWR|O_CREAT) or die "can't open numfile:
$!";
flock(FH, LOCK_EX) or die "can't flock
numfile: $!";
$num = <FH> || 0;
seek(FH, 0, 0) or die "can't rewind
numfile: $!";
truncate(FH, 0) or die "can't truncate
numfile: $!";
(print FH $num+1, "\n") or die "can't write
numfile: $!";
close FH
Counter 2:
01: #!/usr/bin/perl -w 02: use strict;
03: use Fcntl qwflock); 04: my $COUNTER = 'count.dat';
05: my $SEMAPHORE = $COUNTER . ".lock"; 06: print
qq{Content-Type: text/html\n\n}; 07: open(LOCK, ">$SEMAPHORE") or die
"Can't open $SEMAPHORE ($!)";
08: flock(LOCK, LOCK_EX); 09: open(DATA, $COUNTER) or die
"Can't open $COUNTER ($!)";
10: my $count = <DATA>;
11: close DATA; 12: $count++; 13: open(DATA,
"+<$COUNTER") or die "Can't open $COUNTER ($!)";
14: print DATA $count;
15: close DATA;
16: close LOCK; 17: print qq{You are number
$count!};Logically, Counter 2 looks perfect, but why would perldoc uses a
flawed counter? I also don't understand the difference of seek(FH, 0, 0)
or die "can't rewind numfile: $!"; truncate(FH, 0)
or die "can't truncate numfile: $!";and open ">$filename";
use Fcntl qwDEFAULT :flock);
sysopen(FH, "numfile", O_RDWR|O_CREAT) or die "can't open numfile:
$!";
flock(FH, LOCK_EX) or die "can't flock
numfile: $!";
$num = <FH> || 0;
seek(FH, 0, 0) or die "can't rewind
numfile: $!";
truncate(FH, 0) or die "can't truncate
numfile: $!";
(print FH $num+1, "\n") or die "can't write
numfile: $!";
close FH
Counter 2:
01: #!/usr/bin/perl -w 02: use strict;
03: use Fcntl qwflock); 04: my $COUNTER = 'count.dat';
05: my $SEMAPHORE = $COUNTER . ".lock"; 06: print
qq{Content-Type: text/html\n\n}; 07: open(LOCK, ">$SEMAPHORE") or die
"Can't open $SEMAPHORE ($!)";
08: flock(LOCK, LOCK_EX); 09: open(DATA, $COUNTER) or die
"Can't open $COUNTER ($!)";
10: my $count = <DATA>;
11: close DATA; 12: $count++; 13: open(DATA,
"+<$COUNTER") or die "Can't open $COUNTER ($!)";
14: print DATA $count;
15: close DATA;
16: close LOCK; 17: print qq{You are number
$count!};Logically, Counter 2 looks perfect, but why would perldoc uses a
flawed counter? I also don't understand the difference of seek(FH, 0, 0)
or die "can't rewind numfile: $!"; truncate(FH, 0)
or die "can't truncate numfile: $!";and open ">$filename";