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

Standard I/O and redirection

Redirection is an OS function that switches the input source of standard input, standard output, and output destination of standard error output. Modern operating systems have basic redirection capabilities.

There are three redirects to keep in mind: a redirect that switches the input source of standard input to a file, a redirect that switches the output destination of standard output to a file, and a redirect that switches the output destination of standard error output to a file.

1. Preparation

I will make an example to see the redirect movement.

Reads one line from standard input and writes to standard output. It also outputs the string "error\n" to the standard error output.

Save this script with a name of your choice. Here, the name is example_20080731.pl.

When outputting to a file by redirection, please note that the overwrite check of the file is not performed.

use strict;
use warnings;

my $line = <STDIN>; # Read one line from standard input

print $line; # Output to standard output
print STDERR "error\n"; # Output to standard error output

2. Check normal movement

perl example_20080731.pl

Do the above

aaa

And hit from the keyboard

aaa
error

Is output. The standard input is the keyboard, and the standard output and standard error output are the display.

3. Switch the input source of standard input to a file

Since the input source is switched from the keyboard to the file, create a file called input_20080731.txt as a preparation and add the contents

line

will do.

perl example_20080731.pl <input_20080731.txt

And hit on the command line

line
error

Is displayed on the display. The

4. Switch the output destination of standard output to a file

perl example_20080731.pl > output_20080731.txt

At the command line

aaa

When you type, the content aaa is written in output_20080731.txt, and error is displayed on the display. The> symbol is a redirect symbol that switches the output destination of standard output to a file. If the program is written to output the output contents to the standard output, the user can select the output destination by using the redirect.

5. Switch the output destination of standard error output to a file

perl example_20080731.pl 2> error_20080731.txt

At the command line

aaa

If you type, error_20080731.txt will show error and the display will show aaa. The 2> symbol is a redirect symbol that switches the output destination of standard error output to a file. Use this switch if you want to output the error to a file.

6. Use redirects in combination

Redirects can be used in combination.

perl example_20080731.pl <input_20080731.txt> output_20080731.txt 2> error_20080731.txt

Will be written as line in output_20080731.txt and error in error_20080731.txt. The line written in output_20080731.txt is the one written in the first line of input_20080731.txt. In this way, redirect symbols can be used in combination.

Related Informatrion