Gnu(3)
okay bestie, sit down, hydrate, because this is Gnu(3) from the User Contributed Perl Documentation section and it is a lot in the best way. π this is part one of five, so buckle up, we're just getting started.
π NAME π
Term::ReadLine::Gnu β a Perl extension for the GNU Readline/History Library. that's the whole vibe. no notes. π
β¨ SYNOPSIS β¨
use Term::ReadLine; # Do not "use Term::ReadLine::Gnu;"
$term = new Term::ReadLine 'ProgramName';
while ( defined ($_ = $term->readline('prompt>')) ) {
...
}
πΈ DESCRIPTION πΈ
π¦ Overview π¦
So basically this is an implementation of Term::ReadLine (http://search.cpan.org/dist/Term-ReadLine/) that runs on the GNU Readline/History Library (https://tiswww.cwru.edu/php/chet/readline/rltop.html). two iconic libraries, one little Perl package. we love to see it. π«Ά
For basic functions an object oriented interface is provided, and those live in the section Standard Methods and the section "Term::ReadLine::Gnu" Functions.
This package also has an interface with, like, almost all the functions and variables documented in the GNU Readline/History Library Manual. They're covered briefly in the section "Term::ReadLine::Gnu" Functions and the section "Term::ReadLine::Gnu" Variables. For the further details of the GNU Readline/History Library, go read the GNU Readline Library Manual (https://tiswww.cwru.edu/php/chet/readline/readline.html) and the GNU History Library Manual (https://tiswww.cwru.edu/php/chet/readline/history.html). iykyk. π
There are also some "Term::ReadLine::Gnu" original features, and those are described in the section "Term::ReadLine::Gnu" Specific Features.
The sample programs under the eg/ directory and the test programs under the t/ directory in the "Term::ReadLine::Gnu" distribution (http://search.cpan.org/dist/Term-ReadLine-Gnu/) include many examples of this module. See eg/README.md for the details on the eg/ directory. ποΈ
π Standard Methods π
These are standard methods defined by Term::ReadLine (http://search.cpan.org/dist/Term-ReadLine/).
ReadLineβ returns the actual package that executes the commands. If this package is being used,Term::ReadLine::Gnuis returned.new(NAME,[IN,OUT])β returns the handle for the subsequent calls to the following functions. The argument is the name of the application. Optionally it can be followed by two arguments for theINandOUTfile handles. These arguments should be globs.readline(PROMPT[,PREPUT])β gets an input line, with actualGNU Readlinesupport. The trailing newline is removed. ReturnsundefonEOF.PREPUTis an optional argument meaning the initial value of the input.
The optional argument PREPUT is granted only if the value preput is in Features.
PROMPT may include some escape sequences. Use RL_PROMPT_START_IGNORE to begin a sequence of non-printing characters, and RL_PROMPT_END_IGNORE to end the sequence. π
AddHistory(LINE1, LINE2, ...)β adds the lines to the history of input, from where it can be used if the actualreadlineis present.IN,OUTβ return the file handles for input and output, orundefifreadlineinput and output cannot be used for Perl.MinLine([MAX])β if the argumentMAXis specified, it's an advice on the minimal size of a line to be included into the history.undefmeans do not include anything into the history. Returns the old value.findConsoleβ returns an array with two strings that give the most appropriate names for files for input and output using the conventions<$in,>$out.Attribsβ returns a reference to a hash which describes the internal configuration (variables) of the package. The names of the keys in this hash conform to standard conventions with the leadingrl_stripped.
See the section "Term::ReadLine::Gnu" Variables for the supported variables.
Featuresβ returns a reference to a hash with keys being features present in the current implementation. Several optional features are used in the minimal interface:appnameshould be present if the first argument tonewis recognized, andminlineshould be present if theMinLinemethod is not dummy.autohistoryshould be present if the lines are put into the history automatically (maybe subject toMinLine), andaddHistoryif theAddHistorymethod is not dummy.preputmeans the second argument to thereadlinemethod is processed.getHistoryandsetHistorydenote that the corresponding methods are present.tkRunningdenotes that a Tk application may run while ReadLine is getting input.tkRunningβ makes the Tk event loop run when waiting for the user input (i.e., during thereadlinemethod).event_loopβ see the description ofevent_looponTerm::ReadLine(http://search.cpan.org/dist/Term-ReadLine/).ornamentsβ makes the command line stand out by using termcap data. The argument toornamentsshould be0,1, or a string of the formaa,bb,cc,dd. The four components of this string should be names of terminal capacities β the first two will be issued to make the prompt stand out, the last two to make the input line stand out. β¨newTTYβ takes two arguments which are an input filehandle and an output filehandle. Switches to use these filehandles.enableUTF8β enables UTF-8 support.
If STDIN is in UTF-8 by the -C command-line switch or the PERL_UNICODE environment variable, or the IN file handle has the utf8 IO layer, then UTF-8 support is also enabled. In other cases you need this enableUTF8 method.
This is an original method of Term::ReadLine:Gnu. π¦
π "Term::ReadLine::Gnu" Functions π
All of these supported GNU Readline/History Library functions are callable via the method interface, and they have names which conform to standard conventions with the leading rl_ stripped. For example, the rl_foo() function is called as $term->foo().
The titles of the following sections are the same as the titles of the corresponding sections in the Programming with GNU Readline section of the GNU Readline Library Manual (https://tiswww.cwru.edu/php/chet/readline/readline.html). Refer to those for the further details, bestie.
Although it's preferred to use the method interface, most methods have lower level functions in the Term::ReadLine::Gnu::XS package. To use them, a full qualified name is required.
π Basic Behavior π
The function readline() prints a prompt and then reads and returns a single line of text from the user.
$_ = $term->readline('Enter a line: ');
You can change key-bindings using the bind_key(KEY, FUNCTION [,MAP]) function. The first argument, KEY, is the character that you want bind. The second argument, FUNCTION, is the function to call when KEY is pressed. The FUNCTION can be a reference to a Perl function (see Custom Functions) or a named function named by the add_defun() function or commands described in the Bindable Readline Commands section of the GNU Readline Library Manual (https://tiswww.cwru.edu/php/chet/readline/readline.html).
$term->bind_key(ord "\ci, 'tab-insert');
The above example binds Control-I to the 'tab-insert' command. slay. π
π§ Custom Functions π§
You can write new functions using Perl. The calling sequence for a command foo looks like
sub foo ($count, $key) { ... }
where $count is the numeric argument (or 1 if defaulted) and $key is the key that invoked this function.
Here is an example;
sub reverse_line { # reverse a whole line
my($count, $key) = @_; # ignored in this sample function
$t->modifying(0, $a->{end}); # save undo information
$a->{line_buffer} = reverse $a->{line_buffer};
}
See the Writing a New Function section in the GNU Readline Library Manual (https://tiswww.cwru.edu/php/chet/readline/readline.html) for the further details.
π Readline Convenience Functions π
π Naming a Function π
add_defun(NAME, FUNCTION [,KEY=-1])β Add name to a Perl functionFUNCTION. If the optional argumentKEYis specified, bind it to theFUNCTION. Returns a reference toFunctionPtr.
Example:
# name `reverse-line' to a function reverse_line(),
# and bind it to "\C-t"
$term->add_defun('reverse-line', \&reverse_line, ord "\ct");
π Selecting a Keymap π
make_bare_keymap
Keymap rl_make_bare_keymap()
copy_keymap(MAP)
Keymap rl_copy_keymap(Keymap|str map)
make_keymap
Keymap rl_make_keymap()
discard_keymap(MAP)
Keymap rl_discard_keymap(Keymap|str map)
free_keymap(MAP)
void rl_free_keymap(Keymap|str map)
empty_keymap(MAP)
int rl_empty_keymap(Keymap|str map) # GRL 8.0
get_keymap
Keymap rl_get_keymap()
set_keymap(MAP)
Keymap rl_set_keymap(Keymap|str map)
get_keymap_by_name(NAME)
Keymap rl_get_keymap_by_name(str name)
get_keymap_name(MAP)
str rl_get_keymap_name(Keymap map)
set_keymap_name(NAME, MAP)
int rl_set_keymap_name(str name, Keymap|str map) # GRL 8.0
π Binding Keys π
bind_key(KEY, FUNCTION [,MAP])
int rl_bind_key(int key, FunctionPtr|str function,
Keymap|str map = rl_get_keymap())
Bind KEY to the FUNCTION. FUNCTION is the name added by the add_defun method. If the optional argument MAP is specified, binds in MAP. Returns non-zero in case of error.
bind_key_if_unbound(KEY, FUNCTION [,MAP])
int rl_bind_key_if_unbound(int key, FunctionPtr|str function,
Keymap|str map = rl_get_keymap()) # GRL 5.0
unbind_key(KEY [,MAP])
int rl_unbind_key(int key, Keymap|str map = rl_get_keymap())
Bind KEY to the null function. Returns non-zero in case of error.
unbind_function(FUNCTION [,MAP])
int rl_unbind_function(FunctionPtr|str function,
Keymap|str map = rl_get_keymap())
unbind_command(COMMAND [,MAP])
int rl_unbind_command(str command,
Keymap|str map = rl_get_keymap())
bind_keyseq(KEYSEQ, FUNCTION [,MAP])
int rl_bind_keyseq(str keyseq, FunctionPtr|str function,
Keymap|str map = rl_get_keymap()) # GRL 5.0
set_key(KEYSEQ, FUNCTION [,MAP])
int rl_set_key(str keyseq, FunctionPtr|str function,
Keymap|str map = rl_get_keymap()) # GRL 4.2
bind_keyseq_if_unbound(KEYSEQ, FUNCTION [,MAP])
int rl_bind_keyseq_if_unbound(str keyseq, FunctionPtr|str function,
Keymap|str map = rl_get_keymap()) # GRL 5.0
"generic_bind(TYPE, KEYSEQ, DATA, [,MAP])"
int rl_generic_bind(int type, str keyseq,
FunctionPtr|Keymap|str data,
Keymap|str map = rl_get_keymap())
okay so this is the generic bind signature, bestie. no cap. π
parse_and_bind(LINE)
void rl_parse_and_bind(str line)
Parse LINE as if it had been read from the ~/.inputrc file and perform any key bindings and variable assignments found. For further detail see GNU Readline Library Manual <https://tiswww.cwru.edu/php/chet/readline/readline.html>. πβ¨
read_init_file([FILENAME])
int rl_read_init_file(str filename = '~/.inputrc')
the default filename is literally '~/.inputrc', periodt. π
β¨ Associating Function Names and Bindings β¨
named_function(NAME)
FunctionPtr rl_named_function(str name)
here's the C signature, no cap. π
get_function_name(FUNCTION)
str rl_get_function_name(FunctionPtr function) # TRG original
note the # TRG original tag, bestie. π·οΈ
"function_of_keyseq(KEYSEQ [,MAP])"
(FunctionPtr|Keymap|str data, int type)
rl_function_of_keyseq(str keyseq,
Keymap|str map = rl_get_keymap())
wait, this returns a tuple? iconic. β¨
"trim_arg_from_keyseq(KEYSEQ [,MAP])"
int rl_trim_arg_from_keyseq(str keyseq,
Keymap|str map = rl_get_keymap()) # GRL 8.2
GRL 8.2 vibe. π
"invoking_keyseqs(FUNCTION [,MAP])"
(@str) rl_invoking_keyseqs(FunctionPtr|str function,
Keymap|str map = rl_get_keymap())
returns a list of strings, slay. π
"print_keybinding(NAME [MAP [,READABLE]])"
void rl_print_keybinding(str name,
Keymap|str map = rl_get_keymap(),
int readable = 0) # GRL 8.3
# GRL 8.3 bestie. β¨
function_dumper([READABLE])
void rl_function_dumper(int readable = 0)
default is 0. π
"list_funmap_names"
void rl_list_funmap_names()
no args, just vibes. π¦
"funmap_names"
(@str) rl_funmap_names()
returns a list of strings, periodt. π
"add_funmap_entry(NAME, FUNCTION)"
int rl_add_funmap_entry(char *name, FunctionPtr|str function)
okay so char *name and FunctionPtr|str function, no cap. π
β¨ Allowing Undoing β¨
"begin_undo_group"
int rl_begin_undo_group()
undo group starts here, bestie. β©οΈ
"end_undo_group"
int rl_end_undo_group()
and ends here. β¨
"add_undo(WHAT, START, END, TEXT)"
int rl_add_undo(int what, int start, int end, str text)
four args, iconic. π
"free_undo_list"
void rl_free_undo_list()
free that list, periodt. ποΈ
"do_undo"
int rl_do_undo()
do the undo, no cap. β©οΈ
"modifying([START [,END]])"
int rl_modifying(int start = 0, int end = rl_end)
defaults are 0 and rl_end. π
β¨ Redisplay β¨
"redisplay"
void rl_redisplay()
redisplay, slay. π
"forced_update_display"
int rl_forced_update_display()
forced update, periodt. π₯
"on_new_line"
int rl_on_new_line()
new line energy. π
"on_new_line_with_prompt"
int rl_on_new_line_with_prompt() # GRL 4.1
# GRL 4.1 bestie. β¨
clear_visible_line()
int rl_clear_visible_line() # GRL 7.0
# GRL 7.0 and it clears the visible line. π
"reset_line_state"
int rl_reset_line_state()
reset the line state, no cap. π
"crlf"
int rl_crlf()
crlf, iconic. π
show_char(C)
int rl_show_char(int c)
show char, periodt. π¦
"message(FMT[, ...])"
int rl_message(str fmt, ...)
variadic, bestie. π
"clear_message"
int rl_clear_message()
clear that message. β¨
"save_prompt"
void rl_save_prompt()
save prompt, no cap. π
"restore_prompt"
void rl_restore_prompt()
restore prompt, periodt. π
expand_prompt(PROMPT)
int rl_expand_prompt(str prompt)
expand prompt, slay. π
set_prompt(PROMPT)
int rl_set_prompt(const str prompt) # GRL 4.2
# GRL 4.2 bestie. β¨
β¨ Modifying Text β¨
insert_text(TEXT)
int rl_insert_text(str text)
insert text, iconic. π
"delete_text([START [,END]])"
int rl_delete_text(int start = 0, int end = rl_end)
defaults 0 and rl_end. π
"copy_text([START [,END]])"
str rl_copy_text(int start = 0, int end = rl_end)
returns a string, periodt. π
"kill_text([START [,END]])"
int rl_kill_text(int start = 0, int end = rl_end)
kill text, slay. π₯
"replace_line(TEXT [,CLEAR_UNDO])"
int rl_replace_line(str text, int clear_undo = 0) # GRL 4.3
# GRL 4.3 bestie. β¨
push_macro_input(MACRO)
int rl_push_macro_input(str macro)
push macro input, no cap. π
β¨ Character Input β¨
"read_key"
int rl_read_key()
read key, periodt. β¨οΈ
getc(STREAM)
int rl_getc(FILE *STREAM)
getc from FILE *STREAM. π
stuff_char(C)
int rl_stuff_char(int c)
stuff char, iconic. π
execute_next(C)
int rl_execute_next(int c)
execute next, slay. β¨
clear_pending_input()
int rl_clear_pending_input() # GRL 4.2
# GRL 4.2 bestie. π
set_keyboard_input_timeout(uSEC)
int rl_set_keyboard_input_timeout(int usec) # GRL 4.2
# GRL 4.2 and it takes usec. β±οΈ
"set_timeout(SECS, USECS)"
int rl_set_timeout(int secs, int usecs) # GRL 8.2
# GRL 8.2 bestie. β¨
clear_timeout()
int rl_clear_timeout() # GRL 8.2
# GRL 8.2 clear that timeout. π°οΈ
timeout_remaining()
int rl_timeout_remaining() # scalar context, GRL 8.2
(int ret, int secs, int usecs) rl_timeout_remaining() # array context
scalar context and array context, iconic. π
β¨ Terminal Management β¨
prep_terminal(META_FLAG)
void rl_prep_terminal(int META_FLAG)
prep terminal, no cap. π₯οΈ
deprep_terminal()
void rl_deprep_terminal()
deprep terminal, periodt. π
tty_set_default_bindings([MAP])
void rl_tty_set_default_bindings([Keymap|str map = rl_get_keymap()]) # GRL 4.0
# GRL 4.0 bestie. β¨
tty_unset_default_bindings([MAP])
void rl_tty_unset_default_bindings([Keymap|str map = rl_get_keymap()]) # GRL 5.0
# GRL 5.0 no cap. π
tty_set_echoing(VALUE)
int rl_tty_set_echoing(int value) # GRL 7.0
# GRL 7.0 echoing vibes. π
reset_terminal([TERMINAL_NAME])
int rl_reset_terminal(str terminal_name = getenv($TERM))
default is getenv($TERM), periodt. π
β¨ Utility Functions β¨
SETSTATE(READLINE_STATE)
int RL_SETSTATE(int) # GRL 6.0
# GRL 6.0 bestie. β¨
UNSETSTATE(READLINE_STATE)
int RL_UNSETSTATE(int) # GRL 6.0
# GRL 6.0 no cap. π
ISSTATE(READLINE_STATE)
int RL_ISSTATE(int) # GRL 6.0
# GRL 6.0 iconic. π
save_state(READLINE_STATE)
READLINE_STATE rl_save_state() # GRL 6.0
# GRL 6.0 save state, periodt. πΎ
restore_state(READLINE_STATE)
int rl_restore_state(READLINE_STATE) # GRL 6.0
# GRL 6.0 restore state, slay. β¨
free(MEM)
Not implemented since not required for Perl.
int rl_free(void *mem) # GRL 6.0
Not implemented since not required for Perl. # GRL 6.0 bestie, it's literally not implemented. π«
extend_line_buffer(LEN)
Not implemented since not required for Perl.
int rl_extend_line_buffer(int len)
Not implemented since not required for Perl. no cap. π«
"initialize"
int rl_initialize()
initialize, iconic. π
"ding"
int rl_ding()
ding, periodt. π
alphabetic(C)
int rl_alphabetic(int C) # GRL 4.2
# GRL 4.2 bestie. β¨
"display_match_list(MATCHES [,LEN [,MAX]])"
void rl_display_match_list(\@matches, len = $#maches, max) # GRL 4.0
# GRL 4.0 and here's the tea: Since the first element of an array @matches is treated as a possible completion, it is not displayed. See the descriptions of completion_matches(). When MAX is omitted, the max length of an item in @matches is used. πβ¨
β¨ Miscellaneous Functions β¨
"macro_bind(KEYSEQ, MACRO [,MAP])"
int rl_macro_bind(const str keyseq, const str macro, Keymap map)
macro bind, no cap. π
macro_dumper(READABLE)
int rl_macro_dumper(int readline)
macro dumper, periodt. π
"variable_bind(VARIABLE, VALUE)"
int rl_variable_bind(const str variable, const str value)
variable bind, slay. β¨
variable_value(VARIABLE)
str rl_variable_value(const str variable) # GRL 5.1
# GRL 5.1 bestie. π
variable_dumper(READABLE)
int rl_variable_dumper(int readline)
variable dumper, iconic. π
set_paren_blink_timeout(uSEC)
int rl_set_paren_blink_timeout(usec) # GRL 4.2
# GRL 4.2 paren blink timeout, no cap. β±οΈ
get_termcap(cap)
str rl_get_termcap(cap)
get termcap, periodt. π
"reparse_colors"
void rl_reparse_colors() # GRL 8.3
# GRL 8.3 reparse colors, slay. π¨
"clear_history"
void rl_clear_history() # GRL 6.3
# GRL 6.3 clear history, bestie. ποΈ
"activate_mark"
void rl_activate_mark() # GRL 8.1
# GRL 8.1 activate mark. β¨
"deactivate_mark"
void rl_deactivate_mark() # GRL 8.1
# GRL 8.1 deactivate mark. π
"keep_mark_active"
void rl_keep_mark_active() # GRL 8.1
# GRL 8.1 keep mark active. π
"mark_active_p"
int rl_mark_active_p() # GRL 8.1
# GRL 8.1 mark active p. π
β¨ Alternate Interface β¨
"callback_handler_install(PROMPT, LHANDLER)"
void rl_callback_handler_install(str prompt, pfunc lhandler)
callback handler install, no cap. π
"callback_read_char"
void rl_callback_read_char()
callback read char, periodt. π
"callback_sigcleanup"
void rl_callback_sigcleanup() # GRL 7.0
# GRL 7.0 bestie. β¨
"callback_handler_remove"
void rl_callback_handler_remove()
callback handler remove, slay. π
β¨ Readline Signal Handling β¨
pending_signal()
int rl_pending_signal() # GRL 7.0
# GRL 7.0 pending signal, iconic. π¨
"cleanup_after_signal"
void rl_cleanup_after_signal() # GRL 4.0
# GRL 4.0 cleanup after signal. π§Ή
"free_line_state"
void rl_free_line_state() # GRL 4.0
# GRL 4.0 free line state. π
"reset_after_signal"
void rl_reset_after_signal() # GRL 4.0
# GRL 4.0 reset after signal. β¨
"check_signals"
void rl_check_signals() # GRL 8.0
# GRL 8.0 check signals. π
"echo_signal_char"
void rl_echo_signal_char(int sig) # GRL 6.0
# GRL 6.0 echo signal char. π
"resize_terminal"
void rl_resize_terminal() # GRL 4.0
# GRL 4.0 resize terminal. π₯οΈ
"set_screen_size(ROWS, COLS)"
void rl_set_screen_size(int ROWS, int COLS) # GRL 4.2
# GRL 4.2 set screen size. π
get_screen_size()
(int rows, int cols) rl_get_screen_size() # GRL 4.2
# GRL 4.2 get screen size, returns rows and cols. π
reset_screen_size()
void rl_reset_screen_size() # GRL 5.1
# GRL 5.1 reset screen size. π
"set_signals"
int rl_set_signals() # GRL 4.0
# GRL 4.0 set signals. π
"clear_signals"
int rl_clear_signals() # GRL 4.0
# GRL 4.0 clear signals. β¨
β¨ Completion Functions β¨
complete_internal([WHAT_TO_DO])
int rl_complete_internal(int what_to_do = TAB)
default is TAB, periodt. π
completion_mode(FUNCTION)
int rl_completion_mode(FunctionPtr|str function) # GRL 4.3
# GRL 4.3 bestie. β¨
"completion_matches(TEXT [,FUNC])"
(@str) rl_completion_matches(str text,
pfunc func = filename_completion_function)
default func is filename_completion_function, no cap. π
"filename_completion_function(TEXT, STATE)"
str rl_filename_completion_function(str text, int state)
filename completion function, slay. π
"username_completion_function(TEXT, STATE)"
str rl_username_completion_function(str text, int state)
username completion function, iconic. π€
"list_completion_function(TEXT, STATE)"βstr list_completion_function(str text, int state) # TRG original
πΎ History Functions πΎ
okay bestie, buckle up, this is the history-library half of the whole situation π
β¨ Initializing History and State Management β¨
"using_history"βvoid using_history()"history_get_history_state"βHISTORY_STATE history_get_hitory_state() # GRL 6.3"history_set_history_state"βvoid history_set_hitory_state(HISTORY_STATE) # GRL 6.3
not me pointing out that those two literally say hitory π that's just what's written, I'm not touching it, periodt π
π History List Management π
add_history(STRING)βvoid add_history(str string)add_history_time(STRING)βvoid add_history_time(str string) # GRL 5.0remove_history(WHICH)βstr remove_history(int which)free_history(HISTENT)β Not implemented, because Term::ReadLine::Gnu does not support the member'data'of theHIST_ENTRYstructure.remove_history()is the one that frees the memory, bestie.
histdata_t free_history_entry(HIST_ENTRY *histent) # GRL 5.0
"replace_history_entry(WHICH, STRING)"βstr replace_history_entry(int which, str string)"clear_history"βvoid clear_history()StifleHistory(MAX)βint stifle_history(int max|undef)
stifles the history list, remembering only the last MAX entries. If MAX is undef, it remembers all entries. This is a replacement of unstifle_history().
"unstifle_history"βint unstifle_history()
This is equivalent with stifle_history(undef).
"history_is_stifled"βint history_is_stifled()"SetHistory(LINE1 [, LINE2, ...])"β sets the history of input, and from there it can be used if the actual"readline"is present.
π Information About the History List π
"history_list"β Not implemented, since it's not required for Perl.
HIST_ENTRY **history_list()
"where_history"βint where_history()"current_history"βstr current_history()history_get(OFFSET)βstr history_get(offset)history_get_time(OFFSET)βtime_t history_get_time(offset) # GRL 5.0"history_total_bytes"βint history_total_bytes()"GetHistory"β returns the history of input as a list, if the actual"readline"is present.
πΊ Moving Around the History List πΊ
history_set_pos(POS)βint history_set_pos(int pos)"previous_history"βstr previous_history()"next_history"βstr next_history()
π Searching the History List π
"history_search(STRING [,DIRECTION])"βint history_search(str string, int direction = -1)"history_search_prefix(STRING [,DIRECTION])"βint history_search_prefix(str string, int direction = -1)"history_search_pos(STRING [,DIRECTION [,POS]])"β
int history_search_pos(str string,
int direction = -1,
int pos = where_history())
π Managing the History File π
"ReadHistory([FILENAME [,FROM [,TO]]])"β
int read_history(str filename = '~/.history',
int from = 0, int to = -1)
int read_history_range(str filename = '~/.history',
int from = 0, int to = -1)
adds the contents of FILENAME to the history list, a line at a time. If FILENAME is false, then it reads from ~/.history. Start reading at line FROM and end at TO. If FROM is omitted or zero, you start at the beginning. If TO is omitted or less than FROM, then it reads until the end of the file. Returns true if successful, or false if not. read_history() is an alias of read_history_range().
WriteHistory([FILENAME])βint write_history(str filename = '~/.history')
writes the current history to FILENAME, overwriting FILENAME if necessary. If FILENAME is false, then it writes the history list to ~/.history. Returns true if successful, or false if not.
"append_history(NELEMENTS [,FILENAME])"βint append_history(int nelements, str filename = '~/.history')"history_truncate_file([FILENAME [,NLINES]])"β
int history_truncate_file(str filename = '~/.history',
int nlines = 0)
π₯ History Expansion π₯
history_expand(STRING)β(int result, str expansion) history_expand(str string)
Note that this function returns "expansion" in the scalar context.
"get_history_event(STRING, CINDEX [,QCHAR])"β
(str text, int cindex) = get_history_event(str string,
int cindex,
char qchar = '\0')
history_tokenize(STRING)β(@str) history_tokenize(str string)"history_arg_extract(STRING, [FIRST [,LAST]])"βstr history_arg_extract(str string, int first = 0, int last = '$')
π "Term::ReadLine::Gnu" Variables π
okay so all of these GNU Readline/History Library variables can be accessed from a Perl program, fr fr. π Go read the GNU Readline Library Manual (https://tiswww.cwru.edu/php/chet/readline/readline.html) and the GNU History Library Manual (https://tiswww.cwru.edu/php/chet/readline/history.html) for the full deets on each variable. You get at them by using "Attribs" methods. The key names in this hash conform to standard conventions with the leading "rl_" stripped off. slay.
Examples:
$attribs = $term->Attribs;
$v = $attribs->{library_version}; # rl_library_version
$v = $attribs->{history_base}; # history_base
Readline Variables
str rl_line_buffer
int rl_point
int rl_end
int rl_mark
int rl_done
int rl_eof_found (GRL 8.2)
int rl_num_chars_to_read (GRL 4.1)
int rl_pending_input
int rl_dispatching
int rl_erase_empty_line (GRL 4.0)
str rl_prompt (read only)
str rl_display_prompt
int rl_already_prompted (GRL 4.1)
str rl_library_version (read only)
int rl_readline_version (read only)
int rl_gnu_readline_p (GRL 4.1, read only)
str rl_terminal_name
str rl_readline_name
filehandle rl_instream
filehandle rl_outstream
int rl_prefer_env_winsize (GRL 5.1)
pfunc rl_last_func (GRL 4.2, read only)
pfunc rl_startup_hook
pfunc rl_pre_input_hook (GRL 4.0)
pfunc rl_event_hook
pfunc rl_getc_function
pfunc rl_signal_event_hook (GRL 6.3)
pfunc rl_timeout_event_hook (GRL 8.2)
pfunc rl_input_available_hook (GRL 6.3)
pfunc rl_redisplay_function
pfunc rl_prep_term_function (GRL 2.1)
pfunc rl_deprep_term_function (GRL 2.1)
pfunc rl_macro_display_hook (GRL 8.3)
Keymap rl_executing_keymap (read only)
Keymap rl_binding_keymap (read only)
str rl_executing_macro (GRL 4.2, read only)
int rl_executing_key (GRL 6.3, read only)
str rl_executing_keyseq (GRL 6.3, read only)
int rl_key_sequence_length (read only)
int rl_readline_state (GRL 4.2)
int rl_explicit_arg (read only)
int rl_numeric_arg (read only)
int rl_editing_mode (read only)
Signal Handling Variables
int rl_catch_signals (GRL 4.0)
int rl_catch_sigwinch (GRL 4.0)
int rl_persistent_signal_handlers (GRL 7.0)
int rl_change_environment (GRL 6.3)
Completion Variables
pfunc rl_completion_entry_function
pfunc rl_attempted_completion_function
pfunc rl_filename_quoting_function
pfunc rl_filename_dequoting_function
pfunc rl_char_is_quoted_p
pfunc rl_ignore_some_completions_function
pfunc rl_directory_completion_hook
pfunc rl_directory_rewrite_hook (GRL 4.2)
pfunc rl_filename_stat_hook (GRL 6.3)
pfunc rl_filename_rewrite_hook (GRL 6.1)
pfunc rl_completion_rewrite_hook (GRL 8.3)
pfunc rl_completion_display_matches_hook (GRL 4.0)
str rl_basic_word_break_characters
str rl_basic_quote_characters
str rl_completer_word_break_characters
pfunc rl_completion_word_break_hook (GRL 5.0)
str rl_completer_quote_characters
str rl_filename_quote_characters
str rl_special_prefixes
int rl_completion_query_items
int rl_completion_append_character
int rl_completion_suppress_append (GRL 4.3)
int rl_completion_suppress_quote (GRL 5.0)
int rl_completion_found_quote (GRL 5.0, read only)
int rl_completion_quote_character (GRL 5.0, read only)
int rl_completion_mark_symlink_dirs (GRL 4.3)
int rl_ignore_completion_duplicates
int rl_filename_completion_desired
int rl_filename_quoting_desired
int rl_full_quoting_desired (GRL 8.3)
int rl_attempted_completion_over
int rl_sort_completion_matches (GRL 6.0)
int rl_completion_type (read only)
int rl_completion_invoking_key (GRL 6.0, read only)
int rl_inhibit_completion
History Variables
int history_base
int history_length
int history_max_entries (called `max_input_history', read only)
int history_write_timestamps (GRL 5.0)
char history_expansion_char
char history_subst_char
char history_comment_char
str history_word_delimiters (GRL 4.2)
str history_search_delimiter_chars
str history_no_expand_chars
int history_quotes_inhibit_expansion
int history_quoting_state
pfunc history_inhibit_expansion_function
Function References
rl_getc
rl_redisplay
rl_callback_read_char
rl_display_match_list
rl_filename_completion_function
rl_username_completion_function
list_completion_function
shadow_redisplay
Tk_getc
πΈ Custom Completion πΈ
In this section, variables and functions for custom completion get described, along with examples. iconic.
Most of the descriptions in this section came from the GNU Readline Library Manual (https://tiswww.cwru.edu/php/chet/readline/readline.html).
"completion_entry_function"
This variable holds a reference β and that reference refers to a generator function for completion_matches(). π«Ά
A generator function gets called over and over by completion_matches(), handing back one string per call. The args it takes are "TEXT" and "STATE". "TEXT" is the partial word you're trying to complete. "STATE" is zero the very first time it's called β so the generator can do whatever setup it needs β and then a positive non-zero integer on every call after that. When the generator function returns "undef", that's it telling completion_matches() there are no more possibilities left. β¨
If this variable is set to undef, the built-in "filename_completion_function" is used instead. π
There's a sample generator function, "list_completion_function", defined in Gnu.pm. You can use it like this:
use Term::ReadLine;
...
my $term = new Term::ReadLine 'sample';
my $attribs = $term->Attribs;
...
$attribs->{completion_entry_function} =
$attribs->{list_completion_function};
...
$attribs->{completion_word} =
[qw(reference to a list of words which you want to use for completion)];
$term->readline("custom completion>");
See also "completion_matches". π«Ά
"attempted_completion_function"
A reference to an alternative function to create matches. Basically you get to be the main character here. π
The function is called with "TEXT", "LINE_BUFFER", "START", and "END". "LINE_BUFFER" is the current input buffer string. "START" and "END" are indices in "LINE_BUFFER" that say what the boundaries of "TEXT" are. π
If this function exists and returns a null list or "undef", or if this variable is set to "undef", then an internal function rl_complete() will call the value of "completion_entry_function" to generate matches. Otherwise, the array of strings returned gets used.
The default value of this variable is "undef". You can use it like this:
use Term::ReadLine;
...
my $term = new Term::ReadLine 'sample';
my $attribs = $term->Attribs;
...
sub sample_completion {
my ($text, $line, $start, $end) = @_;
# If first word then username completion, else filename completion
if (substr($line, 0, $start) =~ /^\s*$/) {
return $term->completion_matches($text,
$attribs->{'username_completion_function'});
} else {
return ();
}
}
...
$attribs->{attempted_completion_function} = \&sample_completion;
"completion_matches(TEXT, ENTRY_FUNC)"
Returns an array of strings, which is a list of completions for "TEXT". If there are no completions at all, it returns "undef". The first entry in the returned array is the substitution for "TEXT". The remaining entries are the possible completions. π
"ENTRY_FUNC" is a generator function that takes two arguments and returns a string. The first argument is "TEXT". The second is a state argument β it's zero on the first call, and non-zero on subsequent calls. "ENTRY_FUNC" returns "undef" to the caller when there are no more matches.
If the value of "ENTRY_FUNC" is undef, the built-in "filename_completion_function" is used.
"completion_matches" is a Perl wrapper function of an internal function completion_matches(). See also "completion_entry_function". π¦
"completion_function"
A variable whose content is a reference to a function which returns a list of candidates to complete. It's giving plug and play.
This variable is compatible with Term::ReadLine::Perl (http://search.cpan.org/dist/Term-ReadLine-Perl/) and is very easy to use. π
use Term::ReadLine;
...
my $term = new Term::ReadLine 'sample';
my $attribs = $term->Attribs;
...
$attribs->{completion_function} = sub {
my ($text, $line, $start) = @_;
return qw(a list of candidates to complete);
};
"list_completion_function(TEXT, STATE)"
A sample generator function defined by "Term::ReadLine::Gnu". The example code over at "completion_entry_function" shows how to use this function. β¨
"Term::ReadLine::Gnu" Specific Features
"Term::ReadLine::Gnu" Specific Functions
"CallbackHandlerInstall(PROMPT, LHANDLER)"
This method provides the function rl_callback_handler_install() with the following additional features, compatible with the "readline" method: ornament feature, Term::ReadLine::Perl (http://search.cpan.org/dist/Term-ReadLine-Perl/) compatible completion function, history expansion, and addition to the history buffer. π
"call_function(FUNCTION, [COUNT [,KEY]])"
int rl_call_function(FunctionPtr|str function, count = 1, key = -1)
"get_all_function_names"
Returns a list of all function names. π
"shadow_redisplay"
A redisplay function for password input. Password input, bestie!! π You can use it like this:
$attribs->{redisplay_function} = $attribs->{shadow_redisplay};
$line = $term->readline("password> ");
"filename_list"
Returns candidates of filenames to complete. This function can be used with "completion_function" and is implemented for compatibility with Term::ReadLine::Perl (http://search.cpan.org/dist/Term-ReadLine-Perl/). ποΈ
"list_completion_function"
See the description of section "Custom Completion".
"Term::ReadLine::Gnu" Specific Variables
"do_expand"
When true, history expansion is enabled. By default it's false.
"completion_function"
See the description of section "Custom Completion".
"completion_word"
A reference to a list of candidates to complete for "list_completion_function".
"Term::ReadLine::Gnu" Specific Commands
"history-expand-line"
The equivalent of the Bash "history-expand-line" editing command. π₯
"operate-and-get-next"
The equivalent of the Korn shell "operate-and-get-next-history-line" editing command and the Bash "operate-and-get-next".
This command is bound to "\C-o" by default, for compatibility with the Bash and Term::ReadLine::Perl (http://search.cpan.org/dist/Term-ReadLine-Perl/). β¨οΈ
"display-readline-version"
Shows the version of "Term::ReadLine::Gnu" and the one of the GNU Readline Library. π
"change-ornaments"
Change ornaments interactively. β¨
π FILES π
~/.inputrcβ the Readline init file. Using this file, it's possible that you'd like to use a different set of key bindings. When a program which uses the GNU Readline library starts up, the init file is read, and the key bindings are set. π
The conditional init constructs are supported. The program name which is specified by the first argument of the "new" method is used as the application construct.
For example, when your program calls the "new" method like this:
...
$term = new Term::ReadLine 'PerlSh';
...
your ~/.inputrc can define key bindings only for that program, like this:
...
$if PerlSh
Meta-Rubout: backward-kill-word
"\C-x\C-r": re-read-init-file
"\e[11~": "Function Key 1"
$endif
...
For further details, see the section "Readline Init File" in the GNU Readline Library Manual (https://tiswww.cwru.edu/php/chet/readline/readline.html). π¦
π EXPORTS π
No symbols are exported by default. The following tags are defined and their symbols can be exported. π«Ά
readerrβREADERRpromptβRL_PROMPT_START_IGNORERL_PROMPT_END_IGNOREmatch_typeβNO_MATCHSINGLE_MATCHMULT_MATCHkeymap_typeβISFUNCISKMAPISMACRundo_codeβUNDO_DELETEUNDO_INSERTUNDO_BEGINUNDO_ENDrl_stateβRL_STATE_NONERL_STATE_INITIALIZINGRL_STATE_INITIALIZEDRL_STATE_TERMPREPPEDRL_STATE_READCMDRL_STATE_METANEXTRL_STATE_DISPATCHINGRL_STATE_MOREINPUTRL_STATE_ISEARCHRL_STATE_NSEARCHRL_STATE_SEARCHRL_STATE_NUMERICARGRL_STATE_MACROINPUTRL_STATE_MACRODEFRL_STATE_OVERWRITERL_STATE_COMPLETINGRL_STATE_SIGHANDLERRL_STATE_UNDOINGRL_STATE_INPUTPENDINGRL_STATE_TTYCSAVEDRL_STATE_CALLBACKRL_STATE_VIMOTIONRL_STATE_MULTIKEYRL_STATE_VICMDONCERL_STATE_CHARSEARCHRL_STATE_REDISPLAYINGRL_STATE_DONERL_STATE_TIMEOUTRL_STATE_EOF
They can be exported like this:
use Term::ReadLine;
BEGIN {
import Term::ReadLine::Gnu qw(:keymap_type RL_STATE_INITIALIZED);
}
π ENVIRONMENT π
The environment variable "PERL_RL" governs which ReadLine clone gets loaded. See the ENVIRONMENT section on Term::ReadLine (http://search.cpan.org/dist/Term-ReadLine/) for further details. π
If the environment variable "TERM" is set to "emacs", or the environment variable "INSIDE_EMACS" or "EMACS" is defined, then "Term::ReadLine::Stub" is used instead of <Term::ReadLine:Gnu>. π
β¨ SEE ALSO β¨
okay bestie, here's the reading list π β every single one of these is a real one, no cap:
- Term::ReadLine::Gnu Project Home Page β <https://github.com/hirooih/perl-trg> πΈ
- GNU Readline Library Manual β <https://tiswww.cwru.edu/php/chet/readline/readline.html> π
- GNU History Library Manual β <https://tiswww.cwru.edu/php/chet/readline/history.html> π¦
- Sample and test programs (
eg/*andt/*) in the"Term::ReadLine::Gnu"distribution β <http://search.cpan.org/dist/Term-ReadLine-Gnu/> π - Term::ReadLine β <http://search.cpan.org/dist/Term-ReadLine/> β
- Works which use Term::ReadLine::Gnu π
- Distributions which depend on Term::ReadLine::Gnu on CPAN β <http://www.cpan.org/> and <https://metacpan.org/requires/distribution/Term-ReadLine-Gnu>
- Perl Debugger β <http://perldoc.perl.org/perldebug.html> and fr, you run it like this:
perl -d
- Perl Shell (psh) β <http://gnp.github.io/psh/> The Perl Shell is a shell that combines the interactive nature of a Unix shell with the power of Perl.
A programmable completion feature compatible with bash is implemented. π
- SPP (Synopsys Plus Perl) β <http://vlsiweb.stanford.edu/~jsolomon/SPP/> SPP (Synopsys Plus Perl) is a Perl module that wraps around Synopsys' shell programs. SPP is inspired by the original dc_perl written by Steve Golson, but it's an entirely new implementation. Why is it called SPP and not dc_perl? Well, SPP was written to wrap around any of Synopsys' shells. π€·ββοΈ
- PFM (Personal File Manager for Unix/Linux) β <http://p-f-m.sourceforge.net/> Pfm is a terminal-based file manager written in Perl, based on PFM.COM for MS-DOS (originally by Paul Culley and Henk de Heer).
- The soundgrab β <https://sourceforge.net/projects/soundgrab/> soundgrab is designed to help you slice up a big long raw audio file (by default 44.1 kHz 2 channel signed sixteen bit little endian) and save your favorite sections to other files. It does this by providing you with a cassette player like command line interface. π§
- PDL (The Perl Data Language) β <http://pdl.perl.org/> PDL (``Perl Data Language'') gives standard Perl the ability to compactly store and speedily manipulate the large N-dimensional data arrays which are the bread and butter of scientific computing. π₯
- PIQT (Perl Interactive DBI Query Tool) β <http://piqt.sourceforge.net/> PIQT is an interactive query tool using the Perl DBI database interface. It supports ReadLine, provides a built in scripting language with a Lisp like syntax, an online help system, and uses wrappers to interface to the DBD modules.
- vshnu (the New Visual Shell) β <http://www.cs.indiana.edu/~kinzler/vshnu/> A visual shell and CLI shell supplement.
If you know any other works you recommend, please let me know. π«Ά (yes, for real, they want to hear from you)
π AUTHOR π
Hiroo Hayashi "<hiroo.hayashi@computer.org>"
<http://search.cpan.org/~hayashi/>
π§ TODO π§
GTK+ support in addition to Tk. (manifesting this one β¨)
π BUGS π
- Submit a bug report to the bug tracker on GitHub β <https://github.com/hirooih/perl-trg/issues>. π
add_defun()can define up to 16 functions including predefined functions. See #23 β <https://github.com/hirooih/perl-trg/issues/23> β for details. (sixteen. that's the cap. not me counting on my fingers π)- Some functions and variables do not have test code yet. Your contribution is welcome. See
t/readline.tfor details. π - If the pager command (
|or||) in Perl debugger causes segmentation fault, you need to fixperl5db.pl. See <https://rt.perl.org/Public/Bug/Display.html?id=121456> for details. π
okay so that last one is a whole situation β segfaults are not the vibe, go read that ticket if you're hitting it.
π LICENSE π
Copyright (c) 1996-2024 Hiroo Hayashi. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. π
and that's a wrap on this whole page, bestie π«Ά this was perl v5.42.0, dated 2025-07-25, and it's Gnu(3) β go forth and readline responsibly πβ¨
π¦ where this page comes from
- fedora 43: perl-Term-ReadLine-Gnu 1.47-3.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms