Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What does "$|=1" actually mean/do?

Status
Not open for further replies.

NeilFawcett

Programmer
Mar 19, 2004
30
0
0
GB
What does "$|=1" actually mean/do?
 
Forgive me if I'm not 100% true but it's how you remove the buffer in Perl. Adding this line will make nearly all things print to the screen as they happen rather than witing for the buffer to receive XX amount of information before it'll display it back to you.
 
Here's a little something I wrote to demonstrate $|.
If you supply a non-zero value on the command line, the countdown prints as it's happening. Otherwise, nothing is printed 'til blastoff, when the \n flushes the buffer.
Code:
#!perl
# Fun (?) with autoflush.  
# Without $| > 0, nothing is printed 'til blastoff since 
# the prints have no \n.
# See perldoc perlvar.

$| = shift;
print qq(Countdown: );
for (reverse 0..10) {
    printf qq(%2d), $_;
    sleep 1;
    print qq(\b) x 2
}
print qq(\nBlastoff!\n);
 
Can you explain to me then why I don't see the digits appearing each second, and only all at the end with:-
Code:
#!/usr/bin/perl
$| = 1;
print "Content-type: text/html\n\n";
for (reverse 0..5) {
print "$_<BR>";
sleep 1;
}
print "Done";
exit;
This is tried on a Unix machine and at my home on Windows machine with Apache .
 
Well, 'cause you modified it, I guess.
And you're running it in a browser, no?
I was running it from a command prompt.
 
So it has no meaning for web related scripts?!?

I'm sure I've seen it used when updating files as well, to make the software write to the file immediately!?
 
I must admit I'm pretty ignorant of web-related programming. I really can't explain why it behaves differently in that case. (Nor was I aware that it did.) Perhaps someone else knows?
 
You'll have to enclose the different outputs in some tags or other. I'm not totally clear on the rules that define which tags work and which don't - I thought it was limited to block-level elements but apparently not (the <span> and <b> tags seem to work too and they're both inline, I think). Seems like trial-and-error is the order of the day. For example:
Code:
#!/usr/bin/perl
$| = 1;
print "Content-type: text/html\n\n";
for (reverse 0..5) {
print "<div>$_<div>";
sleep 1;
}
print "Done";
exit;
Of course this may not be the behaviour in all browsers (I'm at work now so only have access to IE5)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top