http://qs1969.pair.com?node_id=937870

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

I want to print dots ....... just to make user know that job is still running, here is something in perl I am copying, but it prints dots in different lines. Is it possible to over write .... dots so that it dont fill the terminal

while (1) { for (1..20) {print "."; system ("sleep 0.05"); } print "\n"; }

Replies are listed 'Best First'.
Re: overwritting print in same line
by quester (Vicar) on Nov 14, 2011 at 06:30 UTC
    I'm not entirely sure that I understand your question, but if you just want to show that the script is running without using up screen space, consider using a "spinner" which twirls in place:
    use warnings; use strict; my $spin; while (1) { print substr( "-/|\\", $spin++ % 4, 1 ), "\b"; $| = 1; select undef, undef, undef, 0.05; }
    I changed your "sleep .05" to "select undef, undef, undef, 0.05" because the normal Perl sleep function works in seconds, so "sleep .05" is the same as "sleep 0". See sleep for an explanation and other alternatives. I also added "$|=1" to avoid having the output getting buffered and not displaying anything for long periods of time; see $OUTPUT_AUTOFLUSH.

      Cool spinner code. Thanks for sharing. Fun.

      Btw, The autoflush  $| = 1; can actually be put outside the while loop (above it, obviously), as it's a flag not a function in and of itself and so it doesn't require being repeatedly called for each spin segment...

Re: overwritting print in same line
by tweetiepooh (Hermit) on Nov 14, 2011 at 16:35 UTC
    If you just want to add dots then combine switching off buffering output with not printing "\n"
    $|=1; [some loop condition] print "."; [do other stuff] [end loop]
    This will print dots next to each other which maybe close to what you are asking for.
    Depending on what you are doing you may only want to print a dot every n iterations
    $|=1; $counter=0; [some loop condition] print "." unless $count++ % n; [do other stuff] [end loop]
Re: overwritting print in same line
by ansh batra (Friar) on Nov 14, 2011 at 06:48 UTC
    i know its not the correct solution
    just came to my mind so wanted to share
    while (1) { for (1..20) {print "."; system ("sleep 0.05"); } #print "\n"; system("clear"); }

      hi ansh, quester and anonymous thanks for the replies

      ansh: u suggested is tricky but it actually clear off the screen that what dont want to do that. As u can see spinner suggested by quester is quite good in the sense it doesnot clear off the screen. So if u just replace \b with \n it will print in separate lines. But with \b it is printing on same line.

      quester: this is what I wanted. But ethically i cant use ur code so I need to solve my problem. for example in ur code u replace \b with \n it print in separate line. This is what happening with my dots. But if u keep \b in ur code it prints -\/ in same line and "overwriting" it at same place.I dont understand ur code well. ALSO please can u explain ur code explicitly as exactly whats happening. I am somewhat slow :-(

      Thanks to anonymous also