2

I'm using Term::ReadLine and have an odd issue where I need to hit the up arrow twice to retrieve items from addhistory. Here's the script I'm using.

use Term::ReadLine;

my $term = Term::ReadLine->new('Term1');
print $term->ReadLine."\n";

while (defined(my $cmd = $term->readline())) {
    $term->addhistory($cmd) if $cmd =~ /\S/;
}

To test, I'll input a few "commands" then try to retrieve those commands with the up arrow. I hit the up arrow once and the last item comes up immediately, but I'll need to hit the up arrow twice before each following item is retrieved.

I encountered this issue earlier in a little project I'm working on, and came across someone with the same issue. The resolution to this issue is a modification to the addhistory line as follows:

$term->addhistory($cmd) if $cmd !~ /\S||\n/;

It worked so I moved on until Tanktalus brought this up in another question I posted. Perl Term::ReadLine::Gnu Signal Handling Difficulties

He asks "what is /\S||\n/ supposed to do?". Ok well, let me go find that page that suggested this as a fix... Yeah...can't find it. So while I can make it work with the modification I posted I'm a bit stuck since A. I don't know why it fixes it, and B. Why do I seem to be the only one with this issue?

Any help or advice would be appreciated! :)

Tested the above script with the following versions:

Perl - 5.12.4 and 5.16.1
Term::ReadLine::Gnu - 1.20
Term::ReadLine::Perl - 1.0303 (Just tried it to make sure it wasn't Gnu)
Term::ReadLine - 1.10
1
  • what kind of terminal are you using, and what is the environment variable TERM set to?
    – ysth
    Nov 11, 2012 at 18:09

1 Answer 1

5

/\S||\n/ will always match, so you will never be calling addhistory. If $term->Features->{autohistory} is true, you didn't need to anyway.

By "hit the up arrow twice", do you mean that you move up through the history once for every two up arrows? If so, your unnecessary call to addhistory is the culprit. And just removing the call entirely is the appropriate fix.

1
  • Thanks so much for pointing me in the right direction ysth! That was the case and eliminating the (second) call to addhistory solved my problem. :)
    – Sgt B
    Nov 12, 2012 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.