Term::Query 2.0 review
DownloadTerm::Query is a table-driven query routine. SYNOPSIS use Term::Query qw( query query_table query_table_set_defaults query_tab
|
|
Term::Query is a table-driven query routine.
SYNOPSIS
use Term::Query
qw( query query_table query_table_set_defaults query_table_process );
$result = query $prompt, $flags, [ $optional_args ];
$ok = query_table @array;
query_table_set_defaults @array;
$ok = query_table_process @array, &flagsub, &querysub;
query
The query subroutine fulfills the need for a generalized question-response subroutine, with programmatic defaulting, validation, condition and error checking.
Given $prompt and $flags, and possibly additional arguments, depending upon the characters in $flags, query issues a prompt to STDOUT and solicits input from STDIN. The input is validated against a set of test criteria as configured by the characters in $flags; if any of the tests fail, an error message is noted, and the query is reattempted.
When STDIN is not a tty (not interactive), prompts are not issued, and errors cause a return rather than attempting to obtain more input. This non-interactive behaviour can be disabled by setting the variable $Foce_Interactive as below:
$Term::Query::Force_Interactive = 1;
When $Force_Interactive is a non-null, non-zero value, query will issue prompts, error messages, and ask for additional input even when the input is not interactive.
query_table
The query_table subroutine performs multiple queries, by invoking query, setting associated variables with the results of each query. Prompts, flags, and other arguments for each query are given in an array, called a query table, which is passed to the query_table subroutine by reference.
query_table_set_defaults
The query_table_set_defaults subroutine causes any variables named in the given query table array to be assigned their corresponding default values, if any. This is a non-interactive subroutine.
query_table_process
A general interface to processing a query table is available with the query_table_process subroutine. It accepts a query table array, and two subroutine references, a &flagsub and a &querysub. The &flagsub is invoked on each each flag character given in the $flags argument of the query table (see below). The &querysub is invoked for each query in the query table.
The query_table and query_table_set_defaults subroutines both use query_table_process to perform their functions.
Query Table
The format of the query table array passed to query_table, query_table_set_defaults, and query_table_process subroutines is:
@array = ( $prompt1, $flags1, [ $arglist1, ... ],
$prompt2, $flags2, [ $arglist2, ... ],
...
$promptN, $flagsN, [ $arglistN, ... ] );
In English, there are three items per query: a prompt string, a flags string, and an array of arguments. Note that the syntax used above uses [ ... ] to denote a Perl 5 anonymous array, not an optional set of arguments. Of course, if there are no arguments for a particular query, the corresponding anonymous array can be the null string or zero.
The query table design is such that a query table can be created with a set of variables, their defaults, value constraints, and help strings, and it can be used to both initialize the variables' values and to interactively set their new values. The query_table_set_defaults subroutine performs the former, while query_table does the latter.
Requirements:
Perl
Term::Query 2.0 keywords