METHODS

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

&quot;choices&quot;

argument. If the answer is not in the list of choices presented, the question will be reposed.

If you provide a

&quot;default&quot;

answer, this will be returned when either

$AUTOREPLY

is set to true, (see the

&quot;GLOBAL VARIABLES&quot;

section further below), or when the user just hits

&quot;enter&quot;

.

You can indicate that the user is allowed to enter multiple answers by toggling the

&quot;multi&quot;

flag. Note that a list of answers will then be returned to you, rather than a simple string.

By specifying an

&quot;allow&quot;

handler, you can yourself validate the answer a user gives. This can be any of the types that the Params::Check

&quot;allow&quot;

function allows, so please refer to that manpage for details.

Finally, you have the option of adding a

&quot;print_me&quot;

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

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

&quot;Term::UI-&gt;history_as_string&quot;

function.

See the

&quot;EXAMPLES&quot;

section for samples of how to use this function.

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

&quot;parse_options&quot;

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.

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

&quot;Term::UI::History-&gt;history_as_string&quot;

.

Consult the

&quot;Term::UI::History&quot;

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

&quot;Invalid selection, please try again: &quot;

$Term::UI::History::HISTORY_FH

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

get_reply & allow

### pose an open question, but do a custom verification on
### the answer, which will only exit the question loop, if
### the answer matches the allow handler.
$reply = $term-&gt;get_reply(
prompt =&gt; &quot;What is the magic number?&quot;,
allow =&gt; 42 );

Unless the user now enters

42

, the question will be reposed over and over again. You can use more sophisticated

&quot;allow&quot;

handlers (even subroutines can be used). The

&quot;allow&quot;

handler is implemented using

&quot;Params::Check&quot;

's

&quot;allow&quot;

function. Check its manpage for details.

an elaborate ask_yn sample

### ask a user if he likes cookies. Default to a sensible 'yes'
### and inform him first what cookies are.
$bool = $term-&gt;ask_yn( prompt =&gt; 'Do you like cookies?',
default =&gt; 'y',
print_me =&gt; 'Cookies are LOVELY!!!' );

would print:

Cookies are LOVELY!!!
Do you like cookies? [Y/n]:

If a user then simply hits

&quot;enter&quot;

, agreeing with the default,

$bool

would be set to

&quot;true&quot;

. (Simply hitting 'y' would also return

&quot;true&quot;

. Hitting 'n' would return

&quot;false&quot;

)

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

print $term-&gt;history_as_string;

which would then print:

Cookies are LOVELY!!!
Do you like cookies? [Y/n]: y

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

$Term::UI::AUTOREPLY

to true, will return from every question with the default answer set for the question. Do note that if