Term::CLI::ReadLine - Term::ReadLine compatibility layer for Term::CLI
version 0.058002
use Term::CLI::ReadLine; sub initialise { my $term = Term::CLI::ReadLine->new( ... ); ... # Use Term::ReadLine methods on $term. } # The original $term reference is now out of scope, but # we can get a reference to it again: sub somewhere_else { my $term = Term::CLI::ReadLine->term; ... # Use Term::ReadLine methods on $term. }
This class provides a compatibility layer between Term::ReadLine(3p) and Term::CLI(3p). If Term::ReadLine::Gnu(3p) is not loaded as the Term::ReadLine implementation, this class will compensate for the lack of certain functions by replacing or wrapping methods that are needed by the rest of the Term::CLI(3p) classes.
Term::ReadLine
The ultimate purpose is to behave as consistently as possible regardless of the Term::ReadLine interface that has been loaded.
This class inherits from Term::ReadLine and behaves as a singleton with a class accessor to access that single instance, because even though Term::ReadLine(3p) has an object-oriented interface, the Term::ReadLine::Gnu(3p) and Term::ReadLine::Perl(3p) modules really only keep a single instance around (if you create multiple Term::ReadLine objects, all parameters and history are shared).
Create a new Term::CLI::ReadLine(3p) object and return a reference to it.
Arguments are identical to Term::ReadLine(3p).
A reference to the newly created object is stored internally and can be retrieved later with the term class method. Note that repeated calls to new will reset this internal reference.
new
See Term::ReadLine(3p), Term::ReadLine::Gnu(3p) and/or Term::ReadLine::Perl for the inherited methods.
In a Term::ReadLine::Gnu environment this returns the rl_completion_quote_character. This value is set during completion if the text to be completed has an open quote. Consider the case:
rl_completion_quote_character
foo 'bar <TAB>
When the completion function is called, the rl_completion_quote_character will contain a single quote, '.
'
For non-GNU ReadLine backends, this function returns an empty string.
Print the character that generates a particular signal when entered from the keyboard (e.g. ^C for keyboard interrupt).
^C
This method also accepts a signal name instead of a signal number. It only works for INT (2), QUIT (3), and TSTP (20) signals as these are the only ones that can be entered from a keyboard.
INT
QUIT
TSTP
If Term::ReadLine::Gnu is loaded, this method wraps around the method of the same name in Term::ReadLine::Gnu (translating a signal name to a number first). For other Term::ReadLine implementations, it emulates the Term::ReadLine::Gnu behaviour.
Term::ReadLine::Gnu
Wrap around the original Term::ReadLine's readline with custom signal handling, see the CAVEATS section in Term::CLI.
This also calls AddHistory if autohistory is not set in Features.
AddHistory
autohistory
Features
Return the width of the terminal in characters, as given by Term::ReadLine.
Return the height of the terminal in characters, as given by Term::ReadLine.
Ensure that SIGNAME signals cannot be entered from the keyboard. SIGNAME should be the name of a signal that can be entered from the keyboard, i.e. one of: INT, QUIT, TSTP.
By default, the QUIT keyboard signal is already disabled.
Notes:
This will only disable the keys for the given signals during a readline operation. Outside of that, they will still generate signals.
readline
This only disables the keyboard sequences, not the actual signals themselves (i.e. you can still kill -3 PID from another terminal.
kill -3 PID
Disabling the INT key will cause Ctrl-C to no longer discard the input line under Term::ReadLine::Gnu; it will discard it under Term::ReadLine::Perl! It is therefore recommended to just set $SIG{INT} to IGNORE instead.
$SIG{INT}
IGNORE
Disabling the TSTP key works under Term::ReadLine::Gnu, but not under Term::ReadLine::Perl. The latter maps the key in raw mode and explicitly sends a TSTP signal to itself.
See also SIGNAL HANDLING below.
(Re-)Enable keyboard generation for SIGNAME signals. See ignore_keyboard_signals above for valid SIGNAME values.
Reset all keyboard signal generation to the defaults.
Depending on the underlying Term::ReadLine implementation, these will either call the parent class's method, or implement a proper emulation.
In the case of Term::ReadLine::Perl, this means that ReadHistory and WriteHistory implement their own file I/O read/write (because Term::ReadLine::Perl doesn't provide them); furthermore, StifleHistory uses knowledge of Term::ReadLine::Perl's internals to manipulate the history.
Term::ReadLine::Perl
ReadHistory
WriteHistory
StifleHistory
In cases where history is not supported at all (e.g. Term::ReadLine::Stub, the history list is kept in this object and manipulated.
Term::ReadLine::Stub
If Term::ReadLine is not using the GNU ReadLine library, this object provides stubs for a few GNU ReadLine methods:
If Term::ReadLine::Perl is loaded, this will use knowledge of its internals to force an redraw of the input line.
Prints a newline to the terminal's output.
If Term::ReadLine::Perl is loaded, this will use knowledge of its internals to replace the current input line with str.
If Term::ReadLine::Perl is loaded, this will use knowledge of its internals to either restore (deprep) terminal settings to what they were before calling readline, or to set them to what readline uses. You will rarely (if ever) need these, since the ReadLine libraries usually take care if this themselves.
One exception to this is in signal handlers: Term::CLI::ReadLine calls these methods during its signal handling.
Term::CLI::ReadLine
Use Term::ReadKey::GetTerminalSize to get the appropriate dimensions and return them as (height, width).
Term::ReadKey::GetTerminalSize
Return the latest Term::CLI::ReadLine object created.
The class sets its own signal handlers in the readline function where necessary.
The following signals may be caught: ALRM, CONT, HUP, INT, QUIT, TERM.
ALRM
CONT
HUP
TERM
The signal handlers will:
Restore the terminal to a "sane" state, i.e. the state it was in before readline was called (the CONT signal being an exception to this rule).
If any signal handler was set prior to the call to readline, it will be called and if control returns Term::CLI::ReadLine's signal handler, the terminal will be set back to the state that readline expects it to be in.
If the signal handler was previously set to DEFAULT, it is restored as DEFAULT and the signal is re-thrown, so the default actions (abnormal exit and possible core dump) can take place.
DEFAULT
Just how and when these "wrapper" signal handlers are installed depends on the selected Term::ReadLine implementation. The Gnu backend doesn't require separate handlers for signals that are set to IGNORE or DEFAULT. The Perl backend does require some wrapping.
The INT signal is always wrapped to ensure that the current input line is discarded and a newline is emitted.
One subtle difference between the Term::ReadLine::Gnu and Term::ReadLine::Perl is in keyboard-generated signal handling (interrupt, quit, suspend).
Term::ReadLine::Perl disables keyboard-generated signals. When it reads a Ctrl-C, it will send itself an INT signal, when it sees a Ctrl-Z, it will send a TSTP signal; the "quit" key Ctrl-\ is simply ignored.
Term::ReadLine::Gnu leaves keyboard-generated signals enabled and sets signal handlers to catch them.
This subtle difference means that:
It is impossible to have Ctrl-\ generate a QUIT signal under Term::ReadLine::Perl.
It is impossible to disable Ctrl-Z through "ignore_keyboard_signals" under Term::ReadLine::Perl.
Disabling Ctrl-C through "ignore_keyboard_signals" will completely disable Ctrl-C under Term::ReadLine::Gnu (will not discard the input line), but not Term::ReadLine::Perl.
For this reason, the module by default ignores the QUIT key sequence.
To behave as consistently as possible across the Term::ReadLine backends, the following is best if you don't want keyboard signals to kill or stop the program:
Set $SIG{INT} to IGNORE.
Set $SIG{TSTP} to IGNORE.
$SIG{TSTP}
Ignore keyboard signal QUIT (already default).
Term::CLI(3p), Term::ReadLine(3p), Term::ReadLine::Gnu(3p), Term::ReadLine::Perl(3p).
Steven Bakker <sbakker@cpan.org>, 2018-2021.
Copyright (c) 2018 Steven Bakker
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See "perldoc perlartistic."
This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
To install Term::CLI, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Term::CLI
CPAN shell
perl -MCPAN -e shell install Term::CLI
For more information on module installation, please visit the detailed CPAN module installation guide.