| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # 2001/01/25 shizukesa@pobox.com | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | package POE::Filter::Grep; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 2290 | use strict; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 75 |  | 
| 6 | 2 |  |  | 2 |  | 462 | use POE::Filter; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 59 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 2 |  |  | 2 |  | 10 | use vars qw($VERSION @ISA); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 136 |  | 
| 9 |  |  |  |  |  |  | $VERSION = '1.365'; # NOTE - Should be #.### (three decimal places) | 
| 10 |  |  |  |  |  |  | @ISA = qw(POE::Filter); | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 2 |  |  | 2 |  | 10 | use Carp qw(croak carp); | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 211 |  | 
| 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 |  |  | 2 |  | 9 | use base 'Exporter'; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 1347 |  | 
| 21 |  |  |  |  |  |  | our @EXPORT_OK = qw( FIRST_UNUSED ); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | #------------------------------------------------------------------------------ | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | sub new { | 
| 27 | 12 |  |  | 12 | 1 | 10927 | my $type = shift; | 
| 28 | 12 | 100 |  |  |  | 186 | croak "$type must be given an even number of parameters" if @_ & 1; | 
| 29 | 11 |  |  |  |  | 29 | my %params = @_; | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 11 | 100 | 100 |  |  | 591 | croak "$type requires a Code or both Get and Put parameters" unless ( | 
|  |  |  | 66 |  |  |  |  | 
| 32 |  |  |  |  |  |  | defined($params{Code}) or | 
| 33 |  |  |  |  |  |  | (defined($params{Get}) and defined($params{Put})) | 
| 34 |  |  |  |  |  |  | ); | 
| 35 | 8 | 100 |  |  |  | 202 | croak "Code element is not a subref" | 
|  |  | 100 |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | unless (defined $params{Code} ? ref $params{Code} eq 'CODE' : 1); | 
| 37 | 7 | 100 | 66 |  |  | 384 | croak "Get or Put element is not a subref" | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | unless ((defined $params{Get} ? (ref $params{Get} eq 'CODE') : 1) | 
| 39 |  |  |  |  |  |  | and   (defined $params{Put} ? (ref $params{Put} eq 'CODE') : 1)); | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 5 |  | 66 |  |  | 22 | my $get = $params{Code} || $params{Get}; | 
| 42 | 5 |  | 66 |  |  | 22 | my $put = $params{Code} || $params{Put}; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 5 |  |  |  |  | 13 | delete @params{qw(Code Get Put)}; | 
| 45 | 5 | 50 |  |  |  | 19 | carp("$type ignores unknown parameters: ", join(', ', sort keys %params)) | 
| 46 |  |  |  |  |  |  | if scalar keys %params; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 5 |  |  |  |  | 36 | 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 |  |  |  |  |  |  | sub get_one_start { | 
| 64 | 13 |  |  | 13 | 1 | 1220 | my ($self, $stream) = @_; | 
| 65 | 13 | 50 |  |  |  | 33 | push( @{$self->[BUFFER]}, @$stream ) if defined $stream; | 
|  | 13 |  |  |  |  | 56 |  | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub get_one { | 
| 69 | 55 |  |  | 55 | 1 | 3900 | my $self = shift; | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | # Must be a loop so that the buffer will be altered as items are | 
| 72 |  |  |  |  |  |  | # tested. | 
| 73 | 55 |  |  |  |  | 59 | while (@{$self->[BUFFER]}) { | 
|  | 78 |  |  |  |  | 250 |  | 
| 74 | 65 |  |  |  |  | 62 | my $next_record = shift @{$self->[BUFFER]}; | 
|  | 65 |  |  |  |  | 94 |  | 
| 75 | 65 |  |  |  |  | 160 | return [ $next_record ] if ( | 
| 76 | 65 | 100 |  |  |  | 77 | grep { $self->[CODEGET]->($_) } $next_record | 
| 77 |  |  |  |  |  |  | ); | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 13 |  |  |  |  | 36 | return [ ]; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | #------------------------------------------------------------------------------ | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub put { | 
| 86 | 7 |  |  | 7 | 1 | 2871 | my ($self, $data) = @_; | 
| 87 | 7 |  |  |  |  | 13 | [ grep { $self->[CODEPUT]->($_) } @$data ]; | 
|  | 39 |  |  |  |  | 169 |  | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | #------------------------------------------------------------------------------ | 
| 91 |  |  |  |  |  |  | # 2001-07-27 RCC: This filter now tracks state, so get_pending has | 
| 92 |  |  |  |  |  |  | # become useful. | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub get_pending { | 
| 95 | 6 |  |  | 6 | 1 | 25 | my $self = shift; | 
| 96 | 6 | 100 |  |  |  | 7 | return undef unless @{$self->[BUFFER]}; | 
|  | 6 |  |  |  |  | 56 |  | 
| 97 | 3 |  |  |  |  | 4 | [ @{$self->[BUFFER]} ]; | 
|  | 3 |  |  |  |  | 13 |  | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | #------------------------------------------------------------------------------ | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | sub modify { | 
| 103 | 7 |  |  | 7 | 1 | 2734 | my ($self, %params) = @_; | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 7 |  |  |  |  | 21 | for (keys %params) { | 
| 106 | 7 | 100 | 50 |  |  | 476 | (carp("Modify $_ element must be given a coderef") and next) unless (ref $params{$_} eq 'CODE'); | 
| 107 | 4 | 100 |  |  |  | 25 | if (lc eq 'code') { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
| 108 | 1 |  |  |  |  | 2 | $self->[CODEGET] = $params{$_}; | 
| 109 | 1 |  |  |  |  | 6 | $self->[CODEPUT] = $params{$_}; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  | elsif (lc eq 'put') { | 
| 112 | 1 |  |  |  |  | 8 | $self->[CODEPUT] = $params{$_}; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  | elsif (lc eq 'get') { | 
| 115 | 1 |  |  |  |  | 9 | $self->[CODEGET] = $params{$_}; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | 1; | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | __END__ |