separator

Take a quiz or make one of your own
The CGI Bin is brought to you by
 
Face 2 Interface
Tutorial 3 - Web Hit Counter Using an Image Tag
How Tricky Is This Trick? :
When I first learned this way of doing things I thought, "Gosh, that's a tricky thing to do!" You see, by putting a program name inside the image tag, the server simply executes that program instead of serving up an image from off of a static file. The thing is we're not really interested in the image per se, only the fact that we can run our program in the course of generating the image.
Nonetheless we're going to have to output an image too. Or else the web page will have a broken link for the image and that'll look ugly on older browsers. On newer browsers it'll be fairly inconspicuous, just a 1x1 pixel dot, because that's how large we're making the image. Of course while we're in this program outputting the image, we might as well do our little counting thing :^).
Modularization and Subroutines :
There's a mouthful for you. What's it mean? Modularized programming is a technique where a program is broken up into small pieces (called modules, duh) which work together to produce all of the functionality of the complete program, only we break it up into these smaller pieces.
Subroutines are a popular way to implement a modular design. And beginning with this program we are going to do everything in a modular fashion, so there's a few things to explain.
  • all variables we use from now on will be declared with the my Perl operator, which tells Perl that we are setting up a certain variable of the given name
  • we will henceforth issue the Perl compiler directive use strict; which will insure that only explicitely declared variables are used in our programs
  • by combining 'use strict' and 'my variables' we will help prevent confusion, a large part of what a modularized approach accomplishes, any variable that isn't declared will fail in the compile phase and the program will not run until we fix it
  • subroutine names, when called from elsewhere will be prefixed with the & character so that we know at a glance that we're referring to a subroutine, while it's not required, personally I think it's a darn good idea
So without further adieu, let's look at the program..

separator

© thecgibin.com