![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Dissecting the Graphical Access CounterWhile the amount of code you just read may be intimidating, what weve 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 well cover the highlights of that shortly. But there are several new Perl concepts brought up, too. Lets 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 subroutinenot 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 whats 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 lets 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.
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. Heres an explanation of the other components of the Ghostscript command line:
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-bucketthey 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 programs 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
|
![]() |
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. |