in reply to Need sleep walking help

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.

Replies are listed 'Best First'.
Re^2: Need sleep walking help
by Todd Chester (Scribe) on Feb 01, 2017 at 08:18 UTC
    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.
        I don't see the need to use that select calls.

        Infact you are not using STDERR at all and in the case of a use of warn a newline is added anyway. From the docs:

        Prints the value of LIST to STDERR. If the last element of LIST does n +ot end in a newline, it appends the same file/line number text as die + does.

        In case of need of a granular control over buffer behaviour you can use the autoflush method from IO::Handle that is the base class for all other IO handle classes.

        In your example you just need to put $|++; after use strict; use warnings; lines.

        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.
        No, that is not correct.
        STDERR is "unbuffered" by default.
        STDOUT is buffered by default and is the currently selected output file handle by default. You do not need the "select" in this case.

        This should work fine...

        #!/usr/bin/perl use strict; use warnings; $|=1; # turns off stdout buffering mysleep(10); sub mysleep { my $seconds = shift; print "Sleeping for $seconds seconds ... "; for my $second (1..$seconds) { sleep(1); print " $second"; } print " done!\n"; }
        I would not use "Sleep" as the subroutine name. Lower case "sleep" is the library's function name. This kind of upper vs lower case thing can lead to big and confusing troubles! I used "mysleep" instead of "Sleep".

        PS: Something is very odd about your <code>...</code> tags. Do not enclose the <code>...</code> tags inside of something else.