I want to output all of my STDOUT to a single line....dont want to scroll

Discussion in 'Perl Misc' started by seancsnyder, Sep 11, 2006.

  1. seancsnyder

    seancsnyder Guest

    I want my program to simply output to a single line in STDOUT. In
    other words i want to print a line, then 'backspace' that line and
    print somethign new on the same line. I do not want the terminal
    window to scroll.

    Any ideas? CAn this be done?
    seancsnyder, Sep 11, 2006
    1. Advertisements

  2. seancsnyder

    David Squire Guest

    You could do something like this:



    use strict;
    use warnings;

    $| = 1; # output autoflush mode
    my $last_message_length = 0;
    while (my $message = <DATA>) {
    chomp $message;
    print "\r";
    print ' ' x $last_message_length;
    print "\r";
    print $message; # must not contain \n or \r
    sleep(1); # for example
    $last_message_length = length $message;

    First message
    Second message
    The third message is really very much longer. In fact, it is more than
    80 characters
    The fourth message is shorter


    But as you will most likely see if you run this, there are problems if
    the message is longer than your terminal width. You could also look into
    more robust solutions using, for example, the Curses module from cpan.

    David Squire, Sep 11, 2006
    1. Advertisements

  3. seancsnyder

    seancsnyder Guest

    what does this do?

    $| = 1; # output autoflush mode

    seancsnyder, Sep 11, 2006
  4. seancsnyder

    David Squire Guest


    [top-posting corrected. Please don't do that.]
    What the comment says! See:

    perldoc perlvar

    David Squire, Sep 11, 2006
  5. seancsnyder

    Ted Zlatanov Guest

    Besides the \r technique shown in this thread, you could also clear
    the screen.

    On Unix machines, you can often do it with the `/usr/bin/clear'
    program. IIRC on Windows it's `cls' or something similar.

    This is definitely slower than \r, so if your data updates quickly it
    won't look nice. It does look better in some circumstances, though
    (e.g. your data is longer than the screen width).

    Ted Zlatanov, Sep 13, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.