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


Input, Output

The next two lines in access.pl are liable to be a little confusing to beginners.

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

Why do we close the file and then instantly re-open it? And why is $CountFile in quotes this time with a > character in front of it?

The > operator is the difference between input and output on a file in Perl. You might remember the > and < symbols as the operators for redirection of input and output in MS-DOS. These conventions actually were borrowed from UNIX, so it’s no wonder they’re used in Perl.

When you put the greater-than sign in front of the file name parameter to open, the file is opened for output, or writing, and it will create the file, if it doesn’t exist, or overwrite anything that is in the file. The less-than sign opens the file for input to the program, or reading, but this is so common that Perl regards it as the default if you leave it off. The append symbol (>>) will append anything you write to the file to what already is in the file.

You put quotes around the file name variable $CountFile to avoid confusing the Perl interpreter between the output redirection symbol and the $ to indicate a scalar variable.


TIP:  Try leaving the quotes out of “>$CountFile” and see what happens when you run the program.

So, the code snippet

   close (COUNT);
   open (COUNT, “>$CountFile”);

creates counter.dat the first time you run the program, and opens it for overwriting on any subsequent runs.

“Redirecting” Input and Output

UNIX and Windows 95 and NT, as well as the Windows progenitors all the way back to the early version of MS-DOS, interpret < and > (the greater-than and less-than signs) as special symbols. They are used to redirect the channels through which you normally interact with your computer, essentially the screen and the keyboard, into other places, usually files.

Normally, a command-line program expects its input to come from the keyboard and it ships its output to the screen. However, if you were to run a program called foo in this manner

       foo > bar

whatever foo is supposed to print to the screen would instead be written into a file called bar, which would be created if it didn’t exist already, or overwritten if it did.

Likewise, the line

       foo < bar

would expect the information that the user would normally enter through the keyboard to come from a file called bar.

One more redirection symbol combines two greater-than signs (>>) to create an append command. This command will tack any new data to the end of the file to its right, rather than overwriting it.

print Works with Files, Too

The last bit of code from access.pl brings up a couple of interesting new Perl concepts.

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

In the first line, $Counter += 1, the += is a shortcut borrowed from the C programming language. In this example, it is identical to writing:

   $Counter = $Counter + 1;

Why not simply write $Counter = $Counter + 1? Well, for one thing $Counter += 1 is shorter and therefore follows the twin precepts of laziness and impatience. However, some programmers might insist that the longer form is clearer, that is, that it more clearly states the intention of the code. In the end, it’s a matter of taste.


NOTE:  Probably the most common criticism of C as a programming language is that its inherent economy encourages programmers to write programs that are too terse. Perl, with its roots deep in the C language, must shoulder this criticism as well.


NOTE:  Perl’s += operator is one of a family that includes all of the arithmetic operators used in the language. You also can use -= for subtraction, *= for multiplication, and /= for division. The number on the right side of the equation doesn’t have to be 1, either, but can be any valid number or scalar variable.

The other line worthy of special mention in this code snippet is the second:

   print COUNT $Counter;

This brings up a feature of print that we haven’t covered yet: The function presumes that its first argument is a file handle. If it’s left out, print will use the current output file handle, which, unless you have done something special and specific, will be the standard output, or the screen.


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.