Term::Prompt(3pm) | User Contributed Perl Documentation | Term::Prompt(3pm) |
Term::Prompt - Perl extension for prompting a user for information
use Term::Prompt; $value = prompt(...); use Term::Prompt qw(termwrap); print termwrap(...); $Term::Prompt::MULTILINE_INDENT = '';
You must have Text::Wrap and Term::ReadKey available on your system.
This main function of this module is to accept interactive input. You specify the type of inputs allowed, a prompt, help text and defaults and it will deal with the user interface, (and the user!), by displaying the prompt, showing the default, and checking to be sure that the response is one of the legal choices. Additional 'types' that could be added would be a phone type, a social security type, a generic numeric pattern type...
This is the main function of the module. Its first argument determines its usage and is one of the following single characters:
x: do not care a: alpha-only n: numeric-only i: ignore case c: case sensitive r: ranged by the low and high values f: floating-point y: yes/no e: regular expression s: sub (actually, a code ref, but 'c' was taken) p: password (keystrokes not echoed) m: menu
$result = prompt('x', 'text prompt', 'help prompt', 'default' );
$result is whatever the user types.
$result = prompt('a', 'text prompt', 'help prompt', 'default' );
$result is a single 'word' consisting of [A-Za-z] only. The response is rejected until it conforms.
$result = prompt('n', 'text prompt', 'help prompt', 'default' );
The result will be a positive integer or 0.
$result = prompt('-n', 'text prompt', 'help prompt', 'default' );
The result will be a negative integer or 0.
$result = prompt('+-n', 'text prompt', 'help prompt', 'default' );
The result will be a any integer or 0.
$result = prompt('i', 'text prompt', 'help prompt', 'default', 'legal_options-ignore-case-list');
$result = prompt('c', 'text prompt', 'help prompt', 'default', 'legal_options-case-sensitive-list');
$result = prompt('r', 'text prompt', 'help prompt', 'default', 'low', 'high');
$result = prompt('f', 'text prompt', 'help prompt', 'default');
The result will be a floating-point number.
$result = prompt('y', 'text prompt', 'help prompt', 'default')
The result will be 1 for y, 0 for n. A default not starting with y, Y, n or N will be treated as y for positive, n for negative.
$result = prompt('e', 'text prompt', 'help prompt', 'default', 'regular expression');
The regular expression has and implicit ^ and $ surrounding it; just put in .* before or after if you need to free it up before or after.
$result = prompt('s', 'text prompt', 'help prompt', 'default', sub { warn 'Your input was ' . shift; 1 }); $result = prompt('s', 'text prompt', 'help prompt', 'default', \&my_custom_validation_handler);
User reply is passed to given code reference as first and only argument. If code returns true, input is accepted.
$result = prompt('p', 'text prompt', 'help prompt', 'default' );
$result is whatever the user types, but the characters are not echoed to the screen.
@results = prompt( 'm', { prompt => 'text prompt', title => 'My Silly Menu', items => [ qw (foo bar baz biff spork boof akak) ], order => 'across', rows => 1, cols => 1, display_base => 1, return_base => 0, accept_multiple_selections => 0, accept_empty_selection => 0, ignore_whitespace => 0, separator => '[^0-9]+' }, 'help prompt', 'default');
This will create a menu with numbered items to select. You replace the normal prompt argument with a hash reference containing this information:
1) foo 2) bar 3) baz 4) biff 5) spork 6) boof 7) akak
If set to 'down', the item numbers run down the menu:
1) foo 4) biff 7) akak 2) bar 5) spork 3) baz 6) boof
'down' is the default.
Usually, you would set rows = 1 or cols = 1 to force a non-wrapped layout. Setting both in tandem is untested. Cavet programmer.
The defaults are 1 and 0, respectively.
The default is 0. The return value is a single scalar containing the selection.
[,] ## Commas [,/] ## Commas or slashes [,/\s] ## Commas or slashes or whitespace
1, 5, 6
is collapsed to
1,5,6
before parsing. NOTE: Do not set this option if you are including whitespace as a legal separator.
1,,8,9
will result in a return of
(1,'',8,9)
When set, the return will be:
(1,8,9)
which is probably what you want.
$Term::Prompt::MULTILINE_INDENT = '';
What, you might ask, is the difference between a 'text prompt' and a 'help prompt'? Think about the case where the 'legal_options' look something like: '1-1000'. Now consider what happens when you tell someone that '0' is not between 1-1000 and that the possible choices are: :) 1 2 3 4 5 ..... This is what the 'help prompt' is for.
It will work off of unique parts of 'legal_options'.
Changed by Allen - if you capitalize the type of prompt, it will be treated as a true 'help prompt'; that is, it will be printed ONLY if the menu has to be redisplayed due to and entry error. Otherwise, it will be treated as a list of options and displayed only the first time the menu is displayed.
Capitalizing the type of prompt will also mean that a return may be accepted as a response, even if there is no default; whether it actually is will depend on the type of prompt. Menus, for example, do not do this.
Original Author: Mark Henderson (henderson@mcs.anl.gov or systems@mcs.anl.gov). Derived from im_prompt2.pl, from anlpasswd (see ftp://info.mcs.anl.gov/pub/systems/), with permission.
Contributors:
E. Allen Smith (easmith@beatrice.rutgers.edu): Revisions for Perl 5, additions of alternative help text presentation, floating point type, regular expression type, yes/no type, line wrapping and regular expression functionality added by E. Allen Smith.
Matthew O. Persico (persicom@cpan.org): Addition of menu functionality and $Term::Prompt::MULTILINE_INDENT.
Tuomas Jormola (tjormola@cc.hut.fi): Addition of code refs.
Current maintainer: Matthew O. Persico (persicom@cpan.org)
perl, Term::ReadKey, and Text::Wrap.
Copyright (C) 2004 by Matthew O. Persico
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.6.1 or, at your option, any later version of Perl 5 you may have available.
2022-06-17 | perl v5.34.0 |