| 1 | # 2001/01/25 shizukesa@pobox.com | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # This implements a filter stack, which turns ReadWrite into something | ||||||||||||||||||||||||||||
| 4 | # very, very interesting. | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # 2001-07-26 RCC: I have no idea how to make this support get_one, so | ||||||||||||||||||||||||||||
| 7 | # I'm not going to right now. | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | package POE::Filter::Stackable; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use POE::Filter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places) | |||||||||||||||||||||||||||
| 16 | 1 | @ISA = qw(POE::Filter); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | sub FILTERS () { 0 } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub FIRST_UNUSED () { 1 } # First unused $self offset. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | our @EXPORT_OK = qw( FIRST_UNUSED ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.8
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 1 | my $type = shift; | |||||||||||||||||||||||||||
| 31 | 1 | ▶ | croak "$type must be given an even number of parameters" if @_ & 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 1 | my %params = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | $params{Filters} = [ ] unless defined $params{Filters}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | # Sanity check the filters | ||||||||||||||||||||||||||||
| 36 | 1 | ▶ | if ( ref $params{Filters} eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | my $self = bless [ | ||||||||||||||||||||||||||||
| 39 | $params{Filters}, # FILTERS | ||||||||||||||||||||||||||||
| 40 | 1 | ], $type; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | return $self; | |||||||||||||||||||||||||||
| 43 | } else { | ||||||||||||||||||||||||||||
| 44 | 0 | croak "Filters is not an ARRAY reference!"; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | ▶ | sub clone { | ||||||||||||||||||||||||||
|
Subroutine
clone: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 50 | 1 | my $clone = bless [ | |||||||||||||||||||||||||||
| 51 | [ ], # FILTERS | ||||||||||||||||||||||||||||
| 52 | ], ref $self; | ||||||||||||||||||||||||||||
| 53 | 1 | foreach my $filter (@{$self->[FILTERS]}) { | |||||||||||||||||||||||||||
| 54 | 4 | push (@{$clone->[FILTERS]}, $filter->clone()); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 1 | $clone; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | sub get_one_start { | ||||||||||||||||||||||||||
|
Subroutine
get_one_start: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 2 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 63 | 2 | $self->[FILTERS]->[0]->get_one_start($data); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # RCC 2005-06-28: get_one() needs to strobe through all the filters | ||||||||||||||||||||||||||||
| 67 | # regardless whether there's data to input to each. This is because a | ||||||||||||||||||||||||||||
| 68 | # later filter in the chain may produce multiple things from one piece | ||||||||||||||||||||||||||||
| 69 | # of input. If we stop even though there's no subsequent input, we | ||||||||||||||||||||||||||||
| 70 | # may lose something. | ||||||||||||||||||||||||||||
| 71 | # | ||||||||||||||||||||||||||||
| 72 | # Keep looping through the filters we manage until get_one() returns a | ||||||||||||||||||||||||||||
| 73 | # record, or until none of the filters exchange data. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 3 | ▶ | sub get_one { | ||||||||||||||||||||||||||
|
Subroutine
get_one: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 3 | my $return = [ ]; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | ▶ | while (!@$return) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 6 | my $exchanged = 0; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 6 | foreach my $filter (@{$self->[FILTERS]}) { | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # If we have something to input to the next filter, do that. | ||||||||||||||||||||||||||||
| 86 | 24 | ▶ | if (@$return) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 10 | $filter->get_one_start($return); | |||||||||||||||||||||||||||
| 88 | 10 | $exchanged++; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Get what we can from the current filter. | ||||||||||||||||||||||||||||
| 92 | 24 | $return = $filter->get_one(); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 6 | ▶ | last unless $exchanged; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 3 | return $return; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # get() is inherited from POE::Filter. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1 | ▶ | sub put { | ||||||||||||||||||||||||||
|
Subroutine
put: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 1 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 107 | 1 | foreach my $filter (reverse @{$self->[FILTERS]}) { | |||||||||||||||||||||||||||
| 108 | 4 | $data = $filter->put($data); | |||||||||||||||||||||||||||
| 109 | 4 | ▶ | last unless @$data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 1 | $data; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 1 | ▶ | sub get_pending { | ||||||||||||||||||||||||||
|
Subroutine
get_pending: called CC=5 SCAR=17.8
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 118 | 1 | my $data; | |||||||||||||||||||||||||||
| 119 | 1 | for (@{$self->[FILTERS]}) { | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | $_->put($data) if $data && @{$data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$data and @{$data;}
MC/DC0%0/2
$data and @{$data;}
$data
@{$data;}
Truth table33%1/3
A and B
A= $dataB= @{$data;}
| |||||||||||||||||||||||||||||
| 121 | 4 | $data = $_->get_pending; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 1 | ▶ | $data || []; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 3 | ▶ | sub filter_types { | ||||||||||||||||||||||||||
|
Subroutine
filter_types: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 3 | map { ref($_) } @{$_[0]->[FILTERS]}; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 6 | ▶ | sub filters { | ||||||||||||||||||||||||||
|
Subroutine
filters: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 6 | @{$_[0]->[FILTERS]}; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 1 | ▶ | sub shift { | ||||||||||||||||||||||||||
|
Subroutine
shift: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 142 | 1 | my $filter = shift @{$self->[FILTERS]}; | |||||||||||||||||||||||||||
| 143 | 1 | my $pending = $filter->get_pending; | |||||||||||||||||||||||||||
| 144 | 1 | ▶ | $self->[FILTERS]->[0]->put( $pending ) if $pending; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 1 | $filter; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | ▶ | sub unshift { | ||||||||||||||||||||||||||
|
Subroutine
unshift: called CC=4 SCAR=15.6
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 1 | my ($self, @filters) = @_; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # Sanity check | ||||||||||||||||||||||||||||
| 154 | 1 | foreach my $elem ( @filters ) { | |||||||||||||||||||||||||||
| 155 | 1 | ▶ | if ( ! defined $elem or ! UNIVERSAL::isa( $elem, 'POE::Filter' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $elem and UNIVERSAL::isa($elem, "POE::Filter")
MC/DC0%0/2
defined $elem and UNIVERSAL::isa($elem, "POE::Filter")
defined $elem
UNIVERSAL::isa($elem, "POE::Filter")
Truth table33%1/3
A and B
A= defined $elemB= UNIVERSAL::isa($elem, "POE::Filter")
| |||||||||||||||||||||||||||||
| 156 | 0 | croak "Filter element is not a POE::Filter instance!"; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 1 | unshift(@{$self->[FILTERS]}, @filters); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 5 | ▶ | sub push { | ||||||||||||||||||||||||||
|
Subroutine
push: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 5 | my ($self, @filters) = @_; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Sanity check | ||||||||||||||||||||||||||||
| 169 | 5 | foreach my $elem ( @filters ) { | |||||||||||||||||||||||||||
| 170 | 5 | ▶ | if ( ! defined $elem or ! UNIVERSAL::isa( $elem, 'POE::Filter' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $elem and UNIVERSAL::isa($elem, "POE::Filter")
MC/DC100%2/2
defined $elem and UNIVERSAL::isa($elem, "POE::Filter")
defined $elem
UNIVERSAL::isa($elem, "POE::Filter")
Truth table100%3/3
A and B
A= defined $elemB= UNIVERSAL::isa($elem, "POE::Filter")
| |||||||||||||||||||||||||||||
| 171 | 4 | croak "Filter element is not a POE::Filter instance!"; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 1 | push(@{$self->[FILTERS]}, @filters); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 1 | ▶ | sub pop { | ||||||||||||||||||||||||||
|
Subroutine
pop: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 181 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 182 | 1 | my $filter = pop @{$self->[FILTERS]}; | |||||||||||||||||||||||||||
| 183 | 1 | my $pending = $filter->get_pending; | |||||||||||||||||||||||||||
| 184 | 1 | ▶ | $self->[FILTERS]->[-1]->put( $pending ) if $pending; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 1 | $filter; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 1 | 1; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||||||||||