Question

#!/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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top