Framed/Read.pm

filter: Statementstmt 92.8% 39 / 42 Branchbran 77.7% 14 / 18 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 4 totaltotal 80.4% 66 / 82 SCAR 25.1
CC
12
Cov
86%
read
11.6
new
2.0
get_read_fh
2.0
CRAP 12.4
« Framed/Write.pm ↑ Summary Framed.pm »
1 package IO::Framed::Read;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use IO::SigGuard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use IO::Framed::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new {
11 2 ▶ my ( $class, $in_fh, $initial_buffer ) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
12  
13 2 ▶ if ( !defined $initial_buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $initial_buffer) 2 0 2
14 2 $initial_buffer = q<>;
15 }
16  
17 2 my $self = {
18 _in_fh => $in_fh,
19 _read_buffer => $initial_buffer,
20 _bytes_to_read => 0,
21 };
22  
23 2 return bless $self, $class;
24 }
25  
26 0 ▶sub get_read_fh { return $_[0]->{'_in_fh'} }
Subroutine get_read_fh: not called CC=1 SCAR=6.9
Pod undocumented
27  
28 #----------------------------------------------------------------------
29 # IO subclass interface
30  
31 sub allow_empty_read {
32 1 ▶ my ($self) = @_;
Subroutine allow_empty_read: called CC=1 SCAR=0.0
Pod undocumented
33 1 $self->{'_ALLOW_EMPTY_READ'} = 1;
34 1 return $self;
35 }
36  
37 1 my $buf_len;
38  
39 #We assume here that whatever read may be incomplete at first
40 #will eventually be repeated so that we can complete it. e.g.:
41 #
42 # - read 4 bytes, receive 1, cache it - return q<>
43 # - select()
44 # - read 4 bytes again; since we already have 1 byte, only read 3
45 # … and now we get the remaining 3, so return the buffer.
46 #
47 sub read {
48 7 ▶ my ( $self, $bytes ) = @_;
Subroutine read: called CC=11 SCAR=24.5
Pod undocumented
49  
50 7 ▶ die "I refuse to read zero!" if !$bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless $bytes 0 7 7
51  
52 7 ▶ if ( $buf_len = length $self->{'_read_buffer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf_len = length $self->{"_read_buffer"}) 1 6 7
53 1 ▶ if ( $buf_len + $self->{'_bytes_to_read'} != $bytes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf_len + $self->{"_bytes_to_read"} != $bytes) 0 1 1
54 0 my $should_be = $buf_len + $self->{'_bytes_to_read'};
55 0 die "Continuation: should want “$should_be” bytes, not $bytes!";
56 }
57 }
58  
59 7 ▶ if ( $bytes > $buf_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes > $buf_len) 7 0 7
60 7 $bytes -= $buf_len;
61  
62 7 local $!;
63  
64 7 ▶ $bytes -= IO::SigGuard::sysread( $self->{'_in_fh'}, $self->{'_read_buffer'}, $bytes, $buf_len ) || do {
Condition66%2/3
IO::SigGuard::sysread($self->{'_in_fh'}, $self->{'_read_buffer'}, $bytes, $buf_len) || do { if ($!) { if (not $!{'EAGAIN'} || $!{'EWOULDBLOCK'}) { die 'IO::Framed::X'->create('ReadError', $!); } ; } elsif ($self->{'_ALLOW_EMPTY_READ'}) { return ''; } else { die 'IO::Framed::X'->create('EmptyRead'); } }
l!l&&r!l&&!r
304
MC/DC50%1/2
IO::SigGuard::sysread($self->{'_in_fh'}, $self->{'_read_buffer'}, $bytes, $buf_len) || do { if ($!) { if (not $!{'EAGAIN'} || $!{'EWOULDBLOCK'}) { die 'IO::Framed::X'->create('ReadError', $!); } ; } elsif ($self->{'_ALLOW_EMPTY_READ'}) { return ''; } else { die 'IO::Framed::X'->create('EmptyRead'); } }
IO::SigGuard::sysread($self->{'_in_fh'}, $self->{'_read_buffer'}, $bytes, $buf_len) do { if ($!) { if (not $!{'EAGAIN'} || $!{'EWOULDBLOCK'}) { die 'IO::Framed::X'->create('ReadError', $!); } ; } elsif ($self->{'_ALLOW_EMPTY_READ'}) { return ''; } else { die 'IO::Framed::X'->create('EmptyRead'); } }
Truth table66%2/3
A || B
A= IO::SigGuard::sysread($self->{'_in_fh'}, $self->{'_read_buffer'}, $bytes, $buf_len)B= do { if ($!) { if (not $!{'EAGAIN'} || $!{'EWOULDBLOCK'}) { die 'IO::Framed::X'->create('ReadError', $!); } ; } elsif ($self->{'_ALLOW_EMPTY_READ'}) { return ''; } else { die 'IO::Framed::X'->create('EmptyRead'); } }
ABresult
1X1
011
000
65 4 ▶ if ($!) {
Branch: 2 branches
BranchTrueFalseTotal
if ($!) { } 2 2 4
elsif ($self->{'_ALLOW_EMPTY_READ'}) { } 1 1 2
66 2 ▶ if ( !$!{'EAGAIN'} && !$!{'EWOULDBLOCK'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $!{"EAGAIN"} || $!{"EWOULDBLOCK"}) 1 1 2
Condition66%2/3
$!{'EAGAIN'} || $!{'EWOULDBLOCK'}
l!l&&r!l&&!r
101
MC/DC50%1/2
$!{'EAGAIN'} || $!{'EWOULDBLOCK'}
$!{'EAGAIN'} $!{'EWOULDBLOCK'}
Truth table66%2/3
A || B
A= $!{'EAGAIN'}B= $!{'EWOULDBLOCK'}
ABresult
1X1
011
000
67 1 die IO::Framed::X->create( 'ReadError', $! );
68 }
69 }
70 elsif ($self->{'_ALLOW_EMPTY_READ'}) {
71 1 return q<>;
72 }
73 else {
74 1 die IO::Framed::X->create('EmptyRead');
75 }
76 };
77 }
78  
79 4 $self->{'_bytes_to_read'} = $bytes;
80  
81 4 ▶ if ($bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes) 2 2 4
82 2 return undef;
83 }
84  
85 2 return substr( $self->{'_read_buffer'}, 0, length($self->{'_read_buffer'}), q<> );
86 }
87  
88 #----------------------------------------------------------------------
89  
90 1 1;
« Framed/Write.pm ↑ Summary Framed.pm »