Re^3: What's the deal with apostrophes?
by afoken (Chancellor) on Jun 09, 2009 at 19:50 UTC
|
I still see a problem with your code: You are not counting "pages requested", but "pages requested with a browser that supports and allows Javascript". The real page request counts are in the access log of your web server. If you don't have access to that, you could deliver all pages through a perl script that counts the request.
Oh, and by the way: You are using file locks when you update the counter file, right? And you check that the file name passed by the browser is one of the files you want to update, right?
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] |
|
|
ok, hmmm... Some new points to ponder. I guess I kind of assumed that javascript was pretty ubiquitous. I do have access to "real" server access logs provided by my host. But where's the fun in that, when I can write scripts to do things?
Ummm... file locks? I am not updating the counter file myself, the perl program in the cgi-bin directory does that. It is just a simple increment script that increments the number in the file supplied to it by the calling command. I have made sure that there is a count file for every page, with the same name as the content page, but with no extension (it seems the perl code wants it this way), and that the javascript call in the webpage has the name of the count file for that page. So it all lines up nicely.
Now I am trying to figure out how to add LWP to my cygwin installation so that I can request the count files, and put the numbers into a report that shows each page that is visited. To be sure, I can validate this against the server logs if I want, but again, where's the fun in that?
It's great to have come this far after so long, but now I want to do all the cool stuff that the big people do with real websites, but I can still only barely crawl.
Can you explain the file locking thing a little more for me please? If need be, I can supply the count program code, to check it to see if it locks files.
Thanks for your help.
| [reply] |
|
|
I guess I kind of assumed that javascript was pretty ubiquitous.
The most recent data I've seen claims that 93% of users have javascript available, but I'm not clear on whether that's defined as "using a javascript-capable browser" or "have javascript turned on".
Personally, I use Firefox with the NoScript plugin, which prevents javascript from running unless I've specifically whitelisted the site that the javascript-containing document came from. So I do have javascript... but I won't be running yours unless you give me a reason to.
Also keep in mind that, in general, spiders and other bots don't process any javascript in the pages they encounter. Depending on your objectives, this may be either an advantage or a disadvantage to using javascript.
I do have access to "real" server access logs provided by my host. But where's the fun in that, when I can write scripts to do things?
What about writing scripts to parse and summarize the access logs, then generate pages (maybe even graphs) for you to easily view this information? That sounds a lot more interesting to me than one that just does "open a file, increment the number, write it back out".
| [reply] |
|
|
|
|
Can you explain the file locking thing a little more for me please?
Suppose your site gets so much traffic, you have two people requesting your page almost at the same time. So you have two instances of your count program opening the counter file, reading in the value, incrementing it, and writing the new value back. If the second instance reads the value before the first instance has written the new value, your count will be off. And you're back to writing a script processing the log files. Which I would think is far more enjoyable than writing 1995 style counters, but you don't.
| [reply] |
Re^3: What's the deal with apostrophes?
by tallCoolOne (Initiate) on Jun 10, 2009 at 03:33 UTC
|
Esper,
Is there a particular command or system variable to re-set to change from the default search delimiter / to the examples you have given # ?
It seems a little odd to me to just change characters for something like that and expect perl to "know" that's what they are there for. Does it really work that way?
Again, you have my gratitude for your enlightening information.
Mark | [reply] |
|
|
It seems a little odd to me to just change characters for something like that and expect perl to "know" that's what they are there for. Does it really work that way?
Yes, it does really work that way... more or less.
The one catch is that, when you use alternate delimiters, you have to explicitly use either "m" or "s" at the beginning of the regex. The "m" and "s" are the actual operators which (in the right context) trigger a regex operation, so when perl sees those followed by punctuation, it recognizes that the punctuation used will be the delimiter for that regex without needing to expicitly set or reset anything.
So, for example, all of these variations are equivalent:
#!/usr/bin/perl
use strict;
use warnings;
my $text = 'This is a text string.';
print $text =~ m/is a/, " - m//\n";
# Default delimiter, so the "m" can be omitted
print $text =~ /is a/, " - //\n";
# Custom delimiters, so the "m" is mandatory
print $text =~ m#is a#, " - m##\n";
print $text =~ m!is a!, " - m!!\n";
print $text =~ m^is a^, " - m^^\n";
# Open/close punctuation is used in pairs
print $text =~ m[is a], " - m[]\n";
print $text =~ m(is a), " - m()\n";
print $text =~ m<is a>, " - m<>\n";
The print on each regex displays the number of matches found along with the delimiters used:
1 - m//
1 - //
1 - m##
1 - m!!
1 - m^^
1 - m[]
1 - m()
1 - m<>
| [reply] [d/l] [select] |
|
|
That is about the coolest thing I've seen in perl since ice cubes. Pretty amazing really.
Thanks! That's one of the most helpful, useful tips that I have gotten here. This will make for a lot of simplifying changes in my code from now on.
*bows in respect and wonderment at the awesomeness of perl, and the generosity of the monks*
| [reply] |