dimar has asked for the wisdom of the Perl Monks concerning the following question:

print "Hello World"; sleep 2; print "Goodbye World"; sleep 2;

This is a very simple question about using 'sleep' to pause a 'DOS window'. Is there an alternative?

DESIRED BEHAVIOR: Open 'DOS window'; print "hello world", pause for a little; print 'goodbye world"; pause for a little, Close DOS window; DONE.

ACTUAL BEHAVIOR: Open 'DOS window'; DOS window remains blank; pause for a little; print "hello world"; print 'goodbye world"; Close DOS window immediately after printing, so fast that you cannot even see it; DONE.

Replies are listed 'Best First'.
Re: sleep on windows w activestate perl
by pizza_milkshake (Monk) on May 06, 2004 at 15:50 UTC
      Much agreed pizza_milkshake

      Perl isn't flushing the buffer of screen ouput until you \n. Adding \n to your print statements should fix it, I think (I've never actually played with it or had this problem, but it seems to be a common one)


      Grygonos
Re: sleep on windows w activestate perl
by davido (Cardinal) on May 06, 2004 at 17:40 UTC
    The sleep is occurring, but the output buffer isn't getting flushed until the script terminates. The reason is that the output buffer only gets flushed under one of three conditions: (1) The program terminates OR (2) You output a '\n' character OR (3) the buffer fills.

    You can modify this behavior by setting $| to 1 (turning on autoflush), or you can just end your printed lines with a newline character to force a buffer flush.


    Dave

Re: sleep on windows w activestate perl
by NetWallah (Canon) on May 06, 2004 at 22:07 UTC
    While I agree that output buffering is the problem, setting autoflush is the less-preferred solution in this case.

    I would recommend terminating each line with a "\n", as in

    print "Hello World\n";
    The newline will cause the buffer to flush without any fussing with autoflush. This is why you see most perl scripts terminate "print" lines with "\n" (It also makes the output lines separate, which , in most cases , is desirable.

    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarntees offense.
Re: sleep on windows w activestate perl
by cyocum (Curate) on May 06, 2004 at 15:47 UTC

    In ActiveState Perl, if you just click on the script a console comes up to run your script then exits. In the installation, it associates .pl in Windows for you. I am not sure this completely answers your question however.