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


That’s why there isn’t a comma between COUNT and $Counter in the example. If there were, print would treat COUNT as a scalar or list variable and try to write it to the standard output, leading to strange and possibly indecipherable results. In any event, nothing would get into the file to which COUNT refers.

The read and write Functions in Perl

Perl has just about all the simple functionality you need when you’re dealing with the most common manipulations on a file. The best example of this is the one you just finished: You were able to write a program that creates, reads, and writes to a file without once invoking the Perl functions that specifically refer to these actions, read and write.

Do you need them both? Most definitely, especially read.

In access.pl, your only responsibility was to read the entire contents of counter.dat into a scalar variable. There will be many occasions when you don’t want to do this. You will instead want to read a chunk from the file, process the chunk, and then get another.

read is the tool of choice for such operations. It expects at least three, possibly four, arguments:

       read (HANDLE, BUFFER, LENGTH, OFFSET);

You know HANDLE; it’s just a file handle. BUFFER is a scalar variable that will hold the chunk of the file that you read, and LENGTH is the number of characters—or bytes—in the chunk. For example,

       read (COUNT, $Buffer, 256);

would read 256 bytes into $Buffer from the file referred to by COUNT.

OFFSET is not used that often and is therefore optional. It tells read to load the bytes into BUFFER at a different starting point than the beginning.

The write function is almost never used in normal Perl file processing. print is preferable—and, in fact, more correct—in nearly every case you will encounter.

write is used to put formatted records into a file. It is intended to be used with reports printed on paper, so its functionality veers toward formatting headings, pages, and such.

We will not use write in any program in this book.


WARNING:  Putting the comma between the file handle and the first string argument to print is such an easy gaffe to commit that Larry Wall placed it second in the “Common Goofs for Novices” section of his definitive book, Programming Perl (O’Reilly & Associates, Inc., 1996).

The remaining lines in access.pl write a nice message to the screen and close the file. Nothing new here, right? Now you’re ready to turn the program into a CGI application and bring it to the Web!

Bringing Your Counter to the Web

You have the rudiments of a Web page access counter written already. You also should have a fair-to-middling understanding of how files work in Perl. However, access.pl will only work from the command line at this point, and we want it to run on the Web. Let’s make this basic program CGI-ready.

You’ve Been Here Before

You already know which wrappers need to be fit over a Perl program to turn it into a CGI application. Here’s a recap of what it needs to do:

  Send a proper HTTP header to the Web server.
  print information you want displayed in HTML format.
  Send a proper HTML ender to the Web server, so it knows when you’re finished.
  Run the program as a URL in a Web browser.

Following these rules, we can fit the wrapper over access.pl. Here’s how you do it:

   #!/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

Save this as access.pl again, overwriting the old version, and run it, this time as a URL in your Web browser. Figure 4.5 illustrates what you should see.


Figure 4.5:  The access counter moves to the Web.


WARNING:  Make sure that the directory in which you have installed access.pl has “write” permission set for users coming in from the World Wide Web, or make the program put counter.dat in some other accessible directory. Otherwise, the file will never be written and the only number of visitors you’ll ever see is 1!


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.