in reply to Translating Bash to Perl

You could try something like this:

#!/usr/bin/perl use Term::ReadKey; my %seqs = ( "\e[A" => "Up", "\e[B" => "Down", "\e[C" => "Right", "\e[D" => "Left", "\e[5~" => "PgUp", "\e[6~" => "PgDn", # ... ); ReadMode 4; # raw mode my $buf = ''; while (my $key = ReadKey()) { last if $key eq 'q'; # hit 'q' to quit loop $buf .= $key; $buf = substr($buf, -4); # keep last 4 bytes my $escseq = substr($buf,rindex($buf,"\e")); if (exists $seqs{$escseq}) { print "$seqs{$escseq}\n"; } } ReadMode 0; # reset tty

(There are a few subtle issues with this approach, but it should give you an idea how to approach the problem...)

update: you could also use a dispatch table to execute subs when a certain key is pressed:

#!/usr/bin/perl use Term::ReadKey; my $n; my %seqs = ( "\e[A" => sub { $n++; }, # Up "\e[B" => sub { $n--; }, # Down "\e[C" => sub { $n += 10; }, # Right "\e[D" => sub { $n -= 10; }, # Left "\e[5~" => sub { $n += 100; }, # PgUp "\e[6~" => sub { $n -= 100; }, # PgDn ); ReadMode 4; # raw mode my $buf = ''; while (my $key = ReadKey()) { last if $key eq 'q'; # hit 'q' to quit loop $buf .= $key; $buf = substr($buf, -4); # keep last 4 bytes my $escseq = substr($buf,rindex($buf,"\e")); if (exists $seqs{$escseq}) { $seqs{$escseq}->(); # call associated subroutine print "$n\n"; } } ReadMode 0; # reset tty