Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Resetting your Terminal for each line printed

by ghenry (Vicar)
on Sep 27, 2006 at 07:44 UTC ( #575100=perlquestion: print w/replies, xml ) Need Help??
ghenry has asked for the wisdom of the Perl Monks concerning the following question:

Dear Master Monks,

I've got a blank mind this morning, as I am trying to think how to print on my console but wipe it after each print.

I am just testing like so, but I can't think of a better way (console width independent) than just printing a certain amount of blanks (by adding print ' ' x 80;):

#!/usr/bin/perl use strict; use warnings; # Autoflush Output $| = 1; for my $num (1..100) { print 'Testing long line print', "\r"; print 'Printing over', "\r"; print $num, "\r"; sleep 1; }

Should I search for some Term::* module?


Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a Just ask!!!

Replies are listed 'Best First'.
Re: Resetting your Terminal for each line printed
by Skeeve (Vicar) on Sep 27, 2006 at 07:50 UTC
    If I need something like that I always go for ANSI codes.
     print "\x27[2J";

Re: Resetting your Terminal for each line printed
by liverpole (Monsignor) on Sep 27, 2006 at 11:02 UTC
    Hi ghenry,

    From your question:

        (by adding print ' ' x 80;)

    it looks like you mean you wish to clear to the end of the line, is that correct?

    If so, use <escape>[K like so:

    print "\e[K";

    Some of the escape sequences I use all the time are:

    print "\e[H"; # Put the cursor on the first line print "\e[J"; # Clear from cursor to end of screen print "\e[H\e[J"; # Clear entire screen (just a combination +of the above) print "\e[K"; # Clear to end of current line (as stated +previously) print "\e[m"; # Turn off character attributes (eg. color +s) printf "\e[%dm", $N; # Set color to $N (for values of 30-37, or + 100-107) printf "\e[%d;%dH", $R, $C; # Put cursor at row $R, column $C (good fo +r "drawing")

Re: Resetting your Terminal for each line printed
by BrowserUk (Pope) on Sep 27, 2006 at 07:52 UTC

    Try this

    perl -wle"$|=1; printf qq[\r$_\t] and sleep 1 for reverse 1 .. 60"

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Resetting your Terminal for each line printed
by Fletch (Chancellor) on Sep 27, 2006 at 14:10 UTC

    There's Term::Cap which would let you find the current terminal's clear-to-end-of-line which you could print after your CR.

    Of course if you want to go the whole way there's also Curses.

    Update: The other solutions hard coding VT100-esque sequences are going to work on probably 70+% of the terminals you'll encounter (xterm, kconsole, iTerm, et al), but use Term::Cap if you want to make sure you work regardless of the terminal (presuming correct entries in the system's termcap file :).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://575100]
Approved by Skeeve
[Discipulus]: so few questions these days..

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2017-04-28 07:10 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (519 votes). Check out past polls.