Perl, touch and silly hit counters
While looking at how to set a file's time stamp in Perl (Perl's
Oh, and by the way:
Update (since this little entry seems to rank strangely high for "Perl touch" searches):
As POS points out in a comment below,
For a real
`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?Well, it did amuse me. So much indeed, that I'm actually about to add a page counter to http://alma.ch/perl.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.
:-)
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: perl
7 Comments:
That's pretty good, but I would eliminate the random factor... much better punchline if you emulate thousands of hits per second.
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.
Thanks for the `touch` function, exactly what i was looking for !
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.
there's a list of free PERL books available for download for reference:
Top 4 Perl Books for free download.
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!)
For the perl equivalent of touch(1), try File::Touch, available from your favourite CPAN site.
Post a Comment
<< Home