CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
sleep command working at first


New User

Dec 4, 2008, 10:58 PM

Post #1 of 5 (1413 views)
sleep command working at first Can't Post


I tried running the following code...


print "\n Hi";


print "\n Hello";


I intended for the following output,


(sleeps for 10 seconds)


But I am getting the following output,

(Sleeps for 10 seconds..)



Could anyone tell me this indifferent way of execution?


Dec 4, 2008, 11:59 PM

Post #2 of 5 (1411 views)
Re: [ajithkn666] sleep command working at first [In reply to] Can't Post

It appears that the output is being buffered. Try this:

print "\n Hi";  
$|++;#<-- flush the buffer
print "\n Hello";



Dec 5, 2008, 7:17 AM

Post #3 of 5 (1405 views)
Re: [KevinR] sleep command working at first [In reply to] Can't Post

When something is buffered, which special variable is it stored in

In Reply To


Dec 5, 2008, 11:57 AM

Post #4 of 5 (1401 views)
Re: [malcolm] sleep command working at first [In reply to] Can't Post

As far as I know its not stored in any variable. The buffering is done by the operating system, not perl. Maybe someone else knows more about the subject.

Veteran / Moderator

Dec 5, 2008, 3:34 PM

Post #5 of 5 (1397 views)
Re: [malcolm] sleep command working at first [In reply to] Can't Post

C:\>perldoc -q buffer
Found in C:\Perl\lib\pod\perlfaq5.pod
How do I flush/unbuffer an output filehandle? Why must I do this?

Perl does not support truly unbuffered output (except insofar as you can
"syswrite(OUT, $char, 1)"), although it does support is "command
buffering", in which a physical write is performed after every output

The C standard I/O library (stdio) normally buffers characters sent to
devices so that there isn't a system call for each byte. In most stdio
implementations, the type of output buffering and the size of the buffer
varies according to the type of device. Perl's print() and write()
functions normally buffer output, while syswrite() bypasses buffering
all together.

If you want your output to be sent immediately when you execute print()
or write() (for instance, for some network protocols), you must set the
handle's autoflush flag. This flag is the Perl variable $| and when it
is set to a true value, Perl will flush the handle's buffer after each
print() or write(). Setting $| affects buffering only for the currently
selected default file handle. You choose this handle with the one
argument select() call (see "$|" in perlvar and "select" in perlfunc).

Use select() to choose the desired handle, then set its per-filehandle

$old_fh = select(OUTPUT_HANDLE);
$| = 1;

Some idioms can handle this in a single statement:

select((select(OUTPUT_HANDLE), $| = 1)[0]);

$| = 1, select $_ for select OUTPUT_HANDLE;

Some modules offer object-oriented access to handles and their
variables, although they may be overkill if this is the only thing you
do with them. You can use IO::Handle:

use IO::Handle;
open(DEV, ">/dev/printer"); # but is this?

or IO::Socket:

use IO::Socket; # this one is kinda a pipe?
my $sock = IO::Socket::INET->new( '' );


(This post was edited by FishMonger on Dec 5, 2008, 3:35 PM)


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives