PinePerl has asked for the wisdom of the Perl Monks concerning the following question:

The following code is modified from the Perl Cookbook and is just a demonstration of what I intend, not the actual code being implemented...

use Term::ReadLine; $term = Term::ReadLine->new("APP DESCRIPTION"); $OUT = $term->OUT || *STDOUT; $term->addhistory($fake_line); $line = $term->readline(PROMPT); print $line

I am trying to make improvements to a program created in early 2000's. The built-in terminal interface works similar to the recipe above, yet the interface prevents the user from revising input once the terminal text wraps.

PROMPTThis is a test. This is being written from a terminal. As I writ +e, I get closer to the edge. The prompt continues on the line below w +ithout me hitting the return key. When I press up with an arrow key, +I get these characters ^[[A^[[A

Could someone please advise me on how to do either of the two: 1) allow the word wrap and modify all revised text similar to emacs, or 2) get rid of the word wrap and in turn allow revision of the paragraph before submitting it as input.

If you are aware of a different tool that provides these features, please let me know as I'm unaware of all available Perl modules and have been learning so much this past month.

Replies are listed 'Best First'.
Re: Revising Input Method
by ww (Archbishop) on Jul 27, 2016 at 18:53 UTC

    Your SOPW is so close to incomprehensible (to me, OMMV, or incoherent) that I'm merely making a WAG as to your intent. But because the code below probably won't help much, please explain why you seem to be using Term::ReadLine to WRITE to a console. 1.

    #!/usr/bin/perl use strict; use warnings; use 5.018; use Term::Readline; my $term_out = "This is a test. This is being written from a terminal. + As I write, I get closer to the edge. \nThe prompt continues on the +line below without me hitting the return key.\n When I press up with +an arrow key, nothing untoward happens on W7 w/perl v 5.018."; my $term = Term::ReadLine->new("APP DESCRIPTION"); my $OUT = $term->OUT|| *STDOUT; $term->addhistory(my $fake_line); my $line = $term->readline("Strike RETURN to continue: "); say $line . $term_out;

    Note the use of strict and warnings.

    1. Perhaps this fragment from Term::Readline's doc has sent you in the direction shown in your parent node.

    "new" returns the handle for subsequent calls to following functions. Argument is the name of the application. Optionally can be followed by two arguments for "IN" and "OUT" filehandles. These arguments should be globs. "readline" gets an input line, *possibly* with actual "readline" support. Trailing newline is removed. Returns "undef" on "EOF". "addhistory" adds the line to the history of input, from where it can +be used if the actual "readline" is present.

    If so, please offer a (minimal) version of the full code which displays the same anomalies (control chars) you mentioned and a sample of the source text

    <
    If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.

      The script is unoriginal and from a popular Perl book. It's only a model of my concept. I apologize for the misunderstanding.

Re: Revising Input Method
by Anonymous Monk on Jul 27, 2016 at 20:38 UTC

    From perldoc Term::ReadLine

    DESCRIPTION This package is just a front end to some other packages. It's a st +ub to set up a common interface to the various ReadLine implementations +found on CPAN (under the "Term::ReadLine::*" namespace).

    It looks like you are missing one of the "various ReadLine implementations", such as Term::ReadLine::Perl

Re: Revising Input Method
by hakonhagland (Scribe) on Jul 28, 2016 at 20:47 UTC

    According to the documentation the package is just a front end to some other packages. It's a stub to set up a common interface to the various ReadLine implementations found on CPAN (under the Term::ReadLine::* namespace). If you haven't installed any additional packages like Term::Readline::Gnu or Term::ReadLine::EditLine, it will default to an internal default package called Term::ReadLine::Stub, this default package is very simple, and does not understand things like arrow keys. You can use $term->ReadLine to determine which package it is actually using, see below example.

    I tried to install Term::ReadLine::Gnu on my Ubuntu laptop:

    $ sudo apt-get install libncurses5-dev libreadline6-dev $ cpanm Term::ReadLine::Gnu
    Then I tried
    use feature qw(say); use strict; use warnings; use Term::ReadLine; my $term = Term::ReadLine->new('Test', \*STDIN, \*STDOUT); say "I am using the following ReadLine package: " . $term->ReadLine; $term->ornaments( 0 ); $term->addhistory("sample text added to history"); while (1) { my $line = $term->readline( 'Enter input: ' ); say "You entered: '$line'"; }

    Now the up and down arrow keys are recoginzed, and used to recall the history lines. There are also some useful Emacs keybindings available, see Command Line Editing in the Gnu Bash manual for more information.