CPANPLUS

 view release on metacpan or  search on metacpan

inc/bundle/Term/UI.pm  view on Meta::CPAN


    ### 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) and
the 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 over
and over again. You can use more sophisticated C<allow> handlers (even
subroutines can be used). The C<allow> handler is implemented using
C<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]:  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 C<$Term::UI::AUTOREPLY> to true, will
return 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 Also

C<Params::Check>, C<Term::ReadLine>, C<Term::UI::History>

=head1 BUG REPORTS

Please report bugs or other issues to E<lt>bug-term-ui@rt.cpan.org<gt>.

=head1 AUTHOR

This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.

=head1 COPYRIGHT

This library is free software; you may redistribute and/or modify it
under the same terms as Perl itself.

=cut



( run in 1.375 second using v1.01-cache-2.11-cpan-39bf76dae61 )