Codex

Term::UI

Section: User Contributed Perl Documentation (3pm)

Updated: 2014-01-02

Index?action=index Return to Main Contents


NAME

Term::UI - Term::ReadLine UI made easy

SYNOPSIS

DESCRIPTION

is a transparent way of eliminating the overhead of having to format a question and then validate the reply, informing the user if the answer was not proper and re-issuing the question.
Simply give it the question you want to ask, optionally with choices the user can pick from and a default and will DWYM.

For asking a yes or no question, there's even a shortcut.

HOW IT WORKS

places itself at the back of the array, so you can call its functions through your term object.
uses to record all interactions with the commandline. You can retrieve this history, or alter the filehandle the interaction is printed to. See the manpage or the for details.

METHODS

$reply = $term->get_reply( prompt => 'question?', [choices => \@list, default => $list[0], multi => BOOL, print_me => extra text to print & record, allow => $ref] );

asks a user a question, and then returns the reply to the caller. If the answer is invalid (more on that below), the question will be reposed, until a satisfactory answer has been entered.
You have the option of providing a list of choices the user can pick from using the argument. If the answer is not in the list of choices presented, the question will be reposed.
If you provide a answer, this will be returned when either is set to true, (see the section further below), or when the user just hits .
You can indicate that the user is allowed to enter multiple answers by toggling the flag. Note that a list of answers will then be returned to you, rather than a simple string.
By specifying an handler, you can yourself validate the answer a user gives. This can be any of the types that the Params::Check function allows, so please refer to that manpage for details.
Finally, you have the option of adding a argument, which is simply printed before the prompt. It's printed to the same file handle as the rest of the questions, so you can use this to keep track of a full session of Q&A with the user, and retrieve it later using the function.
See the section for samples of how to use this function.

$bool = $term->ask_yn( prompt => your question, [default => (y|1,n|0), print_me => extra text to print & record] )

Asks a simple or question to the user, returning a boolean indicating or to the caller.
The answer will automatically returned, if the user hits or if is set to true. See the section further below.
Also, you have the option of adding a argument, which is simply printed before the prompt. It's printed to the same file handle as the rest of the questions, so you can use this to keep track of a full session of Q&A with the user, and retrieve it later using the function.
See the section for samples of how to use this function.

($opts, $munged) = $term->parse_options( STRING );

will convert all options given from an input string to a hash reference. If called in list context it will also return the part of the input string that it found no options in.

Consider this example:

As you can see, you can either use a single or a double to indicate an option. If you prefix an option with and do not give it a value, it will be set to 0. If it has no prefix and no value, it will be set to 1. Otherwise, it will be set to its value. Note also that it can deal fine with single/double quoting issues.

$str = $term->history_as_string

Convenience wrapper around .
Consult the man page for details.

GLOBAL VARIABLES

The behaviour of Term::UI can be altered by changing the following global variables:

$Term::UI::VERBOSE

This controls whether Term::UI will issue warnings and explanations as to why certain things may have failed. If you set it to 0, Term::UI will not output any warnings. The default is 1;

$Term::UI::AUTOREPLY

This will make every question be answered by the default, and warn if there was no default provided. This is particularly useful if your program is run in non-interactive mode. The default is 0;

$Term::UI::INVALID

This holds the string that will be printed when the user makes an invalid choice. You can override this string from your program if you, for example, wish to do localization. The default is

$Term::UI::History::HISTORY_FH

This is the filehandle all the print statements from this module are being sent to. Please consult the manpage for details.
This defaults to .

EXAMPLES

Basic get_reply sample

which would look like:

and would hold the text the user typed.

get_reply with choices

which would look like:

will hold one of the choices presented. will repose the question if the user attempts to enter an answer that's not in the list of choices. The string presented is held in the variable (see the section for details.

get_reply with choices and default

which would look like:

Note the default answer after the prompt. A user can now just hit (or set --- see the section) and the sensible answer 'blue' will be returned.

get_reply using print_me & multi

which would look like:

An answer of would fill with

get_reply & allow

Unless the user now enters , the question will be reposed over and over again. You can use more sophisticated handlers (even subroutines can be used). The handler is implemented using 's function. Check its manpage for details.

an elaborate ask_yn sample

would print:

If a user then simply hits , agreeing with the default, would be set to . (Simply hitting 'y' would also return . Hitting 'n' would return )

We could later retrieve this interaction by printing out the Q&A history as follows:

which would then print:

There's a chance we're doing this non-interactively, because a console is missing, the user indicated he just wanted the defaults, etc.

In this case, simply setting to true, will return from every question with the default answer set for the question. Do note that if is true, and no default is set, will warn about this and return .

See Also

, ,

BUG REPORTS

Please report bugs or other issues to <[email protected]<gt>.

AUTHOR

This module by Jos Boumans <[email protected]>.

COPYRIGHT

This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.


Index

NAME

SYNOPSIS

DESCRIPTION

HOW IT WORKS

METHODS

$reply = $term->get_reply( prompt => 'question?', [choices => \@list, default => $list[0], multi => BOOL, print_me => extra text to print & record, allow => $ref] );

$bool = $term->ask_yn( prompt => your question, [default => (y|1,n|0), print_me => extra text to print & record] )

($opts, $munged) = $term->parse_options( STRING );

$str = $term->history_as_string

GLOBAL VARIABLES

$Term::UI::VERBOSE

$Term::UI::AUTOREPLY

$Term::UI::INVALID

$Term::UI::History::HISTORY_FH

EXAMPLES

Basic get_reply sample

get_reply with choices

get_reply with choices and default

get_reply using print_me & multi

get_reply & allow

an elaborate ask_yn sample

See Also

BUG REPORTS

AUTHOR

COPYRIGHT