blib/lib/Text/Filter.pm

filter: Statementstmt 74.5% 129 / 173 Branchbran 65.0% 69 / 106 Conditioncond 41.6% 10 / 24 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 72.0% 31 / 43 Podpod 29.1% 7 / 24 totaltotal 64.2% 248 / 386 CC 63 SCAR 23.3
CC
63
Cov
69%
set_output
39.8
set_input
11.7
set_input_postread
9.9
CRAP 10.3
↑ Summary blib/lib/Text/Filter/Cooked.pm »
1 #! perl
2  
3 package Text::Filter;
4  
5 # Author : Johan Vromans
6 # Last Modified By: Johan Vromans
7 # Last Modified On: Thu Jan 17 13:26:07 2013
8 # Update Count : 169
9 # Status : Released
10  
11 =head1 NAME
12  
13 Text::Filter - base class for objects that can read and write text lines
14  
15 =head1 SYNOPSIS
16  
17 A plethora of tools exist that operate as filters: they get data from
18 a source, operate on this data, and write possibly modified data to a
19 destination. In the Unix world, these tools can be chained using a
20 technique called pipelining, where the output of one filter is
21 connected to the input of another filter. Some non-Unix worlds are
22 reported to have similar provisions.
23  
24 To create Perl modules for filter functionality seems trivial at
25 first. Just open the input file, read and process it, and write output
26 to a destination file. But for really reusable modules this approach
27 is too simple. A reusable module should not read and write files
28 itself, but rely on the calling program to provide input as well as to
29 handle the output.
30  
31 C<Text::Filter> is a base class for modules that have in common
32 that they process text lines by reading from some source (usually a
33 file), manipulating the contents and writing something back to some
34 destination (usually some other file).
35  
36 This module can be used on itself, but it is most powerfull when used
37 to derive modules from it. See section EXAMPLES for an extensive
38 example.
39  
40 =head1 DESCRIPTION
41  
42 The main purpose of the C<Text::Filter> class is to abstract out the
43 details out how input and output must be done. Although in most cases
44 input will come from a file, and output will be written to a file,
45 advanced modules require more detailed control over the input and
46 output. For example, the module could be called from another module,
47 in this case the callee could be allowed to process only a part of the
48 input. Or, a program could have prepared data in an array and wants to
49 call the module to process this data as if it were read from a file.
50 Also, the input stream provides a pushback functionality to make
51 peeking at the input easy.
52  
53 C<Text::Filter> can be used on its own as a convenient input/output
54 handler. For example:
55  
56 use Text::Filter;
57 my $filter = Text::Filter->(input => *STDIN, output => *STDOUT);
58 my $line;
59 while ( defined($line = $filter->readline) ) {
60 $filter->writeline($line);
61 }
62  
63 Or, even simpler:
64  
65 use Text::Filter;
66 Text::Filter->run(input => *STDIN, output => *STDOUT);
67  
68 Its real power shows when such a program is turned into a module for
69 optimal reuse.
70  
71 When creating a module that is to process lines of text, it can be
72 derived from C<Text::Filter>, for example:
73  
74 package MyFilter;
75 use base 'Text::Filter';
76  
77 The constructor method must then call the new() method of the
78 C<Text::Filter> class to set up the base class. This is conveniently
79 done by calling SUPER::new(). A hash containing attributes must be
80 passed to this method, some of these attributes will be used by the
81 base class setup.
82  
83 sub new {
84 my $class = shift;
85 # ... fetch non-attribute arguments from @_ ...
86 # Create the instance, using the attribute arguments.
87 my $self = $class->SUPER::new(@_);
88  
89 Finally, the newly created object must be re-blessed into the desired
90 class, and returned:
91  
92 # Rebless into the desired class.
93 bless($self, $class);
94 }
95  
96 When creating new instances for this class, attributes C<input> and
97 C<output> can be used to specify how input and output is to be
98 handled. Several possible values can be supplied for these attributes.
99  
100 For C<input>:
101  
102 =over
103  
104 =item *
105  
106 A scalar, containing a file name.
107 The named file will be opened,
108 input lines will be read using C<<>>.
109  
110 =item *
111  
112 A file handle (glob).
113 Lines will be read using C<<>>.
114  
115 =item *
116  
117 An instance of class C<IO::File>.
118 Lines will be read using C<<>>.
119  
120 =item *
121  
122 A reference to an array.
123 Input lines will be shift()ed from the array.
124  
125 =item *
126  
127 A reference to a scalar.
128 Input lines will be taken from the contents of the scalar (which will
129 be modified). When exhausted, it will be set to undefined.
130  
131 =item *
132  
133 A reference to an anonymous subroutine.
134 This routine will be called to get the next line of data.
135  
136 =back
137  
138 The default is to read input using de C<< <> >> operator.
139  
140 For C<output>:
141  
142 =over
143  
144 =item *
145  
146 A scalar, containing a file name.
147 The named file will be created automatically,
148 output lines will be written using print().
149  
150 =item *
151  
152 A file handle (glob).
153 Lines will be written using print().
154  
155 =item *
156  
157 An instance of class C<IO::File>.
158 Lines will be written using print().
159  
160 =item *
161  
162 A reference to an array.
163 Output lines will be push()ed into the array.
164 The array will be initialised to C<()> if necessary.
165  
166 =item *
167  
168 A reference to a scalar.
169 Output lines will be appended to the scalar.
170 The scalar will be initialised to C<""> if necessary.
171  
172 =item *
173  
174 A reference to an anonymous subroutine.
175 This routine will be called to append a line of text to the destination.
176  
177 =back
178  
179 The default is to write output to STDOUT.
180  
181 Additional attributes can be used to specify actions to be performed
182 after the data is fetched, or prior to being written. For example, to
183 strip line endings upon input, and add them upon output.
184  
185 =head1 CONSTRUCTOR
186  
187 The constructor is called new() and takes a hash with attributes as
188 its parameter.
189  
190 The following attributes are recognized and used by the constructor,
191 all others are ignored.
192  
193 The constructor will return a blessed hash containing all the original
194 attributes, plus some new attributes. The names of the new attributes
195 all start with C<_filter_>, the new attributes should I<not> be touched.
196  
197 =over 4
198  
199 =item input
200  
201 This designates the input source. The value must be a scalar
202 (containing a file name), a file handle (either a glob or an instance
203 of class C<IO::File>), an array reference, or a reference to a
204 subroutine, as described above.
205  
206 If a subroutine is specified, it must return the next line to be
207 processed, and C<undef> at end.
208  
209 =item input_postread
210  
211 This attribute can be used to select an action to be performed after
212 the data has been read.
213 Its prime purpose is to handle line endings (e.g. remove a trailing newline).
214  
215 The value can be 'none' or 0 (no action), 'chomp' or 1 (standard
216 chomp() operation), or a reference to a subroutine. Default value is 0
217 (no chomping).
218  
219 If the value is a reference to a subroutine, this will be called with
220 the text line that was just read as its only argument, and it must
221 return the new contents of the text line..
222 If it returns undef, this line will be skipped.
223  
224 =item filter
225  
226 If specified, a reference to a subroutine that performs filtering. It
227 will be called after input_postread, with the text line that was just
228 read as its only argument, and it must return the new contents of the
229 text line.
230 If it returns undef, this line will be skipped.
231  
232 =item output
233  
234 This designates the output. The value must be a scalar
235 (containing a file name), a file handle (either a glob or an instance
236 of class C<IO::File>), or a reference to a subroutine, as described
237 above.
238  
239 Note: when a file name is passed, a C<< > >> will be prepended if necessary.
240  
241 =item output_prewrite
242  
243 This attribute can be used to select an action to be performed just
244 before the data is added to the output.
245 Its prime purpose is to handle line endings (e.g. add a trailing newline).
246 The value can be 'none' or 0 (no action) , 'newline' or 1 (append the
247 value of C<$/> to the line), or a reference to a subroutine. Default
248 value is 0 (no action).
249  
250 If the value is 'newline' or 1, and the value of C<$/> is C<"">
251 (paragraph mode), two newlines will be added.
252  
253 If the value is a reference to a subroutine, this will be called with
254 the text line as its only argument, and it must return the new
255 contents of the line to be output.
256 If it returns undef, no output occurs.
257  
258 =back
259  
260 =head1 CLASS METHODS
261  
262 =over 4
263  
264 =item Text::Filter->run([ I<attributes> ])
265  
266 This creates a temporary filter object using the attibutes as in
267 C<new>, and runs its C<run> method.
268  
269 =back
270  
271 =head1 INSTANCE METHODS
272  
273 =over 4
274  
275 =item $filter->readline
276  
277 If there is anything in the pushback buffer, this is returned and the
278 pushback buffer is marked empty.
279  
280 Otherwise, returns the next line from the input stream, or C<undef> if
281 there is no more input.
282  
283 =item $filter->pushback($line)
284  
285 Pushes a line of text back to the input stream.
286 Returns the line.
287  
288 =item $filter->peek
289  
290 Peeks at the input.
291 Short for pushback(readline()).
292  
293 =item $filter->writeline ($line)
294  
295 Adds C<$line> to the output stream.
296  
297 =item $filter->set_input($input [ , $postread ])
298  
299 Sets the input method to C<$input>.
300 If the optional argument C<$postread> is defined, sets the input line
301 postprocessing strategy as well.
302  
303 =item $filter->set_output($output, [ $prewrite ])
304  
305 Sets the output method to C<$output>.
306 If the optional argument C<$prewrite> is defined, sets the output line
307 preprocessing strategy as well.
308  
309 =item $filter->run( [ I<filter> ])
310  
311 This will run the readline/writeline loop. Optionally a filter
312 argument (see CONSTRUCTOR, above) can be passed if filtering is
313 desired and not yet otherwise designated.
314  
315 =back
316  
317 =head1 EXAMPLE
318  
319 This example shows how to filter empty and whitespace lines.
320  
321 use Text::Filter;
322 Text::Filter->run(filter => sub { my $line = shift;
323 return unless $line =~ /\S/;
324 return $line;
325 });
326  
327 This is an example of how to use C<Text::Filter> as a base class.
328  
329 It implements a module that provides a single instance method: grep(),
330 that performs some kind of grep(1)-style function (how surprising!).
331  
332 A class method grepper() is also provided for easy access to do 'the
333 right thing' in the most common case.
334  
335 package Grepper;
336  
337 use strict;
338 use base qw(Exporter Text::Filter);
339 our @EXPORT;
340  
341 # Setup.
342 BEGIN {
343 @EXPORT = qw(grepper);
344 }
345  
346 # Constructor. Major part of the job is done by the superclass.
347 sub new {
348 my $class = shift;
349  
350 # Create a new instance by calling the superclass constructor.
351 my $self = $class->SUPER::new(@_);
352 # The superclass constructor will take care of handling
353 # the input and output attributes, and setup everything for
354 # handling the IO.
355  
356 # Bless the object into the desired class.
357 bless ($self, $class);
358  
359 # And return it.
360 $self;
361 }
362  
363 # Instance method, just an example. No magic.
364 sub grep {
365 my $self = shift;
366 my $pat = shift;
367 my $line;
368 while ( defined($line = $self->readline) ) {
369 $self->writeline($line) if $line =~ $pat;
370 }
371 }
372  
373 # Class method, for convenience.
374 # Usage: grepper (<input file>, <output file>, <pattern>);
375 sub grepper {
376 my ($input, $output, $pat) = @_;
377  
378 # Create a Grepper object.
379 my $grepper = Grepper->new(input => $input, output => $output);
380  
381 # Call its grep method.
382 $grepper->grep ($pat);
383 }
384  
385 =head1 AUTHOR AND CREDITS
386  
387 Johan Vromans (jvromans@squirrel.nl) wrote this module.
388  
389 =head1 COPYRIGHT AND DISCLAIMER
390  
391 This program is Copyright 1998,2013 by Squirrel Consultancy. All
392 rights reserved.
393  
394 This program is free software; you can redistribute it and/or modify
395 it under the terms of either: a) the GNU General Public License as
396 published by the Free Software Foundation; either version 1, or (at
397 your option) any later version, or b) the "Artistic License" which
398 comes with Perl.
399  
400 This program is distributed in the hope that it will be useful, but
401 WITHOUT ANY WARRANTY; without even the implied warranty of
402 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
403 GNU General Public License or the Artistic License for more details.
404  
405 =cut
406  
407 3 ▶use 5.8.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
408 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
409 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
410 3 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
411  
412 3 our $VERSION = "1.10";
413  
414 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
415 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
416  
417 ################ Attribute Controls ################
418  
419 my %_attributes =
420 0 ▶ ( input => sub { scalar <> },
Subroutine __ANON__: not called CC=1 SCAR=6.9
421 3 output => *STDOUT,
422 input_postread => 0,
423 output_prewrite => 0,
424 filter => undef,
425 );
426  
427 15 ▶sub _standard_atts {
Subroutine _standard_atts: called CC=1 SCAR=0.0
428 15 my $self = shift;
429 15 return keys %_attributes;
430 # In derived class:
431 # return ($self->SUPER::_standard_atts, keys %_attributes);
432 }
433  
434 36 ▶sub _attr_default {
Subroutine _attr_default: called CC=2 SCAR=8.1
435 36 my ($self, $attr) = @_;
436 36 ▶ return $_attributes{$attr} if exists $_attributes{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_attributes{$attr} 36 0 36
437 0 return;
438 # In derived class:
439 # return $_attributes{$attr} if exists $_attributes{$attr};
440 # return $self->SUPER::_attr_default;
441 }
442  
443 ################ Constructor ################
444  
445 15 ▶sub new {
Subroutine new: called CC=6 SCAR=18.1
Pod undocumented
446 15 my ($class, %atts) = @_;
447 15 ▶ croak("Constructor 'new' cannot be called as an object method")
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 15 15
448 if ref $class;
449  
450 # Create the object out of the attributes.
451 # API: All original attributes are published in the hash.
452 # (Unfortunately)
453 15 my $self = bless { %atts }, $class;
454  
455 # Fill in initial attribute values.
456 15 foreach my $attr ( $self->_standard_atts ) {
457 99 ▶ my $op = $attr =~ /^_/ ? "_set$attr" : "set_$attr";
Branch: 1 branch
BranchTrueFalseTotal
$attr =~ /^_/ ? : 6 93 99
458 99 my $value;
459  
460 99 ▶ if ( exists $atts{$attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $atts{$attr}) { } 39 60 99
461 39 $value = delete $atts{$attr};
462 }
463 else {
464 60 $value = $self->_attr_default($attr);
465 }
466 99 $self->$op($value);
467 }
468  
469 15 ▶ if ( %atts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%atts) 0 15 15
470 0 warnings::warnif("Unprocessed $class attributes: " .
471 join(" ", sort keys %atts));
472 }
473  
474 # Customization.
475 15 $self->init;
476  
477 # Return the object.
478 15 return $self;
479 }
480  
481 # Stub for customization. Subclasses can override this.
482 15 ▶sub init { }
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
483  
484 ################ Attributes ################
485  
486 15 ▶sub set_input {
Subroutine set_input: called CC=11 SCAR=24.6
Pod documented
487 15 my ($self, $handler, $postread) = @_;
488 15 my $input;
489  
490 15 ▶ if ( ref($handler) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $handler) { } 12 3 15
elsif ($handler =~ /^\*/) { } 1 2 3
491 12 ▶ if ( $handler =~ /=/ && $handler->isa("IO::File") ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($handler =~ /=/ and $handler->isa('IO::File')) { } 0 12 12
elsif (ref $handler eq 'CODE') { } 2 10 12
elsif (ref $handler eq 'SCALAR') { } 8 2 10
elsif (ref $handler eq 'ARRAY') { } 2 0 2
Condition33%1/3
$handler =~ /=/ and $handler->isa("IO::File")
!ll&&!rl&&r
1200
MC/DC0%0/2
$handler =~ /=/ and $handler->isa("IO::File")
$handler =~ /=/ $handler->isa("IO::File")
Truth table33%1/3
A and B
A= $handler =~ /=/B= $handler->isa("IO::File")
ABresult
0X0
100
111
492 0 ▶ $input = sub { scalar <$handler> };
Subroutine __ANON__: not called CC=1 SCAR=6.9
493 0 $self->{_filter_input_fd} = $handler;
494 }
495 elsif ( ref($handler) eq 'CODE' ) {
496 2 $input = $handler;
497 }
498 elsif ( ref($handler) eq 'SCALAR' ) {
499 60 ▶ $input = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
500 60 ▶ return unless defined $$handler;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$handler 6 54 60
501 54 ▶ if ( (my $i = index($$handler, "\n")) >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $i = index($$handler, "\n")) >= 0) { } 46 8 54
502 46 return substr($$handler, 0, $i+1, "");
503 }
504 else {
505 8 my $res = $$handler;
506 8 $$handler = undef;
507 8 return $res;
508 }
509 }
510 8 }
511 elsif ( ref($handler) eq 'ARRAY' ) {
512 2 ▶ $input = sub { shift (@$handler) };
Subroutine __ANON__: called CC=1 SCAR=0.0
513 2 $self->{_filter_input_fd} = $handler;
514 }
515 }
516 elsif ( $handler =~ /^\*/ ) {
517 1 ▶ $input = sub { scalar <$handler> };
Subroutine __ANON__: called CC=1 SCAR=0.0
518 }
519 else {
520 2 my $fd;
521 2 ▶ $fd = IO::File->new($handler)
Branch: 1 branch
BranchTrueFalseTotal
$fd = "IO::File"->new($handler) or croak("Error opening ${handler}: $!") 2 0 2
522 or croak("Error opening $handler: $!");
523 2 ▶ $input = sub { scalar <$fd> };
Subroutine __ANON__: called CC=1 SCAR=0.0
524 2 $self->{_filter_input_fd} = $fd;
525 }
526  
527 15 ▶ croak("Unrecognized value for 'input' attribute: ".
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 15 15
528 $handler) unless defined $input;
529  
530 15 $self->{_filter_input} = $input;
531  
532 15 ▶ $self->set_input_postread($postread)
Branch: 1 branch
BranchTrueFalseTotal
if defined $postread 0 15 15
533 if defined ($postread);
534  
535 15 $self->{_filter_pushback} = [];
536  
537 15 return $self;
538 }
539  
540 15 ▶sub set_output {
Subroutine set_output: called CC=14 SCAR=36.8
Pod documented
541 15 my ($self, $handler, $prewrite) = @_;
542 15 my $output;
543  
544 15 ▶ if ( ref($handler) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $handler) { } 15 0 15
elsif ($handler =~ /^\*/) { } 0 0 0
545 15 ▶ if ( $handler =~ /=/ && $handler->isa("IO::File") ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($handler =~ /=/ and $handler->isa('IO::File')) { } 0 15 15
elsif (ref $handler eq 'ARRAY') { } 3 12 15
elsif (ref $handler eq 'SCALAR') { } 8 4 12
elsif (ref $handler eq 'CODE') { } 4 0 4
Condition33%1/3
$handler =~ /=/ and $handler->isa("IO::File")
!ll&&!rl&&r
1500
MC/DC0%0/2
$handler =~ /=/ and $handler->isa("IO::File")
$handler =~ /=/ $handler->isa("IO::File")
Truth table33%1/3
A and B
A= $handler =~ /=/B= $handler->isa("IO::File")
ABresult
0X0
100
111
546 0 ▶ $output = sub { print { $handler } (shift) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
547 0 $self->{_filter_output_fd} = $handler;
548 }
549 elsif ( ref($handler) eq 'ARRAY' ) {
550 3 ▶ $output = sub { push (@$handler, shift) };
Subroutine __ANON__: called CC=1 SCAR=0.0
551 3 ▶ @$handler = () unless @$handler;
Branch: 1 branch
BranchTrueFalseTotal
unless @$handler 3 0 3
552 }
553 elsif ( ref($handler) eq 'SCALAR' ) {
554 8 ▶ $output = sub { $$handler .= shift };
Subroutine __ANON__: called CC=1 SCAR=0.0
555 8 ▶ $$handler = "" unless defined $$handler;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$handler 1 7 8
556 }
557 elsif ( ref($handler) eq 'CODE' ) {
558 4 $output = $handler;
559 }
560 }
561 elsif ( $handler =~ /^\*/ ) {
562 0 ▶ $output = sub { print { $handler } (shift) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
563 0 $self->{_filter_output_fd} = $handler;
564 }
565 else {
566 0 ▶ $handler = ">" . $handler unless $handler =~ /^>/;
Branch: 1 branch
BranchTrueFalseTotal
unless $handler =~ /^>/ 0 0 0
567 0 my $fd;
568 0 ▶ $fd = IO::File->new($handler)
Branch: 1 branch
BranchTrueFalseTotal
$fd = "IO::File"->new($handler) or croak("Error opening ${handler}: $!") 0 0 0
569 or croak("Error opening $handler: $!");
570 0 ▶ $output = sub { print { $fd } (shift) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
571 0 $self->{_filter_output_fd} = $fd;
572 }
573  
574 15 ▶ croak("Unrecognized value for 'output' attribute: " . $handler)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output 0 15 15
575 unless defined $output;
576  
577 15 $self->{_filter_output} = $output;
578  
579 15 ▶ $self->set_output_prewrite($prewrite)
Branch: 1 branch
BranchTrueFalseTotal
if defined $prewrite 0 15 15
580 if defined $prewrite;
581  
582 15 return $self;
583 }
584  
585 15 ▶sub set_input_postread {
Subroutine set_input_postread: called CC=7 SCAR=23.0
Pod undocumented
586 15 my ($self, $postread) = @_;
587  
588 15 my $posthandler;
589 15 ▶ if ( ref($postread) && ref($postread) eq 'CODE' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $postread and ref $postread eq 'CODE') { } 0 15 15
elsif ($postread eq 'none' or $postread eq '0') { } 11 4 15
elsif ($postread eq 'chomp' or $postread eq '1') { } 4 0 4
Condition33%1/3
ref $postread and ref $postread eq "CODE"
!ll&&!rl&&r
1500
MC/DC0%0/2
ref $postread and ref $postread eq "CODE"
ref $postread ref $postread eq "CODE"
Truth table33%1/3
A and B
A= ref $postreadB= ref $postread eq "CODE"
ABresult
0X0
100
111
590 0 $posthandler = $postread;
591 }
592 ▶ elsif ( $postread eq 'none' || $postread eq '0' ) {
Condition66%2/3
$postread eq "none" or $postread eq 0
l!l&&r!l&&!r
0114
MC/DC50%1/2
$postread eq "none" or $postread eq 0
$postread eq "none" $postread eq 0
Truth table66%2/3
A or B
A= $postread eq "none"B= $postread eq 0
ABresult
1X1
011
000
593 }
594 ▶ elsif ( $postread eq 'chomp' || $postread eq '1' ) {
Condition33%1/3
$postread eq "chomp" or $postread eq 1
l!l&&r!l&&!r
400
MC/DC0%0/2
$postread eq "chomp" or $postread eq 1
$postread eq "chomp" $postread eq 1
Truth table33%1/3
A or B
A= $postread eq "chomp"B= $postread eq 1
ABresult
1X1
011
000
595 4 $posthandler = '';
596 }
597 else {
598 0 croak("Unrecognized value for 'input_postread' attribute: ".
599 $postread);
600 }
601  
602 15 $self->{_filter_postread} = $posthandler;
603  
604 15 return;
605 }
606  
607 15 ▶sub set_output_prewrite {
Subroutine set_output_prewrite: called CC=7 SCAR=23.0
Pod undocumented
608 15 my ($self, $prewrite) = @_;
609  
610 15 my $prehandler;
611 15 ▶ if ( ref($prewrite) && ref($prewrite) eq 'CODE' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $prewrite and ref $prewrite eq 'CODE') { } 0 15 15
elsif ($prewrite eq 'none' or $prewrite eq '0') { } 11 4 15
elsif ($prewrite eq 'newline' or $prewrite eq '1') { } 4 0 4
Condition33%1/3
ref $prewrite and ref $prewrite eq "CODE"
!ll&&!rl&&r
1500
MC/DC0%0/2
ref $prewrite and ref $prewrite eq "CODE"
ref $prewrite ref $prewrite eq "CODE"
Truth table33%1/3
A and B
A= ref $prewriteB= ref $prewrite eq "CODE"
ABresult
0X0
100
111
612 0 $prehandler = $prewrite;
613 }
614 ▶ elsif ( $prewrite eq 'none' || $prewrite eq '0' ) {
Condition66%2/3
$prewrite eq "none" or $prewrite eq 0
l!l&&r!l&&!r
0114
MC/DC50%1/2
$prewrite eq "none" or $prewrite eq 0
$prewrite eq "none" $prewrite eq 0
Truth table66%2/3
A or B
A= $prewrite eq "none"B= $prewrite eq 0
ABresult
1X1
011
000
615 }
616 ▶ elsif ( $prewrite eq 'newline' || $prewrite eq '1' ) {
Condition33%1/3
$prewrite eq "newline" or $prewrite eq 1
l!l&&r!l&&!r
400
MC/DC0%0/2
$prewrite eq "newline" or $prewrite eq 1
$prewrite eq "newline" $prewrite eq 1
Truth table33%1/3
A or B
A= $prewrite eq "newline"B= $prewrite eq 1
ABresult
1X1
011
000
617 4 $prehandler = '';
618 }
619 else {
620 0 croak("Unrecognized value for 'output_prewrite' attribute: ".
621 $prewrite);
622 }
623  
624 15 $self->{_filter_prewrite} = $prehandler;
625  
626 15 return;
627 }
628  
629 16 ▶sub set_filter_filter {
Subroutine set_filter_filter: called CC=3 SCAR=11.0
Pod undocumented
630 16 my ($self, $filter) = @_;
631 16 ▶ return unless defined($filter);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filter 14 2 16
632 2 ▶ croak("filter must be a subroutine (CODE ref)")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $filter eq "CODE" 0 2 2
633 unless ref($filter) eq "CODE";
634 2 $self->{_filter_filter} = $filter;
635 }
636  
637 81 ▶sub get_filter_filter {
Subroutine get_filter_filter: called CC=1 SCAR=0.0
Pod undocumented
638 81 return shift->{_filter_filter};
639 }
640  
641 94 ▶sub get_filter_pushback {
Subroutine get_filter_pushback: called CC=1 SCAR=0.0
Pod undocumented
642 94 return shift->{_filter_pushback};
643 }
644  
645 94 ▶sub get_filter_input {
Subroutine get_filter_input: called CC=1 SCAR=0.0
Pod undocumented
646 94 return shift->{_filter_input};
647 }
648  
649 81 ▶sub get_filter_postread {
Subroutine get_filter_postread: called CC=1 SCAR=0.0
Pod undocumented
650 81 return shift->{_filter_postread};
651 }
652  
653 47 ▶sub get_filter_output {
Subroutine get_filter_output: called CC=1 SCAR=0.0
Pod undocumented
654 47 return shift->{_filter_output};
655 }
656  
657 47 ▶sub get_filter_prewrite {
Subroutine get_filter_prewrite: called CC=1 SCAR=0.0
Pod undocumented
658 47 return shift->{_filter_prewrite};
659 }
660  
661 0 ▶sub get_filter_input_fd {
Subroutine get_filter_input_fd: not called CC=1 SCAR=6.9
Pod undocumented
662 0 return shift->{_filter_input_fd};
663 }
664  
665 0 ▶sub get_filter_output_fd {
Subroutine get_filter_output_fd: not called CC=1 SCAR=6.9
Pod undocumented
666 0 return shift->{_filter_output_fd};
667 }
668  
669 ################ Methods ################
670  
671 90 ▶sub readline {
Subroutine readline: called CC=8 SCAR=21.5
Pod documented
672 90 my ($self) = shift;
673  
674 90 while ( 1 ) {
675  
676 0 return shift(@{$self->get_filter_pushback})
677 94 ▶ if @{$self->get_filter_pushback} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->get_filter_pushback;} > 0 0 94 94
678  
679 94 my $line;
680 94 my $input = $self->get_filter_input;
681 94 ▶ return undef unless defined($line = $input->());
Branch: 1 branch
BranchTrueFalseTotal
unless defined($line = &$input()) 13 81 94
682  
683 81 ▶ if ( defined(my $postread = $self->get_filter_postread) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $postread = $self->get_filter_postread)) 33 48 81
684 33 ▶ if ( $postread ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($postread ne '') { } 0 33 33
685 0 ▶ next unless defined($line = $postread->($line));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($line = &$postread($line)) 0 0 0
686 }
687 else {
688 33 chomp $line;
689 }
690 }
691  
692 81 ▶ if ( defined(my $filter = $self->get_filter_filter) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $filter = $self->get_filter_filter)) 10 71 81
693 10 ▶ next unless defined($line = $filter->($line));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($line = &$filter($line)) 4 6 10
694 }
695  
696 77 return $line;
697 }
698 }
699  
700 0 ▶sub pushback {
Subroutine pushback: not called CC=1 SCAR=6.9
Pod documented
701 0 my ($self, $line) = @_;
702 0 push (@{$self->get_filter_pushback}, $line);
703  
704 0 return $line;
705 }
706  
707 0 ▶sub peek {
Subroutine peek: not called CC=2 SCAR=17.9
Pod documented
708 0 my ($self) = @_;
709 return $self->get_filter_pushback->[0]
710 0 ▶ if @{$self->get_filter_pushback} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->get_filter_pushback;} > 0 0 0 0
711  
712 0 return $self->pushback($self->readline);
713 }
714  
715 0 ▶sub get_input {
Subroutine get_input: not called CC=1 SCAR=6.9
Pod undocumented
716 0 my $self = shift;
717  
718 0 return $self->get_filter_input;
719 }
720  
721 47 ▶sub writeline {
Subroutine writeline: called CC=6 SCAR=20.9
Pod documented
722 47 my ($self, $line) = @_;
723 47 my $prewrite = $self->get_filter_prewrite;
724 47 ▶ if ( defined $prewrite ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prewrite) 12 35 47
725 12 ▶ if ( $prewrite ne '' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prewrite ne '') { } 0 12 12
elsif (defined $/) { } 12 0 12
726 0 $line = $prewrite->($line);
727 0 ▶ return unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
728 }
729 elsif ( defined $/ ) {
730 # Add the line terminator.
731 # In paragraph mode, just add two newlines.
732 12 ▶ $line .= ($/ eq '' ? "\n\n" : $/);
Branch: 1 branch
BranchTrueFalseTotal
$/ eq '' ? : 0 12 12
733 }
734 }
735 47 return $self->get_filter_output->($line);
736 }
737  
738 0 ▶sub get_output {
Subroutine get_output: not called CC=1 SCAR=6.9
Pod undocumented
739 0 my $self = shift;
740  
741 0 return $self->get_filter_output;
742 }
743  
744 6 ▶sub run {
Subroutine run: called CC=5 SCAR=16.1
Pod documented
745 6 my $self = shift;
746  
747 6 ▶ if ( ref($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 4 2 6
748 4 ▶ croak("Usage: \$f->run( [ filterproc ] )") if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 4 4
749 4 ▶ $self->set_filter(shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 3 4
750 }
751 else {
752 2 $self = $self->new(@_);
753 }
754  
755 6 my $line;
756 6 ▶ while ( $line = $self->readline ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line = $self->readline) 20 6 26
757 20 $self->writeline($line);
758 }
759 }
760  
761 16 ▶sub set_filter {
Subroutine set_filter: called CC=1 SCAR=0.0
Pod undocumented
762 16 $_[0]->set_filter_filter($_[1]);
763 }
764  
765 0 ▶sub get_filter {
Subroutine get_filter: not called CC=1 SCAR=6.9
Pod undocumented
766 0 return $_[0]->get_filter_filter($_[1]);
767 }
768  
769 3 1;
↑ Summary blib/lib/Text/Filter/Cooked.pm »