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

I like to create a view where user can see the last 10 lines of the view. So the effect would look like the 'flow of the text'. For example I could feed the numbers and user can see that within the view of 10 lines, the numbers are sequentially growing/flowing. How I can make it possible in Perl?.
Thanks,
artist.

Replies are listed 'Best First'.
Re: Text Animation in Perl
by Roger (Parson) on May 07, 2004 at 15:34 UTC
    You could have a look at Term::ANSIScreen or Curses, assuming that you are working with terminals. Every time you do a screen update in your code, just jump to the positions at the bottom of the screen, and print whatever.

    use Term::ANSIScreen qw/:color :cursor :screen/; ... my %vals = ( Number1 => { X => 1, Y => 20, Width => 10 }, Number2 => { X => 11, Y => 20, Width => 10 }, ... ); sub SetValue { my ($name, $val) = @_; locate $vals{$name}{X}, $vals{$name}{Y}; print $val, ' ' x $vals{$name}{Width} - length($val); # or # printf "%-$vals{$name}{Width}s", $val; } ... for (0..10) { SetValue(Number1, 100 + $_); SetValue(Number2, 200 + $_); sleep(1); } ...

Re: Text Animation in Perl
by pizza_milkshake (Monk) on May 07, 2004 at 16:25 UTC
    i'd use curses. however, unfortunately, the Curses module is barely documented, so it'll be hard to use for someone who isn't already familiar with curses (and even so, the module makes changes that aren't documented). also, it's not a standard module, so perl -MCPAN -e'install Curses' in order to run this example.
    #!perl -l use strict; use warnings; use Curses; use Time::HiRes qw(usleep); initscr(); my ($winh, $winw, $winx, $winy, $line) = qw(10 80 0 0); my $win = newwin($winh, $winw, $winx, $winy) or die $!; $win->scrollok(1); for my $n (0 .. 100) { $line = $n > $winh ? $winh : $n; $win->move($line, 0); $win->addstr("line#$n\n"); $win->refresh(); usleep 10_000; } endwin();

    perl -e"\$_=qq/nwdd\x7F^n\x7Flm{{llql0}qs\x14/;s/./chr(ord$&^30)/ge;print"

Re: Text Animation in Perl
by Plankton (Vicar) on May 07, 2004 at 16:46 UTC
      Another simple(-minded) solution is to just clear the screen before each refresh. You can find the clear string with the following code:
      my $clearchr; if (eval { require Term::Cap }) { my $terminal = Tgetent Term::Cap { TERM => undef }; $clearchr = $terminal->Tputs("cl"); } if ($clearchr eq '') { $clearchr = `clear`; }
      Disclaimer: this will work on Unix, no idea about Windows or other OSes.