File Coverage

lib/App/Info/Handler/Print.pm
Criterion Covered Total %
statement 20 20 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 5 5 100.0
pod 2 2 100.0
total 36 36 100.0


line stmt bran cond sub pod time code
1             package App::Info::Handler::Print;
2              
3             =head1 NAME
4              
5             App::Info::Handler::Print - Print App::Info event messages
6              
7             =head1 SYNOPSIS
8              
9             use App::Info::Category::FooApp;
10             use App::Info::Handler::Print;
11              
12             my $stdout = App::Info::Handler::Print->new( fh => 'stdout' );
13             my $app = App::Info::Category::FooApp->new( on_info => $stdout );
14              
15             # Or...
16             my $app = App::Info::Category::FooApp->new( on_error => 'stderr' );
17              
18             =head1 DESCRIPTION
19              
20             App::Info::Handler::Print objects handle App::Info events by printing their
21             messages to a filehandle. This means that if you want event messages to print
22             to a file or to a system filehandle, you can easily do it with this class.
23             You'll find, however, that App::Info::Handler::Print is most effective for
24             info and error events; unknown and prompt events are better handled by event
25             handlers that know how to prompt users for data. See
26             L for an example of
27             that functionality.
28              
29             Upon loading, App::Info::Handler::Print registers itself with
30             App::Info::Handler, setting up a couple of strings that can be passed to an
31             App::Info concrete subclass constructor. These strings are shortcuts that
32             tell App::Info how to create the proper App::Info::Handler::Print object
33             for handling events. The registered strings are:
34              
35             =over 4
36              
37             =item stdout
38              
39             Prints event messages to C.
40              
41             =item stderr
42              
43             Prints event messages to C.
44              
45             =back
46              
47             See the C constructor below for how to have App::Info::Handler::Print
48             print event messages to different filehandle.
49              
50             =cut
51              
52 1     1   914 use strict;
  1         3  
  1         31  
53 1     1   5 use App::Info::Handler;
  1         2  
  1         24  
54 1     1   6 use vars qw($VERSION @ISA);
  1         2  
  1         339  
55             $VERSION = '0.57';
56             @ISA = qw(App::Info::Handler);
57              
58             # Register ourselves.
59             for my $c (qw(stderr stdout)) {
60             App::Info::Handler->register_handler
61             ($c => sub { __PACKAGE__->new( fh => $c ) } );
62             }
63              
64             =head1 INTERFACE
65              
66             =head2 Constructor
67              
68             =head3 new
69              
70             my $stderr_handler = App::Info::Handler::Print->new;
71             $stderr_handler = App::Info::Handler::Print->new( fh => 'stderr' );
72             my $stdout_handler = App::Info::Handler::Print->new( fh => 'stdout' );
73             my $fh = FileHandle->new($file);
74             my $fh_handler = App::Info::Handler::Print->new( fh => $fh );
75              
76             Constructs a new App::Info::Handler::Print and returns it. It can take a
77             single parameterized argument, C, which can be any one of the following
78             values:
79              
80             =over 4
81              
82             =item stderr
83              
84             Constructs a App::Info::Handler::Print object that prints App::Info event
85             messages to C.
86              
87             =item stdout
88              
89             Constructs a App::Info::Handler::Print object that prints App::Info event
90             messages to C.
91              
92             =item FileHandle
93              
94             =item GLOB
95              
96             Pass in a reference and App::Info::Handler::Print will assume that it's a
97             filehandle reference that it can print to. Note that passing in something that
98             can't be printed to will trigger an exception when App::Info::Handler::Print
99             tries to print to it.
100              
101             =back
102              
103             If the C parameter is not passed, C will default to creating an
104             App::Info::Handler::Print object that prints App::Info event messages to
105             C.
106              
107             =cut
108              
109             sub new {
110 7     7 1 2975 my $pkg = shift;
111 7         38 my $self = $pkg->SUPER::new(@_);
112 7 100 100     69 if (!defined $self->{fh} || $self->{fh} eq 'stderr') {
    100          
    100          
113             # Create a reference to STDERR.
114 2         10 $self->{fh} = \*STDERR;
115             } elsif ($self->{fh} eq 'stdout') {
116             # Create a reference to STDOUT.
117 1         3 $self->{fh} = \*STDOUT;
118             } elsif (!ref $self->{fh}) {
119             # Assume a reference to a filehandle or else it's invalid.
120 1         221 Carp::croak("Invalid argument to new(): '$self->{fh}'");
121             }
122             # We're done!
123 6         32 return $self;
124             }
125              
126             ##############################################################################
127              
128             =head3 handler
129              
130             This method is called by App::Info to print out the message from events.
131              
132             =cut
133              
134             sub handler {
135 6     6 1 9 my ($self, $req) = @_;
136 6         10 print {$self->{fh}} $req->message, "\n";
  6         22  
137             # Return true to indicate that we've handled the request.
138 6         41 return 1;
139             }
140              
141             1;
142             __END__