Perl: stdout for standard output and stderr for standard errors

Source: Internet
Author: User

Perl contains some predefined file handles. For example, standard input stdin and standard outputStdout, And standard errorsStderr. The standard error stderr is an additional output path. How can we understand this sentence? Let's look at an example.

 
%Perl-E 'print "Hello, world! \ Nabc "; print stderr" Hi \ n ";'

Output:

 
Hello, world! Hiabc

Instead:

 
Hello, world! Abchi

This is because the Perl print function is a row buffer. When \ n is met, print the "standard output" immediately. Because ABC does not touch the line feed, there is a buffer first, and the buffer is full before printing. The error output is not buffered, so it is printed immediately.

 
% Perl-e 'print "Hello, world! \ Nabc "; print stderr" Hi \ n "; '> err.txt

Output to err.txt:

 
Hello, world! ABC

Err.txt does not contain HI (HI is still output to the screen), but it only imports the standard output to the file. To import standard errors, you must:

 
% Perl-e 'print "Hello, world! \ Nabc "; print stderr" Hi \ n "; '> &err.txt

> & Only applicable to Unix andLinux. 2 In Windows>

Generally, print is equivalent to the standard output of print stdout. When output to a file, we can use stderr to output the error to the screen. In this way, Perl can instantly see errors (stderr) while working (stdout standard output to files ). (Note: I have always wanted to achieve this before, so I can understand it today .)

For example:

 
% Perl-e 'print stdout "Hello, world! \ N "; print stderr" error \ n "; '> err.txt

#######

The conclusion is that both stdout and none are the same. Both are standard output and can be output to the screen or redirected to the document.

Stderr is the standard error output, which can be output to the screen or redirected to the document. It is only necessary to add &,

 

 

 

1> OUTFILE 2> & 1 is the standard output, and all error outputs are written to OUTFILE.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.