From: PerlFAQ Server on
This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

5.23: I still don't get locking. I just want to increment the number in the file. How can I do this?

Didn't anyone ever tell you web-page hit counters were useless? They
don't count number of hits, they're a waste of time, and they serve only
to stroke the writer's vanity. It's better to pick a random number;
they're more realistic.

Anyway, this is what you can do if you can't help yourself.

use Fcntl qw(:DEFAULT :flock);
sysopen my $fh, "numfile", O_RDWR|O_CREAT or die "can't open numfile: $!";
flock $fh, LOCK_EX or die "can't flock numfile: $!";
my $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 or die "can't close numfile: $!";

Here's a much better web-page hit counter:

$hits = int( (time() - 850_000_000) / rand(1_000) );

If the count doesn't impress your friends, then the code might. :-)



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
From: Ted Zlatanov on
On Thu, 12 Aug 2010 19:42:13 +0000 (UTC) dmcanzi(a)remulak.uwaterloo.ca (David Canzi) wrote:

DC> Advice and example code in the FAQs should be useful to typical
DC> perl users, who run the version of perl that came with their
DC> operating system. A quick scan of about 100 machines in my work
DC> environment didn't find any running perl 5.10.

I disagree. The FAQ should target the latest Perl.

Ted
From: Alan Curry on
In article <DZj8o.2656$yr6.1340(a)newsfe05.iad>,
PerlFAQ Server <brian(a)theperlreview.com> wrote:
>This is an excerpt from the latest version perlfaq5.pod, which
>comes with the standard Perl distribution. These postings aim to
>reduce the number of repeated questions as well as allow the community
>to review and update the answers. The latest version of the complete
>perlfaq is at http://faq.perl.org .
>
>--------------------------------------------------------------------
>
>5.23: I still don't get locking. I just want to increment the number in
>the file. How can I do this?
>
> Didn't anyone ever tell you web-page hit counters were useless? They

Code problems aside, are hit counters still popular enough for this joke
to work? Do new perl programmers now even know what it refers to? You
know, those people who when they say "RedHat 4", aren't referring to the
one that came with libc5, and who think "Seinfeld" comes from
approximately the same time period as "I Love Lucy"...

--
Alan Curry
From: David Canzi on
In article <87eie3imbs.fsf(a)lifelogs.com>,
Ted Zlatanov <tzz(a)lifelogs.com> wrote:
>On Thu, 12 Aug 2010 19:42:13 +0000 (UTC) dmcanzi(a)remulak.uwaterloo.ca
>(David Canzi) wrote:
>
>DC> Advice and example code in the FAQs should be useful to typical
>DC> perl users, who run the version of perl that came with their
>DC> operating system. A quick scan of about 100 machines in my work
>DC> environment didn't find any running perl 5.10.
>
>I disagree. The FAQ should target the latest Perl.

Why?

--
David Canzi | "If the human mind were simple enough for us to understand,
| we would be too simple-minded to understand it." -- anonymous
From: Ben Morrow on

Quoth Ted Zlatanov <tzz(a)lifelogs.com>:
> On Thu, 12 Aug 2010 19:42:13 +0000 (UTC) dmcanzi(a)remulak.uwaterloo.ca
> (David Canzi) wrote:
>
> DC> Advice and example code in the FAQs should be useful to typical
> DC> perl users, who run the version of perl that came with their
> DC> operating system. A quick scan of about 100 machines in my work
> DC> environment didn't find any running perl 5.10.
>
> I disagree. The FAQ should target the latest Perl.

IMHO the FAQ should follow current best practices, based on the latest
version of Perl commonly installed in production, but with 'If you are
using a perl before 5.X you will need to...' warnings where necessary.

I rather suspect autodie isn't widely enough used yet to count as 'best
practice', but I think it ought to become so.

Ben