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

Hello,

I'm missing something here.... I'm trying to print "Message1"; then do some lengthy processing, then I'd like to print "Message 2"; OVER the top of "Message 1".

Notice I did not use "\n";

based upon suggestions, I tried:

$| = 1; print 'mystuff' ; print "\rOVERWRITE"; ---------------------------------------- BAD OUTPUT: mystuff OVERWRITE ---------------------------------------- Desired, final OUTPUT: OVERWRITE

How do I do this?

--Fred

-------------------------------------
Nothing is too wonderful to be true
-- Michael Faraday

Replies are listed 'Best First'.
Re: Dynamically overwriting a line of text on stdout console
by Zaxo (Archbishop) on Jul 22, 2004 at 22:14 UTC

    Check to see if $\ is defined as "\n". It is undefined by default. Perl print will add on a $\ at the end of each print's arguments, as well as a $, between each argument.

    If it's defined because you need it, you can localize $\ for this bit.

    { local $\; print 'somestuff'; print "\rOVERWRITE\n"; }

    After Compline,
    Zaxo

      This in fact works:
      $\ = undef ; print 'somestuff'; print "\rOVERWRITE\n";
      But I don't understand, because:
      print "Text"; print " MoreText\n";
      Does not get printed as:
      Text MoreText
      as $\ being "\n" would seem to imply. I am using version 5.005.

      -------------------------------------
      Nothing is too wonderful to be true
      -- Michael Faraday

Re: Dynamically overwriting a line of text on stdout console
by beable (Friar) on Jul 22, 2004 at 23:44 UTC
    You might want to look at the good old Curses module, which will give you complete control over the output. Here's a simple example:
    #!/usr/bin/perl use strict; use warnings; use Curses; initscr; addstr(10, 15, "mystuff"); refresh; sleep 3; addstr(10, 15, "OVERWRITE"); refresh; sleep 3; endwin; __END__
Re: Dynamically overwriting a line of text on stdout console
by pg (Canon) on Jul 22, 2004 at 23:14 UTC

    The other way is to use \b.

    use strict; use warnings; $| ++; my $msg = ""; for (1..100) { $msg = "\b" x length($msg) . $_; print $msg; sleep 1; }
Re: Dynamically overwriting a line of text on stdout console
by ysth (Canon) on Jul 22, 2004 at 22:19 UTC
    Make sure you aren't using the -l flag.

    Is this output going to the terminal screen? That's the only way \r is going to work, since "mystuff\rOVERWRITE" is what is actually written, and it depends on the terminal to interpret \r as carriage return.

    If you are outputing to a pipe or a file, you are out of luck, unless you use some utility that formats things as if it were a terminal screen (I think I've seen such a beast), or you intercept the output with a tied filehandle or an io layer that interprets things for you.

Re: Dynamically overwriting a line of text on stdout console
by gellyfish (Monsignor) on Jul 23, 2004 at 09:27 UTC

    You can do this more flexibly with the module Term::Cap which is part of the core distribution:

    use Term::Cap; + my $term = Term::Cap->Tgetent(); + print $term->Tputs('cls'); print $term->Tgoto('cm',1,1); + print "foo"; + print $term->Tgoto('cm',1,1); + print "foo";

    /J\