Why does adding or removing a new line change the way perl is used for loop functions?

I'm starting to learn perl using the Wrox Beginning Perl, available at perl.org , and you have a question regarding an example for the loop, they are given in chapter 3.

#!/usr/bin/perl

use warnings;
use strict;

my @count = (1..10);
for (reverse(@count)) {
        print "$_...\n";
        sleep 1;
}
print "Blast Off!\n"

This is the script that they provide, and works as expected. It displays a number followed by ... every second, waiting for a second between each number. When completed,Blast Off!

However, if I delete a new line from the print statement, the behavior will change. The script waits quietly for 10 seconds, and then displays all 10 numbers at Blash Off!once. Why change?

#!/usr/bin/perl

use warnings;
use strict;

my @count = (1..10);
for (reverse(@count)) {
        print "$_...";
        sleep 1;
}
print "Blast Off!\n"
+3
source share
4 answers

Perl print, . $| = 1 , Perl , :

#!/usr/bin/perl

use warnings;
use strict;

$| = 1; #Add this line
my @count = (1..10);
for (reverse(@count)) {
        print "$_...";
        sleep 1;
}
print "Blast Off!\n"'
+11

. . , :

my $ofh = select(STDOUT); $| = 1; select $ofh;

:

my $ofh = select(STDOUT); - STDOUT ( ) , , $ofh

$| = 1; - ( STDOUT) .

select $ofh; - , , , - , STDOUT, .

+4

You are viewing the standard line output behavior of the C stdio library when writing to the terminal.

See autoflush for perl details.

+4
source

An easier way than using select () to set the autoflush flag to STDOUT is to use IO :: Handle (which has been included in the Perl kernel since version 5.004):

use IO::Handle;
STDOUT->autoflush(1)
+1
source

Source: https://habr.com/ru/post/1758837/


All Articles