1. Perl
  2. File input/output
  3. here

Standard input Standard output Standard error output

I will write about standard input, standard output, and standard error output.

  1. Standard input
  2. Standard output
  3. Standard error output

1. Standard input

The standard input is read to the keyboard. You can use standard input to receive input from the keyboard. If you write a program that outputs the characters received from the keyboard as they are, it will be as follows. A file handle called STDIN represents standard input.

use strict;
use warnings;

# Read a line from the file handle that represents the standard input STDIN.
my $line = <STDIN>;
print $line;
If you read from STDIN, you can see that it is read from the keyboard.

2. Standard output

The output destination of the standard output is the display. You can write to standard output and see it on the display. A file handle called STDOUT represents standard output.

use strict;
use warnings;

print STDOUT $line;

The print function prints to STDOUT by default, even if you do not explicitly specify STDOUT.

3. Standard error output

The output destination of the standard error output is the same as the standard output and is the display. Standard error output is a function for notifying the user of an error. A file handle called STDERR represents standard error output. If you write the following, the display will show "aaa" and "bbb".

use strict;
use warnings;

print STDOUT "aaa\n";
print STDERR "bbb\n";

What is the role of standard error if both output to display? It is to separate standard and error output.

The OS has a function called redirection, which allows you to switch the output destination of standard output from the display to a file.

At this time, the standard error output does not switch and the output destination remains the display. There is a standard error output to prevent situations where the error is written to a file and the error is unknown.

If you redirect the standard output to a file as shown below, only the standard error output will be shown on the display.

perl script name> file

Related Informatrion