Click here to get back home

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

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 5.22 I still don't get locking. I just want to increment the number in the file. How can I do this? PerlFAQ Server 06-02-2008
Get Chitika Premium
Posted by PerlFAQ Server on June 2, 2008, 9:03 am
Please log in for more thread options
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.22: 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(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 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.

Similar ThreadsPosted
FAQ 5.20 I still don't get locking. I just want to increment the number in the file. How can I do this? January 20, 2005, 6:03 am
FAQ 5.21 I still don't get locking. I just want to increment the number in the file. How can I do this? April 18, 2005, 11:03 am
FAQ 5.21 I still don't get locking. I just want to increment the number in the file. How can I do this? July 4, 2005, 10:03 am
FAQ 5.21 I still don't get locking. I just want to increment the number in the file. How can I do this? September 20, 2005, 10:03 am
FAQ 5.21 I still don't get locking. I just want to increment the number in the file. How can I do this? November 13, 2005, 11:03 am
FAQ 5.21 I still don't get locking. I just want to increment the number in the file. How can I do this? August 8, 2006, 3:03 am
FAQ 5.21 I still don't get locking. I just want to increment the number in the file. How can I do this? November 14, 2006, 3:03 pm
FAQ 5.21 I still don't get locking. I just want to increment the number in the file. How can I do this? January 5, 2007, 3:03 am
FAQ 5.21 I still don't get locking. I just want to increment the number in the file. How can I do this? February 17, 2007, 3:03 pm
FAQ 5.21 I still don't get locking. I just want to increment the number in the file. How can I do this? May 4, 2007, 9:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap