Chapter 4. Handling Files in Perl
IO Introduction > STDOUT > Writing Files > STDIN > Reading Files >
Reading Directories > Editing Files > Recursive Editing
Standard output is a term used extensively in computer science to refer to the normal output destination of a program. When you are working with a command line interface, like DOS, the standard output is directed to your computer screen.
Standard output is written as STDOUT within Perl programs. Any output generated by a plain old print statement in your Perl program goes to STDOUT. When you write
print "The quick brown fox\n";
you are really asking that your output be sent to STDOUT.
STDOUT is an example of a thing called a filehandle. A filehandle is a special type of variable that is associated with an output destination. It is used to tell your program where you want output to go.
Writing to standard output is so common that Perl automatically prepares the STDOUT filehandle for you so you don't have to do anything extra to use it. You don't even have to specify it since a print statement sends output to STDOUT by default. If you wanted to specify it in your print statement, you'd include it right after the word "print" with no extra commas:
print STDOUT "The quick brown fox\n";
But, you don't need to since the default statement:
print "The quick brown fox\n"
is completely equivalent.
The neat thing about STDOUT is that it can be altered outside your program, with the "redirection" operator. So if you were running your Perl program and you wanted to keep the output for review instead of letting it flash by on the screen, you could redirect STDOUT with the ">" symbol and have the output sent to a file like this:
perl perltest.pl > output.txt
This would then put all the program's output into the file, output.txt. Note that this redirection is a feature of DOS, or Unix, and not of Perl in particular. But Perl supports writing to STDOUT, so that this redirection works. This means you can save your output to any file you choose, without altering the internal contents of your Perl script. That is a very handy and cool thing.
Email me your comments!