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

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

  1. 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?
     
    , Sep 11, 2006
    #1
    1. Advertisements

  2. David Squire Guest

    You could do something like this:

    ----

    #!/usr/bin/perl

    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;
    }

    __DATA__
    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.


    DS
     
    David Squire, Sep 11, 2006
    #2
    1. Advertisements

  3. Guest

    what does this do?

    $| = 1; # output autoflush mode

    thanks
    -sean
     
    , Sep 11, 2006
    #3
  4. David Squire Guest

    wrote:

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

    perldoc perlvar


    DS
     
    David Squire, Sep 11, 2006
    #4
  5. 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
     
    Ted Zlatanov, Sep 13, 2006
    #5
    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.