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

Dear Perl Monks,

Strawberry Perl 5.24.0.1 for Windows.

What am I doing wrong here, besides writing it in C like structure instead of Perl 5.

sub Sleep ( $ ) { my $Seconds = $_[0]; # Stone age sub's my $I = 1; print "Sleeping for $Seconds seconds ... "; until( $I > $Seconds ){ print "$I "; sleep ( 1 ); # sleep for X seconds $I += 1; } print "done\n"; }


It does wait the desired time, but I don't get to see it walk through the time.  This comes out all at once, "after" the desired delay.

Sleep ( 8 );

Sleeping for 8 seconds ... 1 2 3 4 5 6 7 8 done 

Many thansk,
-T

Replies are listed 'Best First'.
Re: Need sleep walking help
by Discipulus (Canon) on Feb 01, 2017 at 08:08 UTC
    hello,

    something change if put $|++; at the top of your code? I suspect yes.

    Infact you are not printing newlines and the console is linebuffered. Instead of $|++; you can also modify 'print "$I "; ' into 'print "$I\n"; ' to get the expected output.

    Look at these example and how the output come out:

    perl -e "sleep 1 and print qq($_ ) for 1..$ARGV[0]" 3 1 2 3 perl -e "$|++; sleep 1 and print qq($_ ) for 1..$ARGV[0]" 3 1 2 3

    I suggest you to read suffering from buffering? a milestone read and also Perl Idioms Explained - $|++

    Anyway you can do something simpler like:

    sub my_sleep { # no prototype my $Seconds = shift; print "Sleeping for $Seconds seconds ...\n"; # added newline sleep 1 and print "$_\n" for 1..$Seconds; print "done\n"; }

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      That is it. By adding a \n it will walk. Since \n is not the desired result, is there a way to flush the buffer? (I could not make heads or tails out of the "suffering from buffering" paper, maybe I am too much of a beginner.)
        the answer is already there:

        something change if you put $|++; at the top of your code? I suspect yes.

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Need sleep walking help
by AnomalousMonk (Archbishop) on Feb 01, 2017 at 19:34 UTC

    Todd Chester: Your code does not render as downloadable links here and here and in this recent thread because you're using  &lt;code&gt; and  &lt;/code&gt; instead of  <code> and  </code> respectively as your XML code tags. Please see Markup in the Monastery, Writeup Formatting Tips and Perl Monks Approved HTML tags (and possibly also How do I change/delete my post?), and then save yourself from going nuts with all the <br>s, &lt;s and &gt;s (update: not to mention the &nbsp;s) when you enter code.

    And yes, this is the XML Police.

    Update: I was a bit puzzled by the  my $Seconds = $_[0]; portion of the originally fake-&lt;code&gt;-tagged source here (now fixed). I had expected the  [0] to render as a link and it didn't. Turns out that  [] [0] don't linkify;  [01] [1] [10] etc. linkify as I expected. (The behavior of  [ ] (one or more spaces enclosed in square brackets) is trickier: the space(s) seem to linkify only if the opening  [ is preceded by a non-space "helper" character. Oh, well...) So add  &#91; &#93; to the list of entities you don't have to worry about if you use proper  <code> ... </code> or  <c> ... </c> tags. XML Police over and out.


    Give a man a fish:  <%-{-{-{-<