next up previous contents
Next: Using the Diamond Operator Up: Files Input Previous: Files Input

Some Files Are Standard

In an effort to make programs more uniform, there are three connections that always exist when your program starts. These are STDIN, STDOUT, and STDERR. Actually, these names are file handles. File handles are variables used to manipulate files. Just like you need to grab the handle of a hot pot before you can pick it up, you need a file handle before you can use a file.

The three special file handles are always open STDIN, STDOUT and STDERR.

STDIN reads from standard input which is usually the keyboard in normal Perl script or input from a Browser in a CGI script. Cgi-lib.pl reads from this automatically.

STDOUT (Standard Output) and STDERR (Standard Error)by default write to a console or a browser in CGI.

You've been using the STDOUT file handle without knowing it for every print() statement in this book. The print() function uses STDOUT as the default if no other file handle is specified. Later in this chapter, in the "Examples: Printing Revisited" section, you will see how to send output to a file instead of to the monitor.

Example: Using STDIN

Reading a line of input from the standard input, STDIN, is one of the easiest things that you can do in Perl. This following three-line program will read a line from the keyboard and then display it. This will continue until you press Ctrl+Z on DOS systems or Ctrl-D on UNIX systems stdin.pl.

while (<STDIN>) {

    print();

}

The <> characters, when used together, are called the diamond operator. It tells Perl to read a line of input from the file handle inside the operator. In this case, STDIN. Later, you'll use the diamond operator to read from other file handles.

In this example, the diamond operator assigned the value of the input string to $_ . Then, the print() function was called with no parameters, which tells print() to use $_ as the default parameter. Using the $_ variable can save a lot of typing, but I'll let you decide which is more readable. Here is the same program (stdin2.pl) without using $_.

while ($inputLine = <STDIN>) {

    print($inputLine);

}

When you pressed Ctrl+Z or Ctrl+D, you told Perl that the input file was finished. This caused the diamond operator to return the undefined value which Perl equates to false and caused the while loop to end. In DOS (and therefore in all of the flavors of Windows), 26-the value of Ctrl+Z-is considered to be the end-of-file indicator. As DOS reads or writes a file, it monitors the data stream and when a value of 26 is encountered the file is closed. UNIX does the same thing when a value of 4-the value of Ctrl+D-is read.

Tip When a file is read using the diamond operator, the newline character that ends the line is kept as part of the input string. Frequently, you'll see the chop() function used to remove the newline. For instance, chop($inputLine = <INPUT_FILE>);. This statement reads a line from the input file, assigns its value to $inputLine and then removes that last character from $inputLine-which is almost guaranteed to be a newline character. If you fear that the last character is not a newline, use the chomp() function instead.

Example: Using Redirection to Change STDIN and STDOUT

DOS and UNIX let you change the standard input from being the keyboard to being a file by changing the command line that you use to execute Perl programs. Until now, you probably used a command line similar to:

perl -w 09lst01.pl

In the previous example, Perl read the keyboard to get the standard input. But, if there was a way to tell Perl to use the file 09LST01.PL as the standard input, you could have the program print itself. Pretty neat, huh? Well, it turns out that you can change the standard input. It's done this way:

perl -w 09lst01.pl < 09lst01.pl

The < character is used to redirect the standard input to the 09LST01.PL file. You now have a program that duplicates the functionality of the DOS type command. And it only took three lines of Perl code!

You can redirect standard output to a file using the > character. So, if you wanted a copy of 09LST01.PL to be sent to OUTPUT.LOG, you could use this command line: perl -w 09lst01.pl <09lst01.pl >output.log

Keep this use of the < and > characters in mind. You'll be using them again shortly when we talk about the open() function. The < character will signify that files should be opened for input and the > will be used to signify an output file. But first, let's continue talking about accessing files listed on the command line.



 
next up previous contents
Next: Using the Diamond Operator Up: Files Input Previous: Files Input
dave@cs.cf.ac.uk