File Coverage

blib/lib/POE/Filter/BigBrother.pm
Criterion Covered Total %
statement 38 41 92.6
branch 7 10 70.0
condition 8 12 66.6
subroutine 9 10 90.0
pod 5 5 100.0
total 67 78 85.9


line stmt bran cond sub pod time code
1             # -*- encoding: utf-8; mode: cperl -*-
2              
3             # $Id: BigBrother.pm yblusseau $
4              
5             package POE::Filter::BigBrother;
6              
7 1     1   764 use strict;
  1         1  
  1         32  
8 1     1   4 use warnings;
  1         2  
  1         28  
9 1     1   743 use POE::Filter;
  1         404  
  1         25  
10              
11 1     1   4 use vars qw($VERSION @ISA);
  1         1  
  1         112  
12             $VERSION = '0.13';
13             @ISA = qw(POE::Filter);
14              
15             sub DEBUG () { 0 }
16              
17             sub BUFFER () { 0 }
18             sub COMBO_MSG () { 1 }
19              
20             use constant {
21 1         546 LF => "\x0A",
22             CR => "\x0D",
23 1     1   3 };
  1         2  
24              
25             #------------------------------------------------------------------------------
26             sub new {
27 2     2 1 4971 my $type = shift;
28              
29 2         9 my $self = bless [
30             '', # BUFFER
31             0 , # COMBO_MSG
32             ], $type;
33              
34 2         6 $self;
35             }
36              
37              
38             #------------------------------------------------------------------------------
39             # get() is inherited from POE::Filter.
40              
41             #------------------------------------------------------------------------------
42             # 2001-07-27 RCC: The get_one() variant of get() allows Wheel::Xyz to
43             # retrieve one filtered block at a time. This is necessary for filter
44             # changing and proper input flow control.
45              
46             sub get_one_start {
47 4     4 1 22 my ($self, $stream) = @_;
48 4         17 $self->[BUFFER] .= join '', @$stream;
49              
50 4         5 DEBUG and warn $self->[BUFFER];
51              
52             # Is it a combo message?
53 4 100 66     32 if (index($self->[BUFFER], "combo".CR) == 0 or
54             index($self->[BUFFER], "combo".LF) == 0) {
55             # suppress the "combo" keyword and the first new-line character
56 3         5 substr($self->[BUFFER], 0, 6, "");
57              
58             # suppress a potential second new-line character
59 3         7 my $first = substr($self->[BUFFER], 0, 1);
60 3 50 33     12 substr($self->[BUFFER], 0, 1, "") if $first eq CR or $first eq LF;
61              
62 3         11 $self->[COMBO_MSG] = 1; # The message is a combo message
63             }
64             }
65              
66             sub get_one {
67 10     10 1 43 my $self = shift;
68 10 100 66     49 return [] unless defined $self->[BUFFER] and length $self->[BUFFER];
69              
70             # Split the Combo message
71 6 100 100     33 if ($self->[COMBO_MSG] and $self->[BUFFER] =~ /(?:(?:\x0D\x0A?){2}|(?:\x0A\x0D?){2})status/) {
72 2         15 my ($msg, $rest) = split /(?:(?:\x0D\x0A?){2}|(?:\x0A\x0D?){2})status/, $self->[BUFFER], 2;
73              
74 2         5 $self->[BUFFER] = "status$rest"; # Next message
75 2         8 return [ $msg ]; # First message
76             }
77             else {
78 4         6 my $block = $self->[BUFFER];
79 4         6 $self->[BUFFER] = ''; # Clear the buffer
80 4         8 $block =~ s/\0+$//; # Remove NULL characters from the end
81 4         11 return [ $block ]; # Return the data
82             }
83             }
84              
85             #------------------------------------------------------------------------------
86              
87             sub put {
88 4     4 1 1111 my ($self, $chunks) = @_;
89 4         11 [ @$chunks ];
90             }
91              
92             #------------------------------------------------------------------------------
93              
94             sub get_pending {
95 0     0 1   my $self = shift;
96 0 0         return undef unless length $self->[BUFFER];
97 0           [ $self->[BUFFER] ];
98             }
99              
100             1; # End of POE::Filter::BigBrother
101             __END__