blib/lib/Schedule/Easing/Stream.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 132 CC 16 SCAR 40.1
CC
16
Cov
0%
read
110.0
new
12.0
sleepwait
12.0
CRAP 55.2
1 package Schedule::Easing::Stream;
2  
3 use strict;
4 use warnings;
5 use Carp qw/confess/;
6  
7 our $VERSION='0.1.4';
8  
9 sub new {
10 my ($ref,%opt)=@_;
11 my $class=ref($ref)||$ref;
12 my %self=(
13 fh =>\*STDIN,
14 input =>undef,
15 update=>undef,
16 lines =>undef,
17 sleep =>0,
18 clock =>undef,
19 regexp=>undef,
20 batch =>1,
21 (ref($ref)?%$ref:()),
22 );
23 foreach my $k (grep {exists($self{$_})} keys(%opt)) { $self{$k}=$opt{$k} }
24 if(0==grep {defined($_)} @self{qw/lines clock regexp/}) { $self{lines}=1 }
25 return bless(\%self,$class);
26 }
27  
28 sub sleepwait {
29 my ($self)=@_;
30 my $fh=$$self{fh};
31 if($$self{sleep}&&($$self{sleep}>=1)) {
32 my $rin='';
33 vec($rin,fileno($fh),1)=1;
34 if(!select($rin,'','',0.1)) { sleep($$self{sleep}) }
35 }
36 }
37  
38 sub processBatch {
39 my ($self,$batch,$wait)=@_;
40 if(@$batch) { my @copy=@$batch; @$batch=(); &{$$self{input}}(@copy) }
41 if($wait) { $self->sleepwait() }
42 }
43  
44 sub read {
45 my ($self)=@_;
46 my $fh=$$self{fh};
47 if(ref($$self{input}) ne 'CODE') { confess('Input callback must be provided') }
48 if($$self{update}&&(ref($$self{update}) ne 'CODE')) { confess('Update must be a callback') }
49 #
50 my @batch;
51 my $countdown=$$self{lines};
52 if($$self{clock}&&($$self{clock}>=1)) {
53 $SIG{ALRM}=sub {
54 $self->processBatch(\@batch,1);
55 &{$$self{update}}(time()) if($$self{update});
56 alarm($$self{clock});
57 };
58 alarm($$self{clock});
59 }
60 while(<$fh>) {
61 if($$self{regexp}&&(my ($ts)=($_=~$$self{regexp}))) {
62 $self->processBatch(\@batch,0);
63 &{$$self{update}}($ts) if($$self{update});
64 $self->processBatch([$_],1);
65 $countdown=$$self{lines};
66 next;
67 }
68 push @batch,$_;
69 if(1+$#batch<$$self{batch}) { next }
70 $self->processBatch(\@batch,1);
71 if(defined($countdown)&&(($countdown-=$$self{batch})<=0)) {
72 &{$$self{update}}(time()) if($$self{update});
73 $countdown=$$self{lines};
74 }
75 };
76 $self->processBatch(\@batch,0);
77 return $self;
78 }
79  
80 1;
81  
82 __END__