next up previous contents index
Next: Command line arguments Up: Input/Output Previous: Input/Output   Contents   Index


Standard Input/Output/Error

We have already seen in earlier examples how to print something out to the screen:
  print qq{How are you?};
By default, the print function will print out to what is called standard output, which is normally the terminal screen. You can specify this explicitly by saying
  print STDOUT qq{How are you?};
where STDOUT is what's called the file handle associated with standard output. There is also a standard error:
  print STDERR qq{A fatal error has occurred!};
which is normally used for reporting errors. By default, this is also tied to the terminal screen - we will see later how to redirect both STDOUT and STDERR to other locations.

To get user input, you can use standard input, with an associated file handle STDIN:

  print qq{Please input a number: };
  my $answer = <STDIN>;
This will first print out the message, and then wait for the user to type something. After the user presses the return key, what was typed is captured into the specified variable $answer. This includes the end carriage return - if you want to remove that from the variable, you can use chomp($answer);; this is such a common task that often one sees
  chomp(my $answer = <STDIN>);
as a way to get keyboard input.
next up previous contents index
Next: Command line arguments Up: Input/Output Previous: Input/Output   Contents   Index