flushing buffer for printing to the screen

E

efoss

I want to be certain that a print statement is immediately executed.
Googling around led me to use "$| = 1". Is this correct? Here is an
example of how I'm using it:

my $test;

$| = 1;

$test = 3;

print "test is $test\n";

Thanks.

Eric
 
J

Jürgen Exner

I want to be certain that a print statement is immediately executed.
Googling around led me to use "$| = 1". Is this correct? Here is an
example of how I'm using it:

$| = 1;
$test = 3;
print "test is $test\n";

Yes, that is the correct use of $|.
However it only tells perl to not buffer the output and has no impact on
whatever buffereing your OS or terminal server or server-client protocol
(if applicable) decides to do.

jue
 
U

Uri Guttman

JE> Yes, that is the correct use of $|.

actually that never uses $| ! the print string ends in newline so it
will always be flushed to stdout. $| is meant for when you print text
without a newline (say a prompt or partial output or to a socket).

also since the program ends there, you also won't see any difference
with setting $| or not as stdout gets flushed then as well. you need to
print a string without a newline and then sleep or wait for input or
something to see the difference.

uri
 
W

Willem

Uri Guttman wrote:
) actually that never uses $| ! the print string ends in newline so it
) will always be flushed to stdout. $| is meant for when you print text
) without a newline (say a prompt or partial output or to a socket).
)
) also since the program ends there, you also won't see any difference
) with setting $| or not as stdout gets flushed then as well. you need to
) print a string without a newline and then sleep or wait for input or
) something to see the difference.

AFAIK, not true if stdout is redirected to a file.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
P

Peter J. Holzer

JE> Yes, that is the correct use of $|.

actually that never uses $| ! the print string ends in newline so it
will always be flushed to stdout. $| is meant for when you print text
without a newline (say a prompt or partial output or to a socket).

No. When the file handle (here STDOUT) doesn't point to a tty, it is
only flushed when the buffer is full not after each line.
also since the program ends there, you also won't see any difference
with setting $| or not as stdout gets flushed then as well.

This is correct.

hp
 
U

Uri Guttman

W> Uri Guttman wrote:
W> ) actually that never uses $| ! the print string ends in newline so it
W> ) will always be flushed to stdout. $| is meant for when you print text
W> ) without a newline (say a prompt or partial output or to a socket).
W> )
W> ) also since the program ends there, you also won't see any difference
W> ) with setting $| or not as stdout gets flushed then as well. you need to
W> ) print a string without a newline and then sleep or wait for input or
W> ) something to see the difference.

W> AFAIK, not true if stdout is redirected to a file.

then the original question is moot. obviously he is asking about stdout
to a terminal where this matters (or a socket which i covered). and for
sockets, the correct call is syswrite anyhow. you can even use it for
stdout and bypass all buffering and not need to deal with $|.

uri
 
U

Uri Guttman

BM> Huh? That's only if the filehandle is line-buffered; AFAIK PerlIO only
BM> supports line-buffering for direct output to a tty.

and the bare print in his code goes to stdout which goes to the tty by
default. hence my comments which assumed those things. otherwise $| is
fairly useless. see my other post for more.

uri
 
U

Uri Guttman

PJH> No. When the file handle (here STDOUT) doesn't point to a tty, it is
PJH> only flushed when the buffer is full not after each line.

but as i keep posting, the code as shown goes to stdout and the tty
unless he redirects it. it is obvious he is looking for the support of
print to the terminal without a newline. that is almost the only real
use for $|.

PJH> This is correct.

uri
 
A

Alan Curry

|but as i keep posting, the code as shown goes to stdout and the tty
|unless he redirects it. it is obvious he is looking for the support of
|print to the terminal without a newline. that is almost the only real
|use for $|.

My most usual real use for $| is when I've got normal output going to
stdout and verbose trace messages going to stderr, and want to pipe them
both into a pager. $| keeps them in order
 
P

Peter J. Holzer

PJH> No. When the file handle (here STDOUT) doesn't point to a tty, it is
PJH> only flushed when the buffer is full not after each line.

but as i keep posting, the code as shown goes to stdout and the tty
unless he redirects it. it is obvious he is looking for the support of
print to the terminal

Only because he wrote so in the subject ("printing to the screen"). I
admit that I didn't notice that when I first replied.

I don't think this is obvious from his code at all. As you already
noticed, his code does use a newline (so it's flushed) and it exits
immediately after the print (so it's flushed again).

without a newline. that is almost the only real use for $|.

I almost never use it for that. Most of the time I use it to get line
buffering for file output (I.e., each print (or printf) writes a whole
line).

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top