Source/Base/Handle.pm

filter: Statementstmt 94.2% 49 / 52 Branchbran 75.0% 9 / 12 Conditioncond 66.6% 12 / 18 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 87.5% 14 / 16 Podpod 71.4% 5 / 7 totaltotal 80.7% 92 / 114 CC 14 SCAR 13.4
CC
14
Cov
85%
_read_too_short
7.9
_read
6.0
close
3.0
CRAP 3.8
« Source/Base.pm ↑ Summary Source/Base/Proc.pm »
1 package Crypt::Random::Source::Base::Handle;
2 # ABSTRACT: L<IO::Handle> based random data sources
3  
4 6 our $VERSION = '0.14';
5  
6 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Types::Standard qw(Bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 extends qw(Crypt::Random::Source::Base);
16  
17 6 has allow_under_read => (
18 isa => Bool,
19 is => "rw",
20 default => 0,
21 );
22  
23 6 has reread_attempts => (
24 is => "rw",
25 default => 1,
26 );
27  
28 6 has handle => (
29 is => "rw",
30 lazy => 1,
31 builder => 1,
32 predicate => "has_handle",
33 clearer => "clear_handle",
34 );
35  
36 2 ▶sub blocking { shift->handle->blocking(@_) }
Subroutine blocking: called CC=1 SCAR=0.0
Pod documented
37 12 ▶sub read { shift->handle->read(@_) }
Subroutine read: called CC=1 SCAR=0.0
Pod documented
38 0 ▶sub opened { shift->handle->opened(@_) }
Subroutine opened: not called CC=1 SCAR=6.9
Pod undocumented
39  
40 9 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=1 SCAR=0.0
Pod undocumented
41 9 my $self = shift;
42 9 $self->close;
43 }
44  
45 4 ▶sub _build_handle {
Subroutine _build_handle: called CC=1 SCAR=0.0
46 4 my ( $self, @args ) = @_;
47 4 $self->open_handle;
48 }
49  
50 0 ▶sub open_handle {
Subroutine open_handle: not called CC=1 SCAR=6.9
Pod documented
51 0 die "open_handle is an abstract method";
52 }
53  
54 12 ▶sub get {
Subroutine get: called CC=2 SCAR=7.1
Pod documented
55 12 my ( $self, $n, @args ) = @_;
56  
57 12 ▶ croak "How many bytes would you like to read?" unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 12 12
58  
59 12 return $self->_read($self->handle, $n, @args);
60 }
61  
62 12 ▶sub _read {
Subroutine _read: called CC=6 SCAR=18.0
63 12 my ( $self, $handle, $n, @args) = @_;
64  
65 12 my $buf;
66 12 my $got = $self->read($buf, $n);
67  
68 12 ▶ if ( defined($got) && $got == $n || $!{EWOULDBLOCK} || $!{EAGAIN} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $got and $got == $n or $!{'EWOULDBLOCK'} or $!{'EAGAIN'}) { } 9 3 12
Condition88%8/9
defined $got and $got == $n or $!{"EWOULDBLOCK"} or $!{"EAGAIN"}
l!l&&r!l&&!r
903
defined $got and $got == $n or $!{"EWOULDBLOCK"}
l!l&&r!l&&!r
723
defined $got and $got == $n
!ll&&!rl&&r
147
MC/DC50%2/4
defined $got and $got == $n or $!{"EWOULDBLOCK"} or $!{"EAGAIN"}
defined $got $got == $n $!{"EWOULDBLOCK"} $!{"EAGAIN"}
Truth table57%4/7
A and B or C or D
A= defined $gotB= $got == $nC= $!{"EWOULDBLOCK"}D= $!{"EAGAIN"}
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
69 9 return $buf;
70 } else {
71 3 ▶ croak "read error: $!" unless defined $got;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 0 3 3
72 3 return $self->_read_too_short($buf, $got, $n, @args);
73 }
74 }
75  
76 3 ▶sub _read_too_short {
Subroutine _read_too_short: called CC=6 SCAR=20.7
77 3 my ( $self, $buf, $got, $req, %args ) = @_;
78  
79 3 ▶ if ( $self->allow_under_read ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->allow_under_read) { } 1 2 3
80 1 return $buf;
81 } else {
82 2 ▶ if ( ($self->reread_attempts || 0) >= ($args{reread_attempt} || 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->reread_attempts || 0) >= ($args{'reread_attempt'} || 0)) { } 2 0 2
Condition50%2/4
$self->reread_attempts || 0
l!l
20
$args{'reread_attempt'} || 0
l!l
02
MC/DC0%0/1
$self->reread_attempts || 0
$self->reread_attempts
MC/DC0%0/1
$args{'reread_attempt'} || 0
$args{'reread_attempt'}
Truth table50%1/2
A
A= $self->reread_attempts
Aresult
11
00
Truth table50%1/2
A
A= $args{'reread_attempt'}
Aresult
11
00
83 2 croak "Source failed to read enough bytes (requested $req, got $got)";
84 } else {
85 0 ▶ return $buf . $self->_read( $req - $got, reread_attempt => 1 + ( $args{reread_attempt} || 0 ) );
Condition0%0/2
$args{'reread_attempt'} || 0
l!l
00
MC/DC0%0/1
$args{'reread_attempt'} || 0
$args{'reread_attempt'}
Truth table0%0/2
A
A= $args{'reread_attempt'}
Aresult
11
00
86 }
87 }
88 }
89  
90 9 ▶sub close {
Subroutine close: called CC=3 SCAR=11.0
Pod documented
91 9 my $self = shift;
92  
93 # During global destruction, $self->handle can be undef already,
94 # so we need to also check if it is defined.
95 9 ▶ if ( $self->has_handle and $self->handle ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_handle and $self->handle) 6 3 9
Condition66%2/3
$self->has_handle and $self->handle
!ll&&!rl&&r
306
MC/DC50%1/2
$self->has_handle and $self->handle
$self->has_handle $self->handle
Truth table66%2/3
A and B
A= $self->has_handleB= $self->handle
ABresult
0X0
100
111
96 6 $self->handle->close; # or die "close: $!"; # open "-|" returns exit status on close
97 6 $self->clear_handle;
98 }
99 }
100  
101 6 1;
102  
103 =pod
104  
105 =encoding UTF-8
106  
107 =head1 NAME
108  
109 Crypt::Random::Source::Base::Handle - L<IO::Handle> based random data sources
110  
111 =head1 VERSION
112  
113 version 0.14
114  
115 =head1 SYNOPSIS
116  
117 use Moo;
118 extends qw(Crypt::Random::Source::Base::Handle);
119  
120 sub open_handle {
121 # invoked as needed
122 }
123  
124  
125 # this class can also be used directly
126 Crypt::Random::Source::Base::Handle->new( handle => $file_handle );
127  
128  
129 # it supports some standard methods:
130  
131 $p->blocking(0);
132  
133 $p->read( my $buf, $n ); # no error handling here
134  
135 =head1 DESCRIPTION
136  
137 This is a concrete base class for all L<IO::Handle> based random data sources.
138  
139 It implements error handling
140  
141 =head1 ATTRIBUTES
142  
143 =head2 handle
144  
145 An L<IO::Handle> or file handle to read from.
146  
147 =head2 blocking
148  
149 This is actually handled by C<handle>, and is documented in L<IO::Handle>.
150  
151 =head2 allow_under_read
152  
153 Whether or not under reading is considered an error.
154  
155 Defaults to false.
156  
157 =head2 reread_attempts
158  
159 The number of attempts to make at rereading if the handle did not provide
160 enough bytes on the first attempt.
161  
162 Defaults to 1.
163  
164 Only used if C<allow_under_read> is enabled.
165  
166 =head1 METHODS
167  
168 =head2 get
169  
170 See L<Crypt::Random::Source::Base/get>.
171  
172 When C<blocking> or C<allow_under_read> are set to a true value this method may
173 return fewer bytes than requested.
174  
175 =head2 read
176  
177 This delegates directly to C<handle>.
178  
179 It B<DOES NOT> provide the same validation as C<get> would have, so no checking
180 for underreads is done.
181  
182 =head2 close
183  
184 Close the handle and clear it.
185  
186 =head2 _read
187  
188 C<< $self->handle->read >> but with additional error checking and different
189 calling conventions.
190  
191 =head2 _read_too_short
192  
193 Called by C<_read> when not enough data was read from the handle. Normally it
194 will either die with an error or attempt to reread. When C<allow_under_read> is
195 true it will just return the partial buffer.
196  
197 =head2 open_handle
198  
199 Abstract method, should return an L<IO::Handle> to use.
200  
201 =head1 SUPPORT
202  
203 Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Crypt-Random-Source>
204 (or L<bug-Crypt-Random-Source@rt.cpan.org|mailto:bug-Crypt-Random-Source@rt.cpan.org>).
205  
206 =head1 AUTHOR
207  
208 יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
209  
210 =head1 COPYRIGHT AND LICENCE
211  
212 This software is copyright (c) 2008 by Yuval Kogman.
213  
214 This is free software; you can redistribute it and/or modify it under
215 the same terms as the Perl 5 programming language system itself.
216  
217 =cut
218  
219 __END__
« Source/Base.pm ↑ Summary Source/Base/Proc.pm »