1 package App::Info::Handler::Prompt;
3 # $Id: Prompt.pm,v 1.1 2004-04-29 09:21:29 ivan Exp $
7 App::Info::Handler::Prompt - Prompting App::Info event handler
11 use App::Info::Category::FooApp;
12 use App::Info::Handler::Print;
14 my $prompter = App::Info::Handler::Print->new;
15 my $app = App::Info::Category::FooApp->new( on_unknown => $prompter );
18 my $app = App::Info::Category::FooApp->new( on_confirm => 'prompt' );
22 App::Info::Handler::Prompt objects handle App::Info events by printing their
23 messages to C<STDOUT> and then accepting a new value from C<STDIN>. The new
24 value is validated by any callback supplied by the App::Info concrete subclass
25 that triggered the event. If the value is valid, App::Info::Handler::Prompt
26 assigns the new value to the event request. If it isn't it prints the error
27 message associated with the event request, and then prompts for the data
30 Although designed with unknown and confirm events in mind,
31 App::Info::Handler::Prompt handles info and error events as well. It will
32 simply print info event messages to C<STDOUT> and print error event messages
33 to C<STDERR>. For more interesting info and error event handling, see
34 L<App::Info::Handler::Print|App::Info::Handler::Print> and
35 L<App::Info::Handler::Carp|App::Info::Handler::Carp>.
37 Upon loading, App::Info::Handler::Print registers itself with
38 App::Info::Handler, setting up a single string, "prompt", that can be passed
39 to an App::Info concrete subclass constructor. This string is a shortcut that
40 tells App::Info how to create an App::Info::Handler::Print object for handling
46 use App::Info::Handler;
47 use vars qw($VERSION @ISA);
49 @ISA = qw(App::Info::Handler);
52 App::Info::Handler->register_handler
53 ('prompt' => sub { __PACKAGE__->new('prompt') } );
61 my $prompter = App::Info::Handler::Prompt->new;
63 Constructs a new App::Info::Handler::Prompt object and returns it. No special
64 arguments are required.
70 my $self = $pkg->SUPER::new(@_);
71 $self->{tty} = -t STDIN && ( -t STDOUT || !( -f STDOUT || -c STDOUT ) );
77 my ($prompt, $tty, $def) = @_;
89 } else { # user hit ctrl-D
93 print "$def\n" if defined $def;
99 my ($self, $req) = @_;
101 my $type = $req->type;
102 if ($type eq 'unknown' || $type eq 'confirm') {
103 # We'll want to prompt for a new value.
104 my $val = $req->value;
105 my ($def, $dispdef) = defined $val ? ($val, " [$val] ") : ('', ' ');
106 my $msg = $req->message or Carp::croak("No message in request");
110 $ans = $get_ans->($msg, $self->{tty}, $def);
111 # Just return if they entered an empty string or we couldnt' get an
113 return 1 unless defined $ans && $ans ne '';
115 # Validate the answer.
116 my $err = $req->error;
117 while (!$req->value($ans)) {
118 print "$err: '$ans'\n";
119 $ans = $get_ans->($msg, $self->{tty}, $def);
120 return 1 unless defined $ans && $ans ne '';
123 } elsif ($type eq 'info') {
124 # Just print the message.
125 print STDOUT $req->message, "\n";
126 } elsif ($type eq 'error') {
127 # Just print the message.
128 print STDERR $req->message, "\n";
130 # This shouldn't happen.
131 Carp::croak("Invalid request type '$type'");
134 # Return true to indicate that we've handled the request.
143 Report all bugs via the CPAN Request Tracker at
144 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-Info>.
148 David Wheeler <L<david@wheeler.net|"david@wheeler.net">>
152 L<App::Info|App::Info> documents the event handling interface.
154 L<App::Info::Handler::Carp|App::Info::Handler::Carp> handles events by
155 passing their messages Carp module functions.
157 L<App::Info::Handler::Print|App::Info::Handler::Print> handles events by
158 printing their messages to a file handle.
160 L<App::Info::Handler|App::Info::Handler> describes how to implement custom
161 App::Info event handlers.
163 =head1 COPYRIGHT AND LICENSE
165 Copyright (c) 2002, David Wheeler. All Rights Reserved.
167 This module is free software; you can redistribute it and/or modify it under the
168 same terms as Perl itself.