Framed/Write.pm

filter: Statementstmt 80.8% 55 / 68 Branchbran 80.0% 16 / 20 Conditioncond 42.8% 6 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 87.5% 14 / 16 Podpod 0.0% 0 / 8 totaltotal 67.9% 91 / 134 SCAR 29.8
CC
16
Cov
75%
_write_now_then_callback
9.4
disable_write_queue
4.1
_write_now
3.0
CRAP 19.8
↑ Summary Framed/Read.pm »
1 package IO::Framed::Write;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use IO::Framed::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 sub new {
9 4 ▶ my ( $class, $out_fh ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
10  
11 4 my $self = {
12 _out_fh => $out_fh,
13 _writer => \&_write_now,
14 };
15  
16 4 return bless $self, $class;
17 }
18  
19 0 ▶sub get_write_fh { return $_[0]->{'_out_fh'} }
Subroutine get_write_fh: not called CC=1 SCAR=6.9
Pod undocumented
20  
21 sub disable_write_queue {
22 3 ▶ if ( $_[0]->{'_write_queue'} && @{ $_[0]->{'_write_queue'} } ) {
Subroutine disable_write_queue: called CC=3 SCAR=14.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"_write_queue"} and @{$_[0]{"_write_queue"};}) 0 3 3
Condition33%1/3
$_[0]{"_write_queue"} and @{$_[0]{"_write_queue"};}
!ll&&!rl&&r
300
MC/DC0%0/2
$_[0]{"_write_queue"} and @{$_[0]{"_write_queue"};}
$_[0]{"_write_queue"} @{$_[0]{"_write_queue"};}
Truth table33%1/3
A and B
A= $_[0]{"_write_queue"}B= @{$_[0]{"_write_queue"};}
ABresult
0X0
100
111
23 0 die 'Refuse to disable non-empty write queue!';
24 }
25  
26 3 $_[0]->{'_writer'} = \&_write_now;
27 3 return $_[0];
28 }
29  
30 sub enable_write_queue {
31 2 ▶ $_[0]->{'_write_queue'} ||= [];
Subroutine enable_write_queue: called CC=2 SCAR=7.1
Pod undocumented
Condition50%1/2
$_[0]{"_write_queue"} ||= []
l!l
02
MC/DC0%0/1
$_[0]{"_write_queue"} ||= []
$_[0]{"_write_queue"}
Truth table50%1/2
A
A= $_[0]{"_write_queue"}
Aresult
11
00
32 2 $_[0]->{'_writer'} = \&_enqueue_write;
33 2 return $_[0];
34 }
35  
36 sub write {
37 65552 ▶ die IO::Framed::X->create('EmptyWrite', "Undefined input to write()!") if !defined $_[1];
Subroutine write: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 6 65546 65552
38 65546 ▶ die IO::Framed::X->create('EmptyWrite', "Empty input to write()!" ) if !length $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless length $_[1] 4 65542 65546
39  
40 65542 $_[0]->{'_writer'}->(@_);
41 }
42  
43 #======================================================================
44 #blocking
45 #======================================================================
46  
47 sub _write_now {
48 65539 ▶ local $!;
Subroutine _write_now: called CC=3 SCAR=11.0
49  
50 65539 ▶ $_[0]->can('WRITE')->( $_[0]->{'_out_fh'}, $_[1] ) or do {
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->can("WRITE")->($_[0]{"_out_fh"}, $_[1]) or do { die "IO::Framed::X"->create("WriteError", $!) } 1 65538 65539
51 1 die IO::Framed::X->create('WriteError', $!);
52 };
53  
54 65538 ▶ $_[2]->() if $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] 0 65538 65538
55  
56 65538 return;
57 }
58  
59 #======================================================================
60 #non-blocking
61 #======================================================================
62  
63 sub _enqueue_write {
64 3 ▶ my $self = shift;
Subroutine _enqueue_write: called CC=1 SCAR=0.0
65  
66 3 push @{ $self->{'_write_queue'} }, \@_;
67  
68 3 return;
69 }
70  
71 #----------------------------------------------------------------------
72  
73 sub flush_write_queue {
74 4098 ▶ my ($self) = @_;
Subroutine flush_write_queue: called CC=3 SCAR=11.0
Pod undocumented
75  
76 4098 ▶ while ( my $qi = $self->{'_write_queue'}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $qi = $self->{"_write_queue"}[0]) 4098 3 4101
77 4098 ▶ return 0 if !$self->_write_now_then_callback( @$qi );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_write_now_then_callback(@$qi) 4095 3 4098
78  
79 3 shift @{ $self->{'_write_queue'} };
80 }
81  
82 3 return 1;
83 }
84  
85 sub get_write_queue_count {
86 5 ▶ my ($self) = @_;
Subroutine get_write_queue_count: called CC=1 SCAR=0.0
Pod undocumented
87  
88 5 return 0 + @{ $self->{'_write_queue'} };
89 }
90  
91 sub forget_write_queue {
92 0 ▶ my ($self) = @_;
Subroutine forget_write_queue: not called CC=1 SCAR=6.9
Pod undocumented
93  
94 0 my $count = @{ $self->{'_write_queue'} };
95  
96 0 @{ $self->{'_write_queue'} } = ();
97  
98 0 return $count;
99 }
100  
101 sub WRITE {
102 2 ▶ require IO::SigGuard;
Subroutine WRITE: called CC=1 SCAR=0.0
103 2 IO::SigGuard->import('syswrite'); # Needed for IO::SigGuard 0.13+
104 2 *WRITE = *IO::SigGuard::syswrite;
105 2 goto &WRITE;
106 }
107  
108 #----------------------------------------------------------------------
109  
110 sub _write_now_then_callback {
111 4098 ▶ local $!;
Subroutine _write_now_then_callback: called CC=7 SCAR=22.4
112  
113 4098 ▶ my $wrote = $_[0]->can('WRITE')->( $_[0]->{'_out_fh'}, $_[1] ) || do {
Condition66%2/3
$_[0]->can('WRITE')->($_[0]{'_out_fh'}, $_[1]) || do { if ($! and not $!{'EAGAIN'} and not $!{'EWOULDBLOCK'}) { die 'IO::Framed::X'->create('WriteError', $!); } ; return undef }
l!l&&r!l&&!r
340950
MC/DC0%0/2
$_[0]->can('WRITE')->($_[0]{'_out_fh'}, $_[1]) || do { if ($! and not $!{'EAGAIN'} and not $!{'EWOULDBLOCK'}) { die 'IO::Framed::X'->create('WriteError', $!); } ; return undef }
$_[0]->can('WRITE')->($_[0]{'_out_fh'}, $_[1]) do { if ($! and not $!{'EAGAIN'} and not $!{'EWOULDBLOCK'}) { die 'IO::Framed::X'->create('WriteError', $!); } ; return undef }
Truth table66%2/3
A || B
A= $_[0]->can('WRITE')->($_[0]{'_out_fh'}, $_[1])B= do { if ($! and not $!{'EAGAIN'} and not $!{'EWOULDBLOCK'}) { die 'IO::Framed::X'->create('WriteError', $!); } ; return undef }
ABresult
1X1
011
000
114 4095 ▶ if ($! && !$!{'EAGAIN'} && !$!{'EWOULDBLOCK'}) {
Subroutine BEGIN: called CC=1 SCAR=0.7
Branch: 1 branch
BranchTrueFalseTotal
if ($! and not $!{"EAGAIN"} and not $!{"EWOULDBLOCK"}) 0 4095 4095
Condition33%2/6
$! and not $!{"EAGAIN"} and not $!{"EWOULDBLOCK"}
!ll&&!rl&&r
409500
$! and not $!{"EAGAIN"}
!ll&&!rl&&r
040950
MC/DC0%0/3
$! and not $!{"EAGAIN"} and not $!{"EWOULDBLOCK"}
$! not $!{"EAGAIN"} not $!{"EWOULDBLOCK"}
Truth table25%1/4
A and not(B) and not(C)
A= $!B= not $!{"EAGAIN"}C= not $!{"EWOULDBLOCK"}
ABCresult
0XX0
10X0
1100
1111
115 0 die IO::Framed::X->create('WriteError', $!);
116 }
117  
118 4095 return undef;
119 };
120  
121 3 ▶ if ($wrote == length $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrote == length $_[1]) 3 0 3
122 3 $_[0]->{'_write_queue_partial'} = 0;
123 3 ▶ $_[2]->() if $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] 2 1 3
124 3 return 1;
125 }
126  
127 #Trim the bytes that we did send.
128 0 substr( $_[1], 0, $wrote ) = q<>;
129  
130 #This seems useful to track … ??
131 0 $_[0]->{'_write_queue_partial'} = 1;
132  
133 0 return 0;
134 }
135  
136 5 1;
↑ Summary Framed/Read.pm »