Output buffering for Perl

Source: Internet
Author: User

When you write a small script today, you need to immediately output the current progress to the command line, and immediately write important data to the report file. But Perl by default is output buffered, display to the command line must be at the end of the line, output to the file, the carriage return does not have to wait until the buffer is full.
Code:

Select (STDOUT);
$| = 1;
Open (Report, ">report.txt") | | Die "Create report Error: $!\n";
Select (report);
$| = 1;
Select (STDOUT);

This can be done before writing the report and outputting the progress. The first sentence is to select the standard output handle as the current default output handle and set it to non-buffered mode. Then select the file handle as the default output handle, set to non-buffered mode. The final reset stdout is the default output handle, and without this sentence, the standard output is redirected to the file.
This will instantly write the report without worrying about losing the data. Even if the procedure is interrupted halfway, there is a part of the result. Do a little tip, lest forget, hehe.

Output buffering for Perl

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.