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