How do I use Term :: ReadLine to fetch command history?

I have the following script that almost matches the pattern in the synopsis paragraph in the 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($_);
}

      

Runs without error, but unfortunately even if I press the up arrow, I only get ^[[A

no history. What am I missing?

The operator print $term

is typing Term::ReadLine::Stub=ARRAY(0x223d2b8)

.

Since we are here, I noticed that it displays a hint below the title ... but I can't find anything in the docs that might prevent it. Is there a way to avoid this?

+3


source to share


1 answer


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

Some of what readline causes embellishments are underlined. if you want to disable them completely, add $term->ornaments(0);

somewhere suitable.



my correspondence with 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($_);
}

      

+5


source







All Articles