ui.pm
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PM 代码 · 共 621 行 · 第 1/2 页
PM
621 行
### if we're allowed to give multiple answers, split ### the answer on whitespace my @answers = $multi ? split(/\s+/, $answer) : $answer; ### the return value list my @rv; if( @$choices ) { for my $answer (@answers) { ### a digit implies a multiple choice question, ### a non-digit is an open answer if( $answer =~ /\D/ ) { push @rv, $answer if allow( $answer, $allow ); } else { ### remember, the answer digits are +1 compared to ### the choices, because humans want to start counting ### at 1, not at 0 push @rv, $choices->[ $answer - 1 ] if $answer > 0 && defined $choices->[ $answer - 1]; } } ### no fixed list of choices.. just check if the answers ### (or otherwise the default!) pass the allow handler } else { push @rv, grep { allow( $_, $allow ) } scalar @answers ? @answers : ($default); } ### if not all the answers made it to the return value list, ### at least one of them was an invalid answer -- make the ### user do it again if( (@rv != @answers) or (scalar(@$choices) and not scalar(@answers)) ) { $prompt = $INVALID; $prompt .= "[$prompt_add] " if $prompt_add; redo LOOP; ### otherwise just return the answer, or answers, depending ### on the multi setting } else { return $multi ? @rv : $rv[0]; } }}=head2 ($opts, $munged) = $term->parse_options( STRING );C<parse_options> 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.Consider this example: my $str = q[command --no-foo --baz --bar=0 --quux=bleh ] . q[--option="some'thing" -one-dash -single=blah' arg]; my ($options,$munged) = $term->parse_options($str); ### $options would contain: ### $options = { 'foo' => 0, 'bar' => 0, 'one-dash' => 1, 'baz' => 1, 'quux' => 'bleh', 'single' => 'blah\'', 'option' => 'some\'thing' }; ### and this is the munged version of the input string, ### ie what's left of the input minus the options $munged = 'command arg';As you can see, you can either use a single or a double C<-> toindicate an option.If you prefix an option with C<no-> 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.=cutsub parse_options { my $term = shift; my $input = shift; my $return = {}; ### there's probably a more elegant way to do this... ### while ( $input =~ s/(?:^|\s+)--?([-\w]+=("|').+?\2)(?=\Z|\s+)// or $input =~ s/(?:^|\s+)--?([-\w]+=\S+)(?=\Z|\s+)// or $input =~ s/(?:^|\s+)--?([-\w]+)(?=\Z|\s+)// ) { my $match = $1; if( $match =~ /^([-\w]+)=("|')(.+?)\2$/ ) { $return->{$1} = $3; } elsif( $match =~ /^([-\w]+)=(\S+)$/ ) { $return->{$1} = $2; } elsif( $match =~ /^no-?([-\w]+)$/i ) { $return->{$1} = 0; } elsif ( $match =~ /^([-\w]+)$/ ) { $return->{$1} = 1; } else { carp(loc(q[I do not understand option "%1"\n], $match)) if $VERBOSE; } } return wantarray ? ($return,$input) : $return;}=head2 $str = $term->history_as_stringConvenience wrapper around C<< Term::UI::History->history_as_string >>.Consult the C<Term::UI::History> man page for details.=cutsub history_as_string { return Term::UI::History->history_as_string };1;=head1 GLOBAL VARIABLESThe behaviour of Term::UI can be altered by changing the followingglobal variables:=head2 $Term::UI::VERBOSEThis 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;=head2 $Term::UI::AUTOREPLYThis 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;=head2 $Term::UI::INVALIDThis 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 C<Invalid selection, please try again: >=head2 $Term::UI::History::HISTORY_FHThis is the filehandle all the print statements from this moduleare being sent to. Please consult the C<Term::UI::History> manpagefor details.This defaults to C<*STDOUT>.=head1 EXAMPLES=head2 Basic get_reply sample ### ask a user (with an open question) for their favourite colour $reply = $term->get_reply( prompt => 'Your favourite colour? ); which would look like: Your favourite colour? and C<$reply> would hold the text the user typed.=head2 get_reply with choices ### now provide a list of choices, so the user has to pick one $reply = $term->get_reply( prompt => 'Your favourite colour?', choices => [qw|red green blue|] ); which would look like: 1> red 2> green 3> blue Your favourite colour? C<$reply> will hold one of the choices presented. C<Term::UI> 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 C<$Term::UI::INVALID>variable (see the C<GLOBAL VARIABLES> section for details.=head2 get_reply with choices and default ### provide a sensible default option -- everyone loves blue! $reply = $term->get_reply( prompt => 'Your favourite colour?', choices => [qw|red green blue|], default => 'blue' );which would look like: 1> red 2> green 3> blue Your favourite colour? [3]: Note the default answer after the prompt. A user can now just hit C<enter>(or set C<$Term::UI::AUTOREPLY> -- see the C<GLOBAL VARIABLES> section) andthe sensible answer 'blue' will be returned.=head2 get_reply using print_me & multi ### allow the user to pick more than one colour and add an ### introduction text @reply = $term->get_reply( print_me => 'Tell us what colours you like', prompt => 'Your favourite colours?', choices => [qw|red green blue|], multi => 1 );which would look like: Tell us what colours you like 1> red 2> green 3> blue Your favourite colours?An answer of C<3 2 1> would fill C<@reply> with C<blue green red>=head2 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->get_reply( prompt => "What is the magic number?", allow => 42 ); Unless the user now enters C<42>, the question will be reposed overand over again. You can use more sophisticated C<allow> handlers (evensubroutines can be used). The C<allow> handler is implemented usingC<Params::Check>'s C<allow> function. Check its manpage for details.=head2 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->ask_yn( prompt => 'Do you like cookies?', default => 'y', print_me => 'Cookies are LOVELY!!!' ); would print: Cookies are LOVELY!!! Do you like cookies? [Y/n]: If a user then simply hits C<enter>, agreeing with the default, C<$bool> would be set to C<true>. (Simply hitting 'y' would also return C<true>. Hitting 'n' would return C<false>)We could later retrieve this interaction by printing out the Q&A history as follows: print $term->history_as_string;which would then print: Cookies are LOVELY!!! Do you like cookies? [Y/n]: yThere's a chance we're doing this non-interactively, because a consoleis missing, the user indicated he just wanted the defaults, etc.In this case, simply setting C<$Term::UI::AUTOREPLY> to true, willreturn from every question with the default answer set for the question.Do note that if C<AUTOREPLY> is true, and no default is set, C<Term::UI>will warn about this and return C<undef>.=head1 See AlsoC<Params::Check>, C<Term::ReadLine>, C<Term::UI::History>=head1 BUG REPORTSPlease report bugs or other issues to E<lt>bug-term-ui@rt.cpan.org<gt>.=head1 AUTHORThis module by Jos Boumans E<lt>kane@cpan.orgE<gt>.=head1 COPYRIGHTThis library is free software; you may redistribute and/or modify it under the same terms as Perl itself.=cut
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?