4

I've the following script, which is almost the same of the sample in synopsis paragraph in documentation.

use strict;
use warnings;
use Term::ReadLine;

my $term = Term::ReadLine->new('My shell');
print $term, "\n";
my $prompt = "-> ";

while ( defined ($_ = $term->readline($prompt)) ) {
   print $_, "\n";
   $term->addhistory($_);
}

It executes with no error, but unfortunately, even if I click the Up Arrow, I only get ^[[A and no history. What am I missing?

The print $term statement prints Term::ReadLine::Stub=ARRAY(0x223d2b8).

Since we are here, I noticed it prints the prompt underlined... but I can't find in the docs anything which could prevent it. Is there any way to avoid it?

Randall
  • 2,859
  • 1
  • 21
  • 24
Zagorax
  • 11,440
  • 8
  • 44
  • 56
  • It works for me (Perl 5.10 on Debian). Have you checked your terminal keybindings? – Rob Feb 05 '13 at 15:45
  • 1
    @Rob, which Term::ReadLine implementation are you using? It worked for me after I installed Term::ReadLine::Gnu, but I think it should have worked even with Stub... – Zagorax Feb 05 '13 at 22:49

1 Answers1

7

To answer the main question, you probably don't have a good Term::ReadLine library installed. you will want either 'perl-Term-ReadLine-Perl' or 'perl-Term-ReadLine-Gnu'. These are the fedora package names, but i'm sure that the ubuntu/debian names would be similar. I believe you could also get them from CPAN, but I haven't tested that. If you haven't installed the package, perl loads a dummy module that has almost no features. for this reason history was not part of it.

The underline is part of what readline calls ornaments. if you want to turn them off completely, add $term->ornaments(0); somewhere apropriate.

my rewrite of your script is as follows

#!/usr/bin/perl
use strict;
use warnings;

use Term::ReadLine; # make sure you have the gnu or perl implementation of readline isntalled
# eg: Term::ReadLine::Gnu or Term::ReadLine::Perl
my $term = Term::ReadLine->new('My shell');
my $prompt = "-> ";
$term->ornaments(0);  # disable ornaments.

while ( defined ($_ = $term->readline($prompt)) ) {
   print $_, "\n";
   $term->addhistory($_);
}
Mobius
  • 2,871
  • 1
  • 19
  • 29