2
#!/usr/bin/env perl
use warnings;
use 5.014;
use Term::Cap;
use POSIX;

my $termios = new POSIX::Termios;
$termios->getattr;
my $ospeed = $termios->getospeed;

my $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
$terminal->Trequire("ku");  # move cursor up
my $UP = $terminal->Tputs("ku");
my $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;
}

When I try this (found here: How can I update values on the screen without clearing it in Perl?) I get this output:

Hour: 0    
Minute: 8    
Second: 20    
AAAHour: 0    
Minute: 8    
Second: 15    
AAAHour: 0    
Minute: 8    
Second: 10    
AAAHour: 0    
Minute: 8    
Second: 5 

Does this mean, that key-up doesn't work with my terminal?

Community
  • 1
  • 1
sid_com
  • 24,137
  • 26
  • 96
  • 187
  • It sends the key-up code alright, but you expect it for some reason to position the stream on the screen. Only there's nothing at the other end to interpret the codes. Use Curses instead. – daxim Nov 15 '11 at 18:35
  • 1
    @daxim, all Curses does is send the same control codes that Term::Cap tells you about. It's just a higher-level library. The real problem is that he's sending the wrong code. – cjm Nov 15 '11 at 21:56

1 Answers1

4

You've misunderstood the ku capability. That's the character sequence generated when the user presses the up arrow key on the terminal. To actually move the cursor up on the screen, you print the up capability. (Also, it's best to avoid the indirect object syntax, although that had nothing to do with your problem.)

Here's a corrected version:

#!/usr/bin/env perl
use warnings;
use 5.014;
use Term::Cap;
use POSIX;

my $termios = POSIX::Termios->new;
$termios->getattr;
my $ospeed = $termios->getospeed;

my $terminal = Term::Cap->Tgetent({ TERM => undef, OSPEED => $ospeed });
$terminal->Trequire("up");  # move cursor up
my $UP = $terminal->Tputs("up");

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

You may find the Termcap manual helpful. It explains what all the capabilities mean.

cjm
  • 61,471
  • 9
  • 126
  • 175