| 1 | package Text::Filter::Chain; # chains multiple filters and runs them sequentially | ||||||||||||||||||||||||||||||||||
| 2 | # $Id: Chain.pm,v 1.5 2000/12/05 15:53:52 verhaege Exp $ | ||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use fields qw(filters input output); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 1 | $VERSION = '0.02'; | |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=23.5
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 9 | 1 | my $proto = shift; | |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | my $pkg = ref($proto) || $proto; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | 1 | my $this = bless [\%{"${pkg}::FIELDS"}], $pkg; | |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | 1 | my %arg = @_; | |||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | while(my ($k,$v) = each %arg) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 17 | 0 | $this->{$k} = $v; | |||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | $this->{filters} ||= []; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$this->{"filters"} ||= []
MC/DC0%0/1
$this->{"filters"} ||= []
$this->{"filters"}
Truth table0%0/2
A
A= $this->{"filters"}
| |||||||||||||||||||||||||||||||||||
| 21 | 0 | foreach(@{$this->{filters}}) { | |||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | $this->is_valid_filter($_) or die "Invalid filter"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | 0 | $this; | |||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub add_filter { | ||||||||||||||||||||||||||||||||
|
Subroutine
add_filter: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 29 | 0 | my $this = shift; | |||||||||||||||||||||||||||||||||
| 30 | 0 | foreach(@_) { | |||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | $this->is_valid_filter($_) or die "Invalid filter"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 32 | 0 | push @{$this->{filters}}, $_; | |||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub is_valid_filter { | ||||||||||||||||||||||||||||||||
|
Subroutine
is_valid_filter: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 37 | 0 | my $this = shift; | |||||||||||||||||||||||||||||||||
| 38 | 0 | my $filter = shift; | |||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||
| 40 | # filter must have methods set_input(), set_output() and run() | ||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | return $filter->can('set_input') && $filter->can('set_output') && $filter->can('run'); | ||||||||||||||||||||||||||||||||
|
Condition0%0/6
$filter->can('set_input') && $filter->can('set_output') && $filter->can('run')
$filter->can("set_input") and $filter->can("set_output")
MC/DC0%0/3
$filter->can('set_input') && $filter->can('set_output') && $filter->can('run')
$filter->can("set_input")
$filter->can("set_output")
$filter->can('run')
Truth table0%0/4
A and B && C
A= $filter->can("set_input")B= $filter->can("set_output")C= $filter->can('run')
| |||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub set_input { # 2 arguments possible (input and input_postread), so stored as ref to an array | ||||||||||||||||||||||||||||||||
|
Subroutine
set_input: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 45 | 0 | my $this = shift; | |||||||||||||||||||||||||||||||||
| 46 | 0 | $this->{input} = \@_; | |||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub set_output { # 2 arguments possible (output and output_prewrite), so stored as ref to an array | ||||||||||||||||||||||||||||||||
|
Subroutine
set_output: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 50 | 0 | my $this = shift; | |||||||||||||||||||||||||||||||||
| 51 | 0 | $this->{output} = \@_; | |||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 55 | 0 | my $this = shift; | |||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | 0 | my $n = scalar @{$this->{filters}}; | |||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | return unless $n; # nothing to be done when no filters present | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | # automagical default output buffer | ||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | unless(defined($this->{output})) { # skip if a chain output is specified | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 62 | 0 | my $final_filter = $this->{filters}->[-1]; | |||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if($final_filter->isa('Text::Filter') # we can do this effort only for Text::Filter or derived classes | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$final_filter->isa("Text::Filter") and not defined $final_filter->{"_filter_output"}
MC/DC0%0/2
$final_filter->isa("Text::Filter") and not defined $final_filter->{"_filter_output"}
$final_filter->isa("Text::Filter")
not defined $final_filter->{"_filter_output"}
Truth table0%0/3
A and not(B)
A= $final_filter->isa("Text::Filter")B= not defined $final_filter->{"_filter_output"}
| |||||||||||||||||||||||||||||||||||
| 64 | && !defined($final_filter->{_filter_output})) { | ||||||||||||||||||||||||||||||||||
| 65 | 0 | $this->set_output([]); # redirects chain output to an array | |||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | # set chain input and output | ||||||||||||||||||||||||||||||||||
| 70 | 0 | $this->{filters}->[0]->set_input(@{$this->{input}}) | |||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if defined $this->{input}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 72 | 0 | $this->{filters}->[-1]->set_output(@{$this->{output}}) | |||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if defined $this->{output}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||
| 75 | # temporary buffers for in between filters | ||||||||||||||||||||||||||||||||||
| 76 | 0 | my $inbuf = []; | |||||||||||||||||||||||||||||||||
| 77 | 0 | my $outbuf = []; | |||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | # run all filters in a sequence | ||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | for(my $i = 0; $i < $n; ++$i) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 81 | 0 | my $filter = $this->{filters}->[$i]; | |||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | $filter->set_input($inbuf) unless $i == 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $filter->set_output($outbuf) unless $i == $n - 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 84 | 0 | $filter->run; | |||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if($i < $n - 1) { # pipe output of this filter to input of next one | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 86 | 0 | $inbuf = $outbuf; | |||||||||||||||||||||||||||||||||
| 87 | 0 | $outbuf = []; | |||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | 1 | 1; | |||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | __END__ | ||||||||||||||||||||||||||||||||||