📄 term::ui.3
字号:
as the rest of the questions, so you can use this to keep track of afull session of Q&A with the user, and retrieve it later using the\&\f(CW\*(C`Term::UI\->history_as_string\*(C'\fR function..PPSee the \f(CW\*(C`EXAMPLES\*(C'\fR section for samples of how to use this function..ie n .Sh "($opts, $munged\fP) = \f(CW$term\->parse_options( \s-1STRING\s0 );".el .Sh "($opts, \f(CW$munged\fP) = \f(CW$term\fP\->parse_options( \s-1STRING\s0 );".IX Subsection "($opts, $munged) = $term->parse_options( STRING );"\&\f(CW\*(C`parse_options\*(C'\fR will convert all options given from an input stringto a hash reference. If called in list context it will also returnthe part of the input string that it found no options in..PPConsider this example:.PP.Vb 2\& my $str = q[command \-\-no\-foo \-\-baz \-\-bar=0 \-\-quux=bleh ] .\& q[\-\-option="some\*(Aqthing" \-one\-dash \-single=blah\*(Aq arg];\&\& my ($options,$munged) = $term\->parse_options($str);\&\& ### $options would contain: ###\& $options = {\& \*(Aqfoo\*(Aq => 0,\& \*(Aqbar\*(Aq => 0,\& \*(Aqone\-dash\*(Aq => 1,\& \*(Aqbaz\*(Aq => 1,\& \*(Aqquux\*(Aq => \*(Aqbleh\*(Aq,\& \*(Aqsingle\*(Aq => \*(Aqblah\e\*(Aq\*(Aq,\& \*(Aqoption\*(Aq => \*(Aqsome\e\*(Aqthing\*(Aq\& };\&\& ### and this is the munged version of the input string,\& ### ie what\*(Aqs left of the input minus the options\& $munged = \*(Aqcommand arg\*(Aq;.Ve.PPAs you can see, you can either use a single or a double \f(CW\*(C`\-\*(C'\fR toindicate an option.If you prefix an option with \f(CW\*(C`no\-\*(C'\fR and do not give it a value, itwill 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 dealfine with single/double quoting issues..ie n .Sh "$str\fP = \f(CW$term\->history_as_string".el .Sh "\f(CW$str\fP = \f(CW$term\fP\->history_as_string".IX Subsection "$str = $term->history_as_string"Convenience wrapper around \f(CW\*(C`Term::UI::History\->history_as_string\*(C'\fR..PPConsult the \f(CW\*(C`Term::UI::History\*(C'\fR man page for details..SH "GLOBAL VARIABLES".IX Header "GLOBAL VARIABLES"The behaviour of Term::UI can be altered by changing the followingglobal variables:.ie n .Sh "$Term::UI::VERBOSE".el .Sh "\f(CW$Term::UI::VERBOSE\fP".IX Subsection "$Term::UI::VERBOSE"This controls whether Term::UI will issue warnings and explanationsas to why certain things may have failed. If you set it to 0,Term::UI will not output any warnings.The default is 1;.ie n .Sh "$Term::UI::AUTOREPLY".el .Sh "\f(CW$Term::UI::AUTOREPLY\fP".IX Subsection "$Term::UI::AUTOREPLY"This will make every question be answered by the default, and warn ifthere was no default provided. This is particularly useful if yourprogram is run in non-interactive mode.The default is 0;.ie n .Sh "$Term::UI::INVALID".el .Sh "\f(CW$Term::UI::INVALID\fP".IX Subsection "$Term::UI::INVALID"This holds the string that will be printed when the user makes aninvalid choice.You can override this string from your program if you, for example,wish to do localization.The default is \f(CW\*(C`Invalid selection, please try again: \*(C'\fR.ie n .Sh "$Term::UI::History::HISTORY_FH".el .Sh "\f(CW$Term::UI::History::HISTORY_FH\fP".IX Subsection "$Term::UI::History::HISTORY_FH"This is the filehandle all the print statements from this moduleare being sent to. Please consult the \f(CW\*(C`Term::UI::History\*(C'\fR manpagefor details..PPThis defaults to \f(CW*STDOUT\fR..SH "EXAMPLES".IX Header "EXAMPLES".Sh "Basic get_reply sample".IX Subsection "Basic get_reply sample".Vb 2\& ### ask a user (with an open question) for their favourite colour\& $reply = $term\->get_reply( prompt => \*(AqYour favourite colour? );.Ve.PPwhich would look like:.PP.Vb 1\& Your favourite colour?.Ve.PPand \f(CW$reply\fR would hold the text the user typed..Sh "get_reply with choices".IX Subsection "get_reply with choices".Vb 4\& ### now provide a list of choices, so the user has to pick one\& $reply = $term\->get_reply(\& prompt => \*(AqYour favourite colour?\*(Aq,\& choices => [qw|red green blue|] );.Ve.PPwhich would look like:.PP.Vb 3\& 1> red\& 2> green\& 3> blue\& \& Your favourite colour?.Ve.PP\&\f(CW$reply\fR will hold one of the choices presented. \f(CW\*(C`Term::UI\*(C'\fR will reposethe question if the user attempts to enter an answer that's not in thelist of choices. The string presented is held in the \f(CW$Term::UI::INVALID\fRvariable (see the \f(CW\*(C`GLOBAL VARIABLES\*(C'\fR section for details..Sh "get_reply with choices and default".IX Subsection "get_reply with choices and default".Vb 5\& ### provide a sensible default option \-\- everyone loves blue!\& $reply = $term\->get_reply(\& prompt => \*(AqYour favourite colour?\*(Aq,\& choices => [qw|red green blue|],\& default => \*(Aqblue\*(Aq );.Ve.PPwhich would look like:.PP.Vb 3\& 1> red\& 2> green\& 3> blue\& \& Your favourite colour? [3]:.Ve.PPNote the default answer after the prompt. A user can now just hit \f(CW\*(C`enter\*(C'\fR(or set \f(CW$Term::UI::AUTOREPLY\fR \*(-- see the \f(CW\*(C`GLOBAL VARIABLES\*(C'\fR section) andthe sensible answer 'blue' will be returned..Sh "get_reply using print_me & multi".IX Subsection "get_reply using print_me & multi".Vb 7\& ### allow the user to pick more than one colour and add an \& ### introduction text\& @reply = $term\->get_reply(\& print_me => \*(AqTell us what colours you like\*(Aq, \& prompt => \*(AqYour favourite colours?\*(Aq,\& choices => [qw|red green blue|],\& multi => 1 );.Ve.PPwhich would look like:.PP.Vb 4\& Tell us what colours you like\& 1> red\& 2> green\& 3> blue\& \& Your favourite colours?.Ve.PPAn answer of \f(CW\*(C`3 2 1\*(C'\fR would fill \f(CW@reply\fR with \f(CW\*(C`blue green red\*(C'\fR.Sh "get_reply & allow".IX Subsection "get_reply & allow".Vb 6\& ### 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\->get_reply(\& prompt => "What is the magic number?",\& allow => 42 );.Ve.PPUnless the user now enters \f(CW42\fR, the question will be reposed overand over again. You can use more sophisticated \f(CW\*(C`allow\*(C'\fR handlers (evensubroutines can be used). The \f(CW\*(C`allow\*(C'\fR handler is implemented using\&\f(CW\*(C`Params::Check\*(C'\fR's \f(CW\*(C`allow\*(C'\fR function. Check its manpage for details..Sh "an elaborate ask_yn sample".IX Subsection "an elaborate ask_yn sample".Vb 5\& ### ask a user if he likes cookies. Default to a sensible \*(Aqyes\*(Aq\& ### and inform him first what cookies are.\& $bool = $term\->ask_yn( prompt => \*(AqDo you like cookies?\*(Aq,\& default => \*(Aqy\*(Aq,\& print_me => \*(AqCookies are LOVELY!!!\*(Aq );.Ve.PPwould print:.PP.Vb 2\& Cookies are LOVELY!!!\& Do you like cookies? [Y/n]:.Ve.PPIf a user then simply hits \f(CW\*(C`enter\*(C'\fR, agreeing with the default, \&\f(CW$bool\fR would be set to \f(CW\*(C`true\*(C'\fR. (Simply hitting 'y' would also return \f(CW\*(C`true\*(C'\fR. Hitting 'n' would return \f(CW\*(C`false\*(C'\fR).PPWe could later retrieve this interaction by printing out the Q&A history as follows:.PP.Vb 1\& print $term\->history_as_string;.Ve.PPwhich would then print:.PP.Vb 2\& Cookies are LOVELY!!!\& Do you like cookies? [Y/n]: y.Ve.PPThere's a chance we're doing this non-interactively, because a consoleis missing, the user indicated he just wanted the defaults, etc..PPIn this case, simply setting \f(CW$Term::UI::AUTOREPLY\fR to true, willreturn from every question with the default answer set for the question.Do note that if \f(CW\*(C`AUTOREPLY\*(C'\fR is true, and no default is set, \f(CW\*(C`Term::UI\*(C'\fRwill warn about this and return \f(CW\*(C`undef\*(C'\fR..SH "See Also".IX Header "See Also"\&\f(CW\*(C`Params::Check\*(C'\fR, \f(CW\*(C`Term::ReadLine\*(C'\fR, \f(CW\*(C`Term::UI::History\*(C'\fR.SH "BUG REPORTS".IX Header "BUG REPORTS"Please report bugs or other issues to <bug\-term\-ui@rt.cpan.org<gt>..SH "AUTHOR".IX Header "AUTHOR"This module by Jos Boumans <kane@cpan.org>..SH "COPYRIGHT".IX Header "COPYRIGHT"This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -