File Coverage

blib/lib/Biblio/COUNTER/Processor.pm
Criterion Covered Total %
statement 9 32 28.1
branch 0 12 0.0
condition 0 3 0.0
subroutine 3 7 42.8
pod 3 3 100.0
total 15 57 26.3


line stmt bran cond sub pod time code
1             package Biblio::COUNTER::Processor;
2              
3 1     1   1298 use strict;
  1         2  
  1         75  
4 1     1   6 use warnings;
  1         2  
  1         29  
5              
6 1     1   5 use Biblio::COUNTER;
  1         1  
  1         513  
7              
8             # A class designed to be inherited from for OO-style event handling
9              
10             sub new {
11 0     0 1   my ($cls, %args) = @_;
12 0           my $self = bless {
13             'ignore' => {},
14             %args,
15             }, $cls;
16 0           return $self;
17             }
18              
19             sub ignore {
20 0     0 1   my ($self, @what) = @_;
21 0           $self->{'ignore'}->{$_} = 1 for @what;
22             }
23              
24             sub run {
25 0     0 1   my ($self, $report) = @_;
26 0           my $fh;
27 0           $self->{'file'} = $report;
28 0 0         if (ref($report) eq '') {
    0          
29             # Assume $report is a file name
30 0 0         if ($report eq '-') {
31 0           $fh = \*STDIN;
32             }
33             else {
34 0 0         open $fh, '<', $report
35             or die "Can't open report '$report': $!";
36             }
37 0           $report = Biblio::COUNTER->report($fh);
38             }
39             elsif ($report->isa('Biblio::COUNTER::Report')) {
40             # Nothing special to do
41             }
42             else {
43             # Assume it's a filehandle
44 0           $report = Biblio::COUNTER->report($report);
45             }
46 0           $report->{'file'} = $self->{'file'};
47 0           my $ignore = $self->{'ignore'};
48             $report->{'callback'} = {
49             '*' => sub {
50 0     0     my ($report, $callback_name, @args) = @_;
51 0 0 0       if (!$ignore->{$callback_name}
52             && defined(my $code = $self->can($callback_name))) {
53 0           $code->($self, $report, @args);
54             }
55             },
56 0           };
57 0           $report->process;
58 0 0         close $fh if defined $fh;
59 0           return $report;
60             }
61              
62              
63             1;
64              
65             =pod
66              
67             =head1 NAME
68              
69             Biblio::COUNTER::Processor - superclass for Biblio::COUNTER processors
70              
71             =head1 SYNOPSIS
72              
73             # Use
74             use Biblio::COUNTER::Processor;
75             $processor = Biblio::COUNTER::Processor->new;
76             $processor->ignore(@events);
77             $report = $processor->run;
78              
79             # Subclass
80             use MyProcessor;
81             @MyProcessor::ISA = qw(Biblio::COUNTER::Processor);
82             # Event handlers
83             sub begin_report {
84             my ($self, $report) = @_;
85             # etc.
86             }
87             sub count {
88             my ($self, $report, $scope, $field, $period, $val) = @_;
89             # etc.
90             }
91             # etc.
92            
93             =head1 DESCRIPTION
94              
95             B is an inheritable class that provides an
96             intermediate interface to L. When used on
97             its own, it does nothing; subclasses must define handlers for the various
98             events (e.g., C or C) that are triggered as a report
99             is processed.
100              
101             Event-handling methods (if defined) are invoked with the arguments
102             documented in L, except that an additional argument (the
103             instance of the L
104             subclass) is prepended.
105              
106             For a class that actually B something when events are triggered, see
107             L.
108              
109             =head1 METHODS
110              
111             =over 4
112              
113             =item B
114              
115             $processor = Biblio::COUNTER::Processor->new;
116              
117             Create a new processor.
118              
119             =item B(I<$file>)
120              
121             $report = $processor->run($what);
122              
123             Process the given report.
124              
125             I<$what> may be a file handle, the name of a file, or an instance of
126             (a subclass of) L.
127              
128             A L object will be
129             instantiated and its B method invoked; each time an event is
130             triggered, a method with the event's name (e.g., C or
131             C) is invoked B. Since this class doesn't
132             define any such methods, the default behavior when an event is triggered is
133             to do nothing.
134              
135             =item B(I<@events>)
136              
137             $processor->ignore(qw/line input output/);
138              
139             Specify the events to ignore. The various events are documented in
140             L.
141              
142             =back
143              
144             =head1 INHERITANCE
145              
146             B is designed to be inheritable; in fact, it
147             doesn't do anything on its own (unless you count I as
148             doing something).
149              
150             =head1 BUGS
151              
152             There are no known bugs. Please report bugs to the author via e-mail
153             (see below).
154              
155             =head1 AUTHOR
156              
157             Paul Hoffman (nkuitse AT cpan DOT org)
158              
159             =head1 COPYRIGHT
160              
161             Copyright 2008 Paul M. Hoffman.
162              
163             This is free software, and is made available under the same terms as Perl
164             itself.
165              
166             =head1 SEE ALSO
167              
168             L
169              
170             L
171              
172             L
173