1

I am writing an interface to my database and would like to be able to edit entries. I thought I'd use Readline, but can't get it work. I'd say my code should work, based on Term::ReadLine::Gnu and this answer on SE.

perl -w -MTerm::ReadLine -E'
$term = new Term::ReadLine "ProgramName";
$prompt = "prompt> ";
sub su {insert_text("----default----");};
$attributes = $term->Attribs;
$attributes{startup_hook} = \&su;
while (defined ($_ = $term->readline($prompt)))
{ eval; }
'

I'm getting the clean prompt:

prompt> 

while I expect this:

prompt> ----default----

I'm still not very fluent with Perl, and this is getting complex. Is there a bug on my side? Is something missing? Or is it a hole in the module implementation?

1

I've found out I can use this syntax:

$term->readline($prompt, $default)

This is both much shorter and more simple than the code in the question.

I found it here: Term::ReadLine::Perl5, though I'm not yet sure how these three are related:

  • Term::ReadLine::Perl5
  • Term::ReadLine::Gnu
  • Term::ReadLine

(The question stays open though.)

  • 1
    Term::ReadLine is a frontend module, ::Gnu, ::Perl, and ::Perl5 are implementations of the functionality, so you can normally just use Term::ReadLine; and not have to worry which implementation it is using behind the scenes. – haukex Mar 12 '18 at 10:21
  • @haukex Apparently this syntax works with ::Perl5, but not with what I found (after installing Term::ReadLine) on my new OS before explicitly using Term::ReadLine::Perl5. (What was it?) – Tomasz Apr 13 '18 at 23:13
  • Sorry for the late reply. As far as I can tell from a quick look at the source, Term::ReadLine only knows about Term::ReadLine::Gnu, ::EditLine, and ::Perl, but not ::Perl5, which is probably why you need to load that one explicitly. – haukex Apr 21 '18 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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