blib/lib/IO/Lambda/Inotify.pm

filter: Statementstmt 86.6% 97 / 112 Branchbran 54.0% 27 / 50 Conditioncond 60.0% 12 / 20 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 85.1% 23 / 27 Podpod 33.3% 2 / 6 totaltotal 71.8% 163 / 227 CC 32 SCAR 17.2
CC
32
Cov
75%
__ANON__
18.7
inotify_plain
9.2
inotify_auto
6.4
CRAP 5.6
↑ Summary
1 package IO::Lambda::Inotify;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use IO::Lambda qw(:all :dev);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Linux::Inotify2;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 our $VERSION = '1.01';
10 4 our @EXPORT_OK = qw(
11 inotify
12 inotify_server
13 inotify_auto
14 inotify_timeout
15 inotify_plain
16 );
17 4 our %EXPORT_TAGS = ( all => [ qw(inotify) ] );
18 4 our $INOTIFY;
19  
20 4 ▶our $DEBUG = $IO::Lambda::DEBUG{inotify} || 0;
Condition50%1/2
$IO::Lambda::DEBUG{'inotify'} || 0
l!l
04
MC/DC0%0/1
$IO::Lambda::DEBUG{'inotify'} || 0
$IO::Lambda::DEBUG{'inotify'}
Truth table50%1/2
A
A= $IO::Lambda::DEBUG{'inotify'}
Aresult
11
00
21  
22 sub inotify_one_server
23 6 ▶{
Subroutine inotify_one_server: called CC=3 SCAR=11.0
Pod undocumented
24 6 my ($inotify, $fh) = @_;
25  
26 6 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 6 0 6
27 6 ▶ open $fh, "<&", $inotify-> fileno or die "can't dup inotify handle:$!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<&", $inotify->fileno or die "can't dup inotify handle:$!" 6 0 6
28 }
29  
30 6 $inotify-> blocking(0);
31  
32 6 ▶ lambda {
Subroutine __ANON__: called CC=1 SCAR=0.0
33 6 context $fh;
34 6 ▶ readable {
Subroutine __ANON__: called CC=2 SCAR=6.9
35 6 $inotify-> poll;
36 6 ▶ if ($inotify-> {io_lambda_condvar}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inotify->{"io_lambda_condvar"}) 5 1 6
37 5 $inotify-> {io_lambda_condvar}-> terminate;
38 5 delete $inotify-> {io_lambda_condvar};
39 }
40 6 again;
41 }
42 6 }
43 6 }
44  
45 sub inotify_server
46 1 ▶{
Subroutine inotify_server: called CC=1 SCAR=0.0
Pod documented
47 1 my @k = @_;
48 1 ▶ lambda {
Subroutine __ANON__: called CC=1 SCAR=0.0
49 1 context map { inotify_one_server $_ } @k;
50 1 &tails();
51 1 };
52 }
53  
54 sub inotify
55 8 ▶{
Subroutine inotify: called CC=6 SCAR=18.3
Pod documented
56 8 ▶ return inotify_auto(@_) unless $_[0] and ref($_[0]) and $_[0]->isa('Linux::Inotify2');
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] and ref $_[0] and $_[0]->isa("Linux::Inotify2") 2 6 8
Condition66%4/6
$_[0] and ref $_[0] and $_[0]->isa("Linux::Inotify2")
!ll&&!rl&&r
206
$_[0] and ref $_[0]
!ll&&!rl&&r
026
MC/DC33%1/3
$_[0] and ref $_[0] and $_[0]->isa("Linux::Inotify2")
$_[0] ref $_[0] $_[0]->isa("Linux::Inotify2")
Truth table50%2/4
A and B and C
A= $_[0]B= ref $_[0]C= $_[0]->isa("Linux::Inotify2")
ABCresult
0XX0
10X0
1100
1111
57 6 ▶ return inotify_timeout(@_) if 4 == @_ and defined $_[3];
Branch: 1 branch
BranchTrueFalseTotal
if 4 == @_ and defined $_[3] 5 1 6
Condition66%2/3
4 == @_ and defined $_[3]
!ll&&!rl&&r
105
MC/DC50%1/2
4 == @_ and defined $_[3]
4 == @_ defined $_[3]
Truth table66%2/3
A and B
A= 4 == @_B= defined $_[3]
ABresult
0X0
100
111
58 1 return inotify_plain(@_);
59 }
60  
61 sub inotify_plain
62 6 ▶{
Subroutine inotify_plain: called CC=7 SCAR=22.2
Pod undocumented
63 6 my ( $inotify, $path, $flags ) = @_;
64  
65 6 my @queue;
66 5 ▶ my $watch = $inotify-> watch( $path, $flags, sub {
Subroutine __ANON__: called CC=6 SCAR=29.3
67 5 my $event = shift;
68 5 ▶ push @queue, $event || $!;
Condition33%1/3
$event || $!
l!l&&r!l&&!r
500
MC/DC0%0/2
$event || $!
$event $!
Truth table33%1/3
A || B
A= $eventB= $!
ABresult
1X1
011
000
69 5 ▶ if ( $DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Lambda::Inotify::DEBUG) 0 5 5
70 0 ▶ if ( $event ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event) { } 0 0 0
71 0 ▶ warn "event $inotify.". $event-> w . " $event\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Inotify::DEBUG 0 0 0
72 } else {
73 0 ▶ warn "event on $inotify failed :$!\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Inotify::DEBUG 0 0 0
74 }
75 }
76 6 } );
77  
78 6 ▶ unless ( $watch ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($watch) 0 6 6
79 0 my $error = $!;
80 0 ▶ warn "$inotify.watch($path,$flags) = $error\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Inotify::DEBUG 0 0 0
81 0 ▶ return lambda { (undef, $error) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 }
83  
84 6 ▶ warn "new $watch\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Inotify::DEBUG 0 6 6
85  
86 6 ▶ unless ( $inotify-> {io_lambda_server} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inotify->{"io_lambda_server"}) 5 1 6
87 5 my $fh;
88 5 ▶ warn "auto-start $inotify\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Inotify::DEBUG 0 5 5
89 5 ▶ unless ( open $fh, , "<&", $inotify-> fileno) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<&", $inotify->fileno) 0 5 5
90 0 my $error = $!;
91 0 $watch-> cancel;
92 0 ▶ return lambda{ (undef, "can't dup inotify handle:$error") };
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 }
94 5 $inotify-> {io_lambda_server} = inotify_one_server($inotify);
95 5 $inotify-> {io_lambda_server}-> start;
96 }
97 6 $inotify-> {io_lambda_refcnt}++;
98
99 5 ▶ my $scope_exit = bless sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
100 5 ▶ warn "auto-cancel $watch\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Inotify::DEBUG 0 5 5
101  
102 5 ▶ unless (--$inotify-> {io_lambda_refcnt}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (--$inotify->{"io_lambda_refcnt"}) 4 1 5
103 4 ▶ warn "auto-stop $inotify\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Inotify::DEBUG 0 4 4
104 4 $inotify-> {io_lambda_server}-> terminate;
105 4 undef $inotify-> {io_lambda_server};
106 }
107 5 $watch-> cancel;
108 6 }, __PACKAGE__;
109
110  
111 # this lambda will be called on again and again
112 8 ▶ return lambda {
Subroutine __ANON__: called CC=4 SCAR=14.0
113 8 ▶ unless ( $watch-> {inotify} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($watch->{"inotify"}) 1 7 8
114 1 ▶ warn "$watch was cancelled\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Inotify::DEBUG 0 1 1
115 1 return undef, 'watcher is expired';
116 }
117  
118 7 my $scope_keeper = $scope_exit;
119  
120 7 ▶ my $listener = $inotify-> {io_lambda_condvar} //= lambda {};
Subroutine __ANON__: not called CC=1 SCAR=0.2
Condition66%2/3
$inotify->{"io_lambda_condvar"} //= &lambda(sub { } )
l!l&&r!l&&!r
160
MC/DC0%0/2
$inotify->{"io_lambda_condvar"} //= &lambda(sub { } )
$inotify->{"io_lambda_condvar"} &lambda(sub { } )
Truth table66%2/3
A //= B
A= $inotify->{"io_lambda_condvar"}B= &lambda(sub { } )
ABresult
1X1
011
000
121 7 $listener-> bind;
122 7 context $listener;
123 5 ▶ tail {
Subroutine __ANON__: called CC=4 SCAR=14.0
124 5 ▶ unless (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@queue) 1 4 5
125 1 this-> start;
126 1 return; # not our watcher
127 }
128  
129 4 my $event = shift @queue;
130 4 ▶ warn "[$event]\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Inotify::DEBUG 0 4 4
131 4 ▶ return ref($event) ? ($event) : (undef, $event);
Branch: 1 branch
BranchTrueFalseTotal
ref $event ? : 4 0 4
132 }
133 7 }
134 6 }
135  
136 sub inotify_auto
137 2 ▶{
Subroutine inotify_auto: called CC=4 SCAR=18.6
Pod undocumented
138 2 my @stuff = @_;
139  
140 2 ▶ $INOTIFY ||= Linux::Inotify2-> new;
Condition66%2/3
$IO::Lambda::Inotify::INOTIFY ||= "Linux::Inotify2"->new
l!l&&r!l&&!r
110
MC/DC0%0/2
$IO::Lambda::Inotify::INOTIFY ||= "Linux::Inotify2"->new
$IO::Lambda::Inotify::INOTIFY "Linux::Inotify2"->new
Truth table66%2/3
A ||= B
A= $IO::Lambda::Inotify::INOTIFYB= "Linux::Inotify2"->new
ABresult
1X1
011
000
141 2 ▶ unless ( $INOTIFY ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($IO::Lambda::Inotify::INOTIFY) 0 2 2
142 0 my $error = $!;
143 0 ▶ warn "Linux::Inotify2-> new(): $error\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Inotify::DEBUG 0 0 0
144 0 ▶ return lambda { (undef, $error) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 }
146  
147 2 return inotify( $INOTIFY, @stuff );
148 }
149  
150 sub inotify_timeout
151 5 ▶{
Subroutine inotify_timeout: called CC=1 SCAR=0.0
Pod undocumented
152 5 my ( $inotify, $path, $flags, $timeout ) = @_;
153  
154 5 my $watch = inotify_plain($inotify, $path, $flags);
155  
156 6 ▶ return lambda {
Subroutine __ANON__: called CC=1 SCAR=0.0
157 6 context $watch;
158 6 ▶ tail { this-> terminate; return @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
159  
160 6 context $timeout;
161 6 ▶ timeout { this-> terminate; return undef, 'timeout' };
Subroutine __ANON__: called CC=1 SCAR=0.0
162 }
163 5 }
164  
165 5 ▶sub DESTROY { shift-> () }
Subroutine DESTROY: called CC=1 SCAR=0.0
166  
167 4 1;
168  
169 __DATA__
↑ Summary