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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



ajithkn666
New User

Dec 4, 2008, 10:58 PM

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

Hi,

I tried running the following code...

--------------sleepEx.pl---------------------

#!/usr/bin/perl

print "\n Hi";

sleep(10);

print "\n Hello";

---------------End-------------------------

I intended for the following output,

Hi

(sleeps for 10 seconds)

Hello.



But I am getting the following output,

(Sleeps for 10 seconds..)

Hi

Hello.



Could anyone tell me this indifferent way of execution?


KevinR
Veteran


Dec 4, 2008, 11:59 PM

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

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


Code
print "\n Hi";  
$|++;#<-- flush the buffer
sleep(10);
print "\n Hello";

-------------------------------------------------


malcolm
Novice

Dec 5, 2008, 7:17 AM

Post #3 of 5 (538 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


KevinR
Veteran


Dec 5, 2008, 11:57 AM

Post #4 of 5 (534 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.
-------------------------------------------------


FishMonger
Veteran / Moderator

Dec 5, 2008, 3:34 PM

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


Quote
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
command.

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
variables.

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

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?
DEV->autoflush(1);

or IO::Socket:

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

$sock->autoflush();



(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