| 1 | # 2001/01/25 shizukesa@pobox.com | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | package POE::Filter::Grep; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use POE::Filter; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places) | |||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | @ISA = qw(POE::Filter); | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | sub BUFFER () { 0 } | ||||||||||||||||||||||||||||||||||||||||||
| 15 | sub CODEGET () { 1 } | ||||||||||||||||||||||||||||||||||||||||||
| 16 | sub CODEPUT () { 2 } | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | sub FIRST_UNUSED () { 3 } # First unused $self offset. | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | our @EXPORT_OK = qw( FIRST_UNUSED ); | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 12 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 12 | my $type = shift; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 12 | ▶ | croak "$type must be given an even number of parameters" if @_ & 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 11 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | croak "$type requires a Code or both Get and Put parameters" unless ( | ||||||||||||||||||||||||||||||||||||||||||
| 32 | defined($params{Code}) or | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 11 | ▶ | (defined($params{Get}) and defined($params{Put})) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $params{"Code"} or defined $params{"Get"} and defined $params{"Put"}
defined $params{"Get"} and defined $params{"Put"}
MC/DC100%3/3
defined $params{"Code"} or defined $params{"Get"} and defined $params{"Put"}
defined $params{"Code"}
defined $params{"Get"}
defined $params{"Put"}
Truth table100%4/4
A or B and C
A= defined $params{"Code"}B= defined $params{"Get"}C= defined $params{"Put"}
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 35 | croak "Code element is not a subref" | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 8 | ▶ | unless (defined $params{Code} ? ref $params{Code} eq 'CODE' : 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | croak "Get or Put element is not a subref" | ||||||||||||||||||||||||||||||||||||||||||
| 38 | unless ((defined $params{Get} ? (ref $params{Get} eq 'CODE') : 1) | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | ▶ | and (defined $params{Put} ? (ref $params{Put} eq 'CODE') : 1)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
defined $params{"Get"} ? ref $params{"Get"} eq "CODE" : 1 and defined $params{"Put"} ? ref $params{"Put"} eq "CODE" : 1
MC/DC100%2/2
defined $params{"Get"} ? ref $params{"Get"} eq "CODE" : 1 and defined $params{"Put"} ? ref $params{"Put"} eq "CODE" : 1
defined $params{"Get"} ? ref $params{"Get"} eq "CODE" : 1
defined $params{"Put"} ? ref $params{"Put"} eq "CODE" : 1
Truth table100%3/3
A and B
A= defined $params{"Get"} ? ref $params{"Get"} eq "CODE" : 1B= defined $params{"Put"} ? ref $params{"Put"} eq "CODE" : 1
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | ▶ | my $get = $params{Code} || $params{Get}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$params{'Code'} || $params{'Get'}
MC/DC0%0/2
$params{'Code'} || $params{'Get'}
$params{'Code'}
$params{'Get'}
Truth table66%2/3
A || B
A= $params{'Code'}B= $params{'Get'}
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | my $put = $params{Code} || $params{Put}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$params{'Code'} || $params{'Put'}
MC/DC0%0/2
$params{'Code'} || $params{'Put'}
$params{'Code'}
$params{'Put'}
Truth table66%2/3
A || B
A= $params{'Code'}B= $params{'Put'}
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 5 | delete @params{qw(Code Get Put)}; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 5 | ▶ | carp("$type ignores unknown parameters: ", join(', ', sort keys %params)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | if scalar keys %params; | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | my $self = bless [ | |||||||||||||||||||||||||||||||||||||||||
| 49 | [ ], # BUFFER | ||||||||||||||||||||||||||||||||||||||||||
| 50 | $get, # CODEGET | ||||||||||||||||||||||||||||||||||||||||||
| 51 | $put, # CODEPUT | ||||||||||||||||||||||||||||||||||||||||||
| 52 | ], $type; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # get() is inherited from POE::Filter. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # 2001-07-27 RCC: The get_one variant of get() allows Wheel::Xyz to | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # retrieve one filtered record at a time. This is necessary for | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # filter changing and proper input flow control. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 13 | ▶ | sub get_one_start { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_one_start: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 13 | my ($self, $stream) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 13 | ▶ | push( @{$self->[BUFFER]}, @$stream ) if defined $stream; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 55 | ▶ | sub get_one { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_one: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 55 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | # Must be a loop so that the buffer will be altered as items are | ||||||||||||||||||||||||||||||||||||||||||
| 72 | # tested. | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 55 | ▶ | while (@{$self->[BUFFER]}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 65 | my $next_record = shift @{$self->[BUFFER]}; | |||||||||||||||||||||||||||||||||||||||||
| 75 | return [ $next_record ] if ( | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 65 | ▶ | grep { $self->[CODEGET]->($_) } $next_record | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 13 | return [ ]; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 7 | ▶ | sub put { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
put: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 7 | my ($self, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 7 | [ grep { $self->[CODEPUT]->($_) } @$data ]; | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # 2001-07-27 RCC: This filter now tracks state, so get_pending has | ||||||||||||||||||||||||||||||||||||||||||
| 92 | # become useful. | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 6 | ▶ | sub get_pending { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_pending: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 6 | ▶ | return undef unless @{$self->[BUFFER]}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | [ @{$self->[BUFFER]} ]; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 7 | ▶ | sub modify { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
modify: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 7 | my ($self, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 7 | for (keys %params) { | |||||||||||||||||||||||||||||||||||||||||
| 106 | 7 | ▶ | (carp("Modify $_ element must be given a coderef") and next) unless (ref $params{$_} eq 'CODE'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
ref $params{$_} eq 'CODE' || carp("Modify $_ element must be given a coderef") && (next)
carp("Modify $_ element must be given a coderef") and next
MC/DC0%0/2
ref $params{$_} eq 'CODE' || carp("Modify $_ element must be given a coderef") && (next)
ref $params{$_} eq 'CODE'
carp("Modify $_ element must be given a coderef")
Truth table66%2/3
A || B
A= ref $params{$_} eq 'CODE'B= carp("Modify $_ element must be given a coderef")
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 4 | ▶ | if (lc eq 'code') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | $self->[CODEGET] = $params{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 1 | $self->[CODEPUT] = $params{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | elsif (lc eq 'put') { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | $self->[CODEPUT] = $params{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | elsif (lc eq 'get') { | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | $self->[CODEGET] = $params{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||||||||||||||||||||||||