CIT 597 <STDIN>, <STDOUT>
and <STDERR> in Perl
Fall 2008, David Matuszek
In Perl, <STDIN> is the "standard input" file, <STDOUT> is
the "standard output" file, and <STDERR> is
the "standard error" output file.
The statement $line = <STDIN>; will read one line from
standard input and assign it to the variable $line. It will
return a false value if there are no remaining lines.
The statement print $line; will print the value of the variable $line on
standard output.
The default is that <STDIN> refers to keyboard
input, and <STDOUT> and <STDERR>
refer to console (screen) output. But these are only defaults, and can
be changed.
The Perl program
# Usage: copyfile.pl < inputFile > outputFile
while ($line = <STDIN>) {
print $line;
}
will read in all the lines from
<STDIN> and copy them to <STDOUT>.
On the command line, < means "use this input file" and > means
"use this output file." So (if the above program is on the file
copyfile.pl), the command
perl copyfile.pl < inputFile.txt > outputFile.txt
will copy the file inputFile.txt to the file outputFile.txt.
"Your program should accept input from STDIN and produce
its results on STDOUT."
...and what I meant by this was, it should act like any other command-line
program, and use the files given on the command line as <STDIN> and
<STDOUT>. With just perl copyfile.pl and
nothing more, it would take its input from the keyboard and produce
output on the screen.
The above trivial program works fine on the command line, and does exactly what it is supposed to do.
I have been unable to make this program accept command-line arguments under
EasyEclipse. Whatever I try, <STDIN> refers to the
keyboard, and <STDOUT> goes to the console.
I get the same behavior in both Mac OS X and Windows XP. What am I
missing?
I'll offer ten points extra credit to the first person to offer a good solution to this problem, provided I have time to post the solution before the Perl assignment is due.