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


Files in Perl

The file-cabinet analogy carries neatly into Perl. The Perl functions that allow you to manipulate files are:

  open
  close
  read
  print
  write

It’s always best to learn by doing, so rather than wading into a lengthy discussion of these file functions, why don’t you write a program to handle the rudimentary requirements of the access counter? Enter the following code, which we will analyze shortly:

   #!/perl/bin/perl

   # access.pl
   #
   # First version.  Creates or opens a file with a number
   # in it, increments the number, writes it back.

       $CountFile = "counter.dat";      # Name of counter file.

   # 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 and exit.

       $Counter += 1;
       print COUNT $Counter;
       print "$CountFile has been written to $Counter times.\n";
       close (COUNT);
   #                    End access.pl

Save this sample code as access.pl and run it a few times from the command line. Figure 4.4 illustrates what you should see.


FIGURE 4.4:  The results of running the simplest form of the access counter.

Opening, Closing, and Writing Files

When you ran access.pl you probably noticed that it uses only two of the new Perl functions listed above: open and close. They really are all you need to do simple file manipulation.

And, as usual when using new Perl stuff, you saw some strange and unfamiliar operators. Let’s dissect this program so you can see what you’ve done.

The first program line sets a program variable:

   $CountFile = "counter.dat";    # Name of counter file.

The file’s name is counter.dat. Of course, you can call it anything you want, within the file-naming rules of your operating system. Whatever you do, it is best to do it here, at the top of the program, and put it in a variable. You could have referred to counter.dat throughout the program, but what if you decided to change it? By putting the name in a variable, you’ve ensured that it only has to be changed once. Laziness and impatience win the day again!

File Handles

The next two code lines in access.pl open the file and read whatever is in it.

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

The arguments to open take this form

   open (HANDLE, Expression);

where Expression usually is a file name or some variable that contains one. But what is this HANDLE?

When you open a file, you actually are instructing Perl to perform a rather complex series of steps. Information about the file is stored in the computer’s memory where you can deal with it if you want, and the processes that allow you to read from it and write to it are initialized. Perl then needs a handy way to refer to the file, so it sets up a handle, which you can treat as a variable name for the file. Until the file is closed and the handle is put back into circulation, you will perform all of your file operations on this handle.


TIP:  MS-DOS and Windows mavens may recall an instruction in their config.sys files that goes files=40 or some other number. Technically, the number refers to the maximum file handles that the operating system can have opened at any one time.

The next line of the program shows the handle in action, and it also shows a useful shortcut.

   $Counter = <COUNT>;         # Read the contents.

You can read directly from a file handle by putting brackets (<>) around it, as you did with <COUNT>. Because you can be reasonably certain that counter.dat contains either one number or nothing, you can simply set the value of the $Counter variable to whatever you read from the file.

But what about the first time, through, when counter.dat doesn’t even exist yet? When you open a nonexistent file name for reading, Perl returns a NULL handle. So, setting a scalar variable to the handle’s contents would effectively set the variable to 0.


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.