5

I want to display a set of values on screen and update that value every 5 seconds. I don't want to clear the screen.

eg:

hours: 1

mins : 30

sec: 45

here, values should change accordingly.

How should i do that in Perl?

Regards, Anandan

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Anandan
  • 983
  • 3
  • 13
  • 28

3 Answers3

14

Are you talking about getting more control over where things are printed on your screen? Then you probably want to check out the Term::Cap module.

A poor man's way to do this on one line is to use \r to keep overwriting the same line.

while ($t>0) {
    # note no new line at the end of printf statement
    printf "\rHours: %d  Minutes: %d  Seconds: %d     ", $t/3600, ($t/60)%60, $t/60;
    sleep 5;
    $t -= 5;
}

EDIT Here's something that works on my system. Your terminal's capabilities may vary.

require Term::Cap;
$terminal = Tgetent Term::Cap { TERM => cygwin, OSPEED => 9600 };
$terminal->Trequire("up");  # move cursor up
$UP = $terminal->Tputs("up");
$t = 500;
while ($t > 0) {
    printf "Hour: %d    \n", $t/3600;
    printf "Minute: %d    \n", ($t/60)%60;
    printf "Second: %d    \n", $t%60;
    print $UP,$UP,$UP;
    sleep 5;
    $t -= 5;
}
cjm
  • 61,471
  • 9
  • 126
  • 175
mob
  • 117,087
  • 18
  • 149
  • 283
  • Don't use \r in these cases. It's a logical character, not a specific bit pattern, so it's not always what you think it is. Specify it as the ASCII character you want instead. – brian d foy Sep 23 '09 at 17:16
  • I've changed this from `ku` (the characters generated by pressing the up arrow key) to `up` (the capability for moving the cursor up). It's possible these are the same on your terminal, but that's not true in general. – cjm Nov 15 '11 at 19:25
  • @briandfoy, I am just curious, could you elaborate on why not to use `\r`? Thats how I would have accomplished this. – Joel Berger Nov 16 '11 at 01:59
12

For this sort of thing I like to use Curses. It's just not for Perl, either. :)

brian d foy
  • 129,424
  • 31
  • 207
  • 592
-4

Something like this:

use Term::ANSIScreen qw(cls);
while(1) {
    cls;

    print "....";

    sleep 5;
}

Alternatives of "cls" can be found in this question.

Community
  • 1
  • 1
gugod
  • 830
  • 4
  • 10