You have to make the output unbuffered.
You can do that by adding this:
$|=1;
Before the loop.
One Planet, One Internet...
We Are All Connected...
| [reply] [d/l] |
Often printing will be done immediately if you finish your text with an \n:
print "\nChecking something - \n";
HTH, Rata
| [reply] [d/l] [select] |
Often...
More specifically, when the buffering mode is set to "line-buffered" — which is the default with interactive things like terminals.
In general, and to summarize, there are three buffering modes:
- unbuffered — when explicitly requested, and the default for stderr
- line-buffered — the default in interactive contexts
- block-buffered (aka "fully buffered") — the default for everything else, like writing to files, pipes, etc.
In block-buffered mode, flushing doesn't happen before the respective buffer, e.g. 4k bytes size, has filled up.
For example (block-buffered):
$ strace -ewrite perl -E 'say "X"x99 for 1..100' >/dev/null
write(1, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"..., 4096) = 4096
write(1, "XXX\nXXXXXXXXXXXXXXXXXXXXXXXXXXXX"..., 4096) = 4096
write(1, "XXXXXXX\nXXXXXXXXXXXXXXXXXXXXXXXX"..., 1808) = 1808
Without the redirect of stdout, i.e. when it's connected to the terminal, you'd see 100 write system calls à 100 bytes... (line-buffered)
| [reply] [d/l] [select] |
| [reply] |
Let's give it an error to make it more interesting:
#!/usr/bin/perl
use strict;
use warnings;
foreach my $i ( 1..3 ) {
if ($i lt 3) {
print "\nChecking $i - ", "\n";
sleep 3;
print "OK\n";
}
else {
print "\nChecking $i - ", "\n";
sleep 3;
print "Not OK\n\n";
}
}
Updated: changed "something" to $i.
Updated: Found a better unbuffered way...
#!/usr/bin/perl
use strict;
use warnings;
foreach my $i ( 1..3 ) {
if ($i lt 3) {
print "\nChecking $i - " and
sleep 3 and
print "OK";
}
else {
print "\nChecking $i - " and
sleep 3 and
print "Not OK\n";
}
}
| [reply] [d/l] [select] |