Tuesday, December 21, 2004

Perl, touch and silly hit counters

While looking at how to set a file's time stamp in Perl (Perl's `touch`), I stumbled across this in the perl faq:

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.

[8 lines of boring and ugly code]

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.
:-)

Well, it did amuse me. So much indeed, that I'm actually about to add a page counter to http://alma.ch/perl.

Oh, and by the way: `touch` in Perl is utime in case that is what actually brought you here.

Update (since this little entry seems to rank strangely high for "Perl touch" searches):

As POS points out in a comment below, utime will not create the file if needed, as `touch` would. To update-or-create, try something like utime(time, time, "file") or ( open(F, ">file") && close F )

For a real `touch` clone, you probably need a little function, or even the big one from the Perl Power Tools project.

Labels:

7 Comments:

Anonymous flash q fiasco said...

That's pretty good, but I would eliminate the random factor... much better punchline if you emulate thousands of hits per second.

11 May, 2005 07:04  
Anonymous Alex said...

Thanks for the tip .. I googled for touch and Perl and your blog was one of the first pages up. Sure enough, p.824 explained everything. I'll add that to the Camel index for the next lucky person who needs to do a touch inside a Perl script.

18 April, 2006 20:47  
Anonymous Anonymous said...

Thanks for the `touch` function, exactly what i was looking for !

09 May, 2006 11:22  
Anonymous POS said...

utime will only update the creation/modification times of a file, it will not create the file like the UNIX equivalent will (which is what I'm looking for).

Just FYI.

08 June, 2006 04:11  
Anonymous Anonymous said...

there's a list of free PERL books available for download for reference:

Top 4 Perl Books for free download.

30 August, 2006 09:46  
Anonymous Steve said...

If you came here looking for touch in Perl to create a file...

Although touch is often used to create files, that's not what it's designed for.

People do use it for creating, but looking for a version of touch in Perl for the sake of creating files indicates that you could do with learning more about why it is used.

The main purpose of touch is to modify the timestamps on a file.

The reason for using touch is that if the file doesn't already exist it creates it, but the creating the file is just a side effect of the timestamp setting functionality.

If you just need to create a file in Linux/Unix/whatever, instead of doing:
touch new.filename
you can do the same thing with simply:
> new.filename

Once you understand that you get closer to the Perl syntax for opening a filehandle in write mode (which is what you need to do to create a file that doesn't exist yet!)

20 December, 2007 22:35  
Anonymous ~dom said...

For the perl equivalent of touch(1), try File::Touch, available from your favourite CPAN site.

20 March, 2008 15:37  

Post a Comment

Links to this post:

Create a Link

<< Home