Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Dissecting the Graphical Access Counter

While the amount of code you just read may be intimidating, what we’ve just done is quite simple in concept. The Web site access count is obtained and calculated in precisely the same way it was in Skill 4. However, with Ghostscript at your disposal, the count could be formatted as a graphical image and placed in a file, which then could be called into the page with the HTML <IMG SRC=> tag.

The new access.pl introduces quite a bit of PostScript code, and we’ll cover the highlights of that shortly. But there are several new Perl concepts brought up, too. Let’s cover those in order.

Notice that the beginning of MakeGraphicalCount has several calls to a function called local:

   sub MakeGraphicalCount
   {
       local ($GS) = "/gs/gswin32c.exe";      # Ghostscript interpreter.
       local ($count) = @_;                   # Store counter locally.
   # Set the size of the graphical image in a couple of variables.

       local ($x) = 75;
       local ($y) = 40;

Variables in Perl are global, that is, a variable declared in one place is recognized throughout the program, subroutines and all. Sometimes, especially in subroutines, you will want to use variables that are local to the subroutine—not recognized outside of it. This is the function performed by local.

In access.pl, a variable $x could have been declared in the main part of the program and it would have been recognized in MakeGraphicalCount. However, by declaring $x with the local function, we have ensured that its scope, or the time it remains valid, is only as long as the subroutine is executing.

Another line in MakeGraphicalCount illustrates an interesting shortcut that you can use with print:

   print GS <<WE_ARE_FINISHED;

The GS parameter is just the file handle opened to get to the Ghostscript interpreter. But what’s this <<WE_ARE_FINISHED?

This is known to some Perl programmers as the “here” function. In other words, the two less-than signs in front of a string tell print to print everything that follows until it encounters the string. The technique can be very useful when you want to print a large amount of unformatted text, as we did with the PostScript code that produced the graphical counter. What is especially nice about the “here” function is that Perl will recognize and interpret any variables you throw into the unformatted text.

Now let’s step through the subroutine. Notice that the path to the Ghostscript interpreter is immediately put into a variable.

   local ($GS) = "/gs/gswin32c.exe";      # Ghostscript interpreter.

The variable makes it easier to use the full path (less typing), and puts it in a convenient spot should it ever need to be changed.


TIP:  The command-line Ghostscript interpreter for Windows is called gswin32c.exe and it should be installed in the GS directory. In UNIX, the interpreter is called gs.

Two local variables, $x and $y, are declared next to hold the values of the size of the box in which to put the image. The numbers represent pixels; in our example, the box is 70 pixels wide by 40 pixels high.

Now, MakeGraphicalCount opens a file handle with the command to invoke the Ghostscript interpreter set up as a “pipe,” designated by the | symbol:

   open (GS, "|$GS -sDEVICE=jpeg -sOutputFile=./counter.jpg -q -dBATCH
   ⇒ -g${x}x${y} - 2>NUL");

The “pipe” means that anything written to the GS file handle will go directly to Ghostscript as input, which will happen shortly in a long print statement.

Here’s an explanation of the other components of the Ghostscript command line:

  -sDEVICE=jpeg specifies that Ghostscript should output JPEG-formatted data.
  -sOutputFile=./counter.jpg tells the interpreter to put its output in a file called counter.jpg in the current directory.
  -q calls for “quiet” mode—any messages from Ghostscript are suppressed.
  -dBATCH tells the interpreter to quit when it runs out of input.
  -g${x}x${y} specifies the boundaries of the image in the format LENGTHxWIDTH. Notice that the variables $x and $y were enclosed in brackets to protect them from the “x” in the middle.

Lastly, the – tells Ghostscript to take its input from the standard input rather than a file. The 2>NUL redirects error messages to the NUL bit-bucket—they are not printed.

Now, the “pipe” is opened and Ghostscript awaits your command. The print statement will ship whatever follows until it encounters the string WE_ARE_FINISHED. What follows is a PostScript program:

   %!PS-Adobe-2.0 EPSF-1.2
   %%BoundingBox: 0 0 $x $y
   %%EndComments
   /Palatino-Italic findfont 36 scalefont setfont
   /white    {1 1 1 setrgbcolor} def
   /black    {0 0 0 setrgbcolor} def
   black clippath fill
   10 10 moveto
   ($count) white show
   showpage
   WE_ARE_FINISHED

As you can see from this and the previous, longer example, PostScript is nothing more than text instructions to the interpreter. In this example, the three lines beginning with % are comments. The first line,

   %!PS-Adobe-2.0 EPSF-1.2

is actually similar to the first line in a Perl script. In this case, it tells Ghostscript to produce Encapsulated PostScript output, version 1.2.

The program’s boundaries are declared with BoundingBox, which takes four parameters: the origin (in the example 0, 0), and the upper bounds, in which we put the two variables we set up earlier.

The first line of actual PostScript code is

   /Palatino-Italic findfont 36 scalefont setfont


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.