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


The Counter: Making It Graphical

Once you have installed Ghostscript on your server, you’re ready to go. The UNIX installation requires that you compile the Ghostscript files first, but the process generally puts everything where it’s supposed to go. If you’re not afraid of your C compiler, it probably won’t let you down.

On Windows NT and 95 systems, create a directory called GS and extract the two program archives, gs503ini.zip and gs503w32.zip, there. Unzip the PostScript Type 1 fonts (in gs503fn1.zip) in GS\fonts.


TIP:  The Ghostscript interpreter for Windows looks by default in the GS directory for its program and other input files and in GS\fonts for its fonts. You may put the files anywhere you like, but then Ghostscript needs to be told about it in a rather tedious set of command-line instructions. It’s best to go with the defaults.

The Ghostscript interpreter runs from a very intimidating command line. It is, after all, a powerful program that can do lots of things. You won’t have to worry about much of it, though, because most of what you need from Ghostscript will be set up in a Perl script.

Let’s take another look at the access counter you wrote in Skill 4.

   #!/perl/bin/perl

    # access.pl
    #
    # Second version. Creates or opens a file with a number
    # in it, increments the number, writes it back, then displays
    # the result in a message on a Web page.

        require "perl-cgi/html.pl";               # Get HTML header,
                                                    ender.
        $CountFile = "counter.dat";               # Name of counter
                                                    file.
        $PageTitle = "Web Page Access Counter";   # Web page title.

    # Open the file and read it.  If it doesn’t exist, its "contents"
    # will be read into a program variable as "0".

        open (COUNT, $CountFile);
        $Counter = <COUNT>;                       # Read the contents.

    # Close the file, then reopen it for output.

        close (COUNT);
        open (COUNT, ">$CountFile");

    # Increment $Counter, then write it back out. Put up a message
    # with the new value. Close the file.

        $Counter += 1;
        print COUNT $Counter;
        close (COUNT);

    # Put the result up in a standard HTML document.

        &HTML_Header ($PageTitle);                # HTTP header info.
        print "<BODY>\n";
        print "<H1>$PageTitle</H1>\n";            # Big heading.
        print "<HR>\n";                           # Draw a rule.
        print "<H3>You are visitor #$Counter ";
        print "to our Web page!</H3>\n";
        &HTML_Ender;

   #                    End access.pl

The additions you need to make to access.pl to allow it to hook up with Ghostscript are lengthy, so it would be best to put them in a subroutine. Add the following code to the end of the program:

   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;

   # Invoke Ghostscript as a "pipe" with open().

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

   # Now print the PostScript code out to the interpreter.

       print GS <<WE_ARE_FINISHED;
   %!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

       close (GS);
       return (1);

   }                   # End MakeGraphicalCount()

Now you need to add a line to access.pl that calls MakeGraphicalCount right after the count value is obtained from the file and incremented:

   # Increment $Counter, then write it back out.  Put up a message
   # with the new value.  Close the file.

        $Counter += 1;
        print COUNT $Counter;
        close (COUNT);
   &MakeGraphicalCount ($Counter);

The count won’t be printed as a heading any more, but will be called up as a JPEG image file instead. One more small change to access.pl will take care of it.

   # Put the result up in a standard HTML document.

        &HTML_Header ($PageTitle);           # HTTP header info.
        print "<BODY>\n";
        print "<H1>$PageTitle</H1>\n";       # Big heading.
        print "<HR>\n";                      # Draw a rule.
   ⁦   print "<H3>You are visitor </H3>\n";
   →   print "<IMG SRC=\"counter.jpg\">\n";
   ᜆ   print "<H3> to our Web page!</H3>\n";
       &HTML_Ender;

  #                    End access.pl

Put this new-and-improved version of access.pl in the appropriate directory and run it as a URL from your favorite Web browser. The result will be similar to the page displayed in Figure 8.6.


Figure 8.6:  The access counter is now displayed from a graphics file.


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.