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

i was wondering if there's any easy way in perl to print a character or string to a specific position on the terminal screen (ie: 12th character on the 3rd line). for instance, say I want to have a command line program that monitors a protocol for a kind of transer like so:
Downstream: 0 Upstream: 0
now, i want to JUST replace the zeroes, without having to clear the screen and print it all out again. and i'd need to do this in win32 (if possible, *nix too)

Replies are listed 'Best First'.
Re: printing characters to specific position on screen
by BrowserUk (Patriarch) on Aug 08, 2005 at 21:21 UTC

    Try this:

    #! perl use strict; use Win32::Console; my $cons = new Win32::Console( STD_OUTPUT_HANDLE ); $cons->Cls; $cons->WriteChar( 'Downstream: 0 Upstream: 0', 12, 3 ); my( $up, $dn ) = (0) x 2; for ( 1 .. 1000 ) { Win32::Sleep 100; $cons->WriteChar( $up += int rand 100, 24, 3 ); $cons->WriteChar( $dn += int rand 100, 40, 3 ); }

    Obviously this won't work on *nix. You might find Win32::Console::ANSI makes it easier ot write a portable script.


    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".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      wow, using Win32::Console::ANSI as suggested, this is EXACTLY what i need...i can even throw in some coloring too!
Re: printing characters to specific position on screen
by Zaxo (Archbishop) on Aug 08, 2005 at 20:43 UTC

    On unix, Curses will do that.

    Often, you can print "\r" to return to the start of the line and overprint the current data with a whole new line.

    After Compline,
    Zaxo

      hmm, in win2k, \r prints an undistinguishable character and wraps to the next line....and \b doesnt even print.