Peta::ReadLine#
Interactive line editing, command history, and configurable key bindings, with the Term::ReadLine::Gnu interface.
Install a readline instance, hand it a prompt, and you get the usual bash-style editing: cursor motion, word-wise kill and yank, incremental history search, filename completion, and an inputrc-controlled key map. The module has Term::ReadLine::Gnu’s whole surface - nearly one hundred entry points expose readline’s C-level interface directly, organised here by the same section names the GNU Readline manual uses - and Term::ReadLine::Gnu is this module under its old name.
A typical script calls new to obtain the readline object, then readline in a loop with add_history after each accepted line. Scripts that customise behaviour reach for rl_parse_and_bind and rl_variable_bind for inputrc-style configuration, or install completion by binding rl_completion_entry_function via the Attribs tied hash.
use Term::ReadLine;
my $term = Term::ReadLine->new('demo', \*STDIN, \*STDOUT);
print $term->ReadLine, "\n"; # Term::ReadLine::Gnu
$term->add_history($_) for 'ls -l', 'make test', 'git status';
my @h = $term->GetHistory;
print scalar(@h), " in history, last '$h[-1]'\n"; # 3 in history, last 'git status'
my $attribs = $term->Attribs; # readline's variables, as a hash
$attribs->{completion_entry_function} = $attribs->{list_completion_function};
$attribs->{completion_word} = [qw(alpha beta gamma)]; # Tab completes these
my $line = $term->readline('> '); # one line, with editing and history
Programs normally use Term::ReadLine, which picks Term::ReadLine::Gnu when it is available, as it is here, and gets this module; a program that names Term::ReadLine::Gnu gets it under that name, and ReadLine answers with the name it was asked by. The library behind the interface is pperl’s own line editor, built in: no readline or curses library is loaded, and the terminal is described from the terminfo database. It behaves as GNU Readline 8.3 does - the same commands, key bindings, init file, history and completion, and the same screen after every key - and says what it is: $attribs->{library_version} is 8.3 (Peta::ReadLine 0.2.0), the interface level first, where programs look for a number, and $attribs->{gnu_readline_p} is 0.
A few methods are Perl in ReadLine/XS.pm over private XSUBs, and so are not among the entries below: $term->rl_message($fmt, @args) shows sprintf($fmt, @args) in place of the prompt until rl_clear_message; $term->rl_discard_keymap($map) frees the bindings of a keymap (by name or handle) and returns it, still usable; $term->get_history_event($string, $cindex [, $qchar]) expands the history event at $cindex and returns ($text, $cindex), the index where scanning stopped, so a string of several !-events can be walked one at a time.
This is a native implementation of the XS half of Term::ReadLine::Gnu 1.47; ReadLine.pm and ReadLine/XS.pm are its Gnu.pm and Gnu/XS.pm under this module’s name.
Differences from upstream#
The module is Peta::ReadLine; Term::ReadLine::Gnu is an alias of it, so a program written for Term::ReadLine::Gnu runs unchanged and sees that name, while Peta::ReadLine->new reports Peta::ReadLine.