For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2006 > Help with print









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help with print
yuanlii

2006-03-22, 3:57 am

This is the first time I post in this group, any help will be
appreciated!

I found a strange problem when I print some messages, the following
code:
#!/usr/bin/perl
use strict;
use warnings;

while (1) {
print "1\n";
sleep(1);
}

prints "1\n" every second, but the following:

#!/usr/bin/perl
use strict;
use warnings;

while (1) {
print "1";
sleep(1);
}
print nothing to the console.

What's the difference between "1" and "1\n"?
If I want an "1" without "\n", what should I do?

Thank you!

usenet@DavidFilmer.com

2006-03-22, 8:00 am

yuanlii wrote:
> print nothing to the console.


If you waited a LONG time, you would see some output. By default, Perl
buffers output. When you threw newlines into the mix, Perl dutifully
printed them, but Perl is buffering the output of your non-newline
statements. Your output will show up EVENTUALLY.

Add this near the top of your script and you will get what you expect:

$| = 1;

Cheers!

--
http://DavidFilmer.com

yuanlii

2006-03-22, 9:57 pm

Got it, thank you:)

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com