system("clear"); has asked for the wisdom of the Perl Monks concerning the following question:

I am building a simple timer script and I wanted to make a loading bar that corresponded to the time. But unfortunately I can't get it to print without making a new line Thanks... I'm not to good a perl

#!/usr/bin/perl use Math::Round qw(:all); print "Minutes?\n"; $minutes = <>; chop $minutes; $timeleft = 60*$minutes; system("clear"); while($timeleft >= 0){ $devided = $timeleft/60; $rounded = nlowmult (1, $devided); $seconds = $timeleft-($rounded*60); print "$rounded minutes and to $seconds seconds left\n"; $ran = 30/$timeleft; $count = $ran*$timeleft; while ($count >=0 ){ print "#"; $count--; } sleep (1); system("clear"); $timeleft--; } print "TIME UP!!!!!!\a"; while (1){ print "\a"; print "\a"; }

Replies are listed 'Best First'.
Re: Printing without a newline?
by davido (Cardinal) on Apr 19, 2014 at 05:51 UTC

    You are Suffering from Buffering. You need to make your output filehandle hot, by disabling buffering. You do this by adding local $| = 1; before you print.

    $| is documented in perlvar.


    Dave

      Thanks! it works :)
Re: Printing without a newline?
by AnomalousMonk (Archbishop) on Apr 19, 2014 at 01:22 UTC

    The  -l command-line parameter will cause the contents of $\, undefined by default, to be assigned  "\n" and also to be appended at the end of print each time it is called (see perlrun): are you using  -l on your command line?

    Also, setting  $| to true causes unbuffered output: each print is printed whether or not it has a newline terminator. (Output is buffered by default.) See  $| in perlvar and also Suffering from Buffering.

    c:\@Work\Perl>perl -wMstrict -e "{ local $| = 1; ;; for my $n (reverse 1 .. 5, '(unbuffered)') { sleep 1; print qq{$n }; } } ;; for my $s ('(buffered)', 'Blast', 'Off', 'Now', qq{\n}) { sleep 1; print qq{$s }; } " (unbuffered) 5 4 3 2 1 (buffered) Blast Off Now
Re: Printing without a newline?
by Desdinova (Friar) on Apr 19, 2014 at 01:00 UTC
    All your print statements seem to end with \n this is what creates the new lines
      oops... srry i forgot to undo that... its fixed now