Handle.pm

filter: Statementstmt 96.9% 159 / 164 Branchbran 90.9% 40 / 44 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 37 / 37 Podpod 100% 1 / 1 totaltotal 95.0% 248 / 261 CC 26 SCAR 8.9
CC
26
Cov
95%
_seek
5.2
_is_valid_data_tell
5.0
_read
4.0
CRAP 2.4
« Handle/Exception.pm ↑ Summary Handle/IO.pm »
1 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Data::Handle;
6  
7 5 our $VERSION = '1.000001';
8  
9 # ABSTRACT: A Very simple interface to the __DATA__ file handle.
10  
11 5 our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97 5 my %datastash;
98 5 ▶use Symbol qw( gensym );
Subroutine BEGIN: called CC=1 SCAR=0.0
99 5 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
100 5 ▶use parent qw( IO::File );
Subroutine BEGIN: called CC=1 SCAR=0.0
101 5 ▶use Package::Stash 0.15; # has_symbol
Subroutine BEGIN: called CC=1 SCAR=0.0
102 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
103 5 ▶use Data::Handle::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 5 ▶use Data::Handle::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 5 ▶use Try::Tiny qw( try catch );
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107  
108  
109  
110  
111  
112  
113  
114  
115 28 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
116 28 my ( $class, $targetpackage ) = @_;
117  
118 28 ▶ _e('NoSymbol')->throw("$targetpackage has no DATA symbol")
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_has_data_symbol($targetpackage) 1 27 28
119 if ( !$class->_has_data_symbol($targetpackage) );
120  
121 27 ▶ if ( !$class->_is_valid_data_tell($targetpackage) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_valid_data_tell($targetpackage)) 1 26 27
122 1 _e('BadFilePos')
123 ->throw( "$targetpackage has a DATA symbol, but the filepointer"
124 . " is well beyond the __DATA__ section.\n"
125 . " We can't work out safely where it is.\n"
126 . $class->_stringify_metadata($targetpackage)
127 . "\n" );
128 }
129  
130 26 my $sym = gensym();
131 26 my $xsym = $sym;
132 26 weaken($xsym);
133  
134 ## no critic( ProhibitTies )
135 26 tie *{$sym}, 'Data::Handle::IO', { self => $xsym };
136 26 ${ *{$sym} }{stash} = {};
137 26 bless $sym, $class;
138 26 $sym->_stash->{start_offset} = $class->_get_start_offset($targetpackage);
139 26 $sym->_stash->{targetpackage} = $targetpackage;
140 26 $sym->_stash->{current_offset} = $class->_get_start_offset($targetpackage);
141 26 $sym->_stash->{filehandle} = $class->_get_data_symbol($targetpackage);
142 26 return $sym;
143  
144 }
145  
146 73 ▶sub _has_data_symbol {
Subroutine _has_data_symbol: called CC=1 SCAR=0.0
147 73 my ( undef, $package ) = @_;
148 73 my $rval = undef;
149 73 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
150 73 my $stash = Package::Stash->new($package);
151 70 ▶ return unless $stash->has_symbol('DATA');
Branch: 1 branch
BranchTrueFalseTotal
unless $stash->has_symbol("DATA") 1 69 70
152 69 my $fh = $stash->get_symbol('DATA');
153 69 $rval = defined fileno *{$fh};
154 }
155 3 ▶ catch {
Subroutine __ANON__: called CC=2 SCAR=7.6
156 3 ▶ if (/is not a module name/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/is not a module name/) 3 0 3
157 3 $rval = undef;
158 3 return;
159 }
160 ## no critic (RequireCarping)
161 0 die $_;
162 73 };
163 73 return $rval;
164 }
165  
166 35 ▶sub _get_data_symbol {
Subroutine _get_data_symbol: called CC=2 SCAR=6.9
167 35 my ( $self, $package ) = @_;
168 35 ▶ if ( !$self->_has_data_symbol($package) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_data_symbol($package)) 1 34 35
169 1 _e('Internal::BadGet')->throw('_get_data_symbol was called when there is no data_symbol to get');
170 }
171 34 return Package::Stash->new($package)->get_symbol('DATA');
172 }
173  
174 57 ▶sub _get_start_offset {
Subroutine _get_start_offset: called CC=3 SCAR=11.0
175 57 my ( $self, $package ) = @_;
176  
177 return $datastash{$package}->{offset}
178 57 ▶ if ( exists $datastash{$package}->{offset} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $datastash{$package}{"offset"} 52 5 57
179  
180 5 ▶ if ( !$self->_has_data_symbol($package) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_data_symbol($package)) 1 4 5
181 1 _e('Internal::BadGet')->throw('_get_start_offset was called when there is no data_symbol to get');
182 }
183 4 my $fd = $self->_get_data_symbol($package);
184 4 my $position = tell $fd;
185  
186 4 $datastash{$package}->{offset} = $position;
187  
188 4 return $position;
189 }
190  
191 28 ▶sub _is_valid_data_tell {
Subroutine _is_valid_data_tell: called CC=5 SCAR=16.1
192 28 my ( $self, $package ) = @_;
193 return 1
194 28 ▶ if ( exists $datastash{$package} && 1 == $datastash{$package}->{valid} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $datastash{$package} and 1 == $datastash{$package}{"valid"} 23 5 28
Condition66%2/3
exists $datastash{$package} and 1 == $datastash{$package}{"valid"}
!ll&&!rl&&r
5023
MC/DC50%1/2
exists $datastash{$package} and 1 == $datastash{$package}{"valid"}
exists $datastash{$package} 1 == $datastash{$package}{"valid"}
Truth table66%2/3
A and B
A= exists $datastash{$package}B= 1 == $datastash{$package}{"valid"}
ABresult
0X0
100
111
195 5 ▶ if ( !$self->_has_data_symbol($package) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_data_symbol($package)) 1 4 5
196 1 _e('Internal::BadGet')->throw('_is_valid_data_tell was called when there is no data_symbol to get');
197 }
198  
199 4 my $fh = $self->_get_data_symbol($package);
200 4 my $offset = $self->_get_start_offset($package);
201  
202 # The offset to the start of __DATA__ is 9 bytes because it includes the
203 # trailing \n
204 #
205 4 my $checkfor = qq{__DATA__\n};
206 4 seek $fh, ( $offset - length $checkfor ), 0;
207 4 read $fh, my ($buffer), length $checkfor;
208 4 seek $fh, $offset, 0;
209  
210 4 $datastash{$package}->{previous_bytes} = $buffer;
211  
212 4 ▶ if ( $buffer eq $checkfor ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer eq $checkfor) { } 3 1 4
213 3 $datastash{$package}->{valid} = 1;
214 3 return 1;
215 }
216 else {
217 1 $datastash{$package}->{valid} = 0;
218 1 return;
219 }
220 }
221  
222 1 ▶sub _stringify_metadata {
Subroutine _stringify_metadata: called CC=2 SCAR=7.3
223 1 my ( undef, $package ) = @_;
224 1 my @lines = ();
225 1 ▶ if ( not exists $datastash{$package} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $datastash{$package}) { } 0 1 1
226 0 push @lines, "Nothing known about $package\n";
227 0 return join "\n", @lines;
228 }
229 else {
230 1 push @lines, q{Offset : } . $datastash{$package}->{offset};
231 1 push @lines, q{Prelude : '} . $datastash{$package}->{previous_bytes} . q{'};
232 1 push @lines, q{Valid: } . $datastash{$package}->{valid};
233 1 return join "\n", @lines;
234 }
235 }
236  
237 22 ▶sub _readline {
Subroutine _readline: called CC=3 SCAR=11.0
238 22 my ( $self, @args ) = @_;
239  
240 22 ▶ _e('API::Invalid::Params')->throw('_readline() takes no parameters') if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 1 21 22
241  
242 21 my $fh = $self->_fh;
243 21 $self->_restore_pos();
244 21 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 1 20 21
245 1 my @result = <$fh>;
246 1 $self->_set_pos();
247 1 return @result;
248 }
249 20 my $result = <$fh>;
250 20 $self->_set_pos();
251 20 return $result;
252 }
253  
254 5 ▶sub _read {
Subroutine _read: called CC=4 SCAR=13.9
255 5 my ( $self, undef, $len, $offset ) = @_;
256  
257 ## no critic ( ProhibitMagicNumbers )
258 5 ▶ _e('API::Invalid::Params')->throw('_read() takes 2 or 3 parameters.')
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ < 3 or scalar @_ > 4 2 3 5
Condition100%3/3
scalar @_ < 3 or scalar @_ > 4
l!l&&r!l&&!r
113
MC/DC100%2/2
scalar @_ < 3 or scalar @_ > 4
scalar @_ < 3 scalar @_ > 4
Truth table100%3/3
A or B
A= scalar @_ < 3B= scalar @_ > 4
ABresult
1X1
011
000
259 if ( scalar @_ < 3 or scalar @_ > 4 );
260  
261 3 $self->_restore_pos();
262 3 my $return;
263 3 ▶ if ( defined $offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offset) { } 2 1 3
264 2 $return = read $self->_fh, $_[1], $len, $offset;
265 }
266 else {
267 1 $return = read $self->_fh, $_[1], $len;
268 }
269 3 $self->_set_pos();
270 3 return $return;
271 }
272  
273 37 ▶sub _getc {
Subroutine _getc: called CC=2 SCAR=6.9
274 37 my ($self) = @_;
275 37 ▶ _e('API::Invalid::Params')->throw('_get() takes 0 parameters.')
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 1 1 36 37
276 if scalar @_ > 1;
277 36 $self->_restore_pos();
278 36 my $return = getc $self->_fh;
279 36 $self->_set_pos();
280 36 return $return;
281 }
282  
283 8 ▶sub _seek {
Subroutine _seek: called CC=5 SCAR=16.6
284 8 my ( $self, $position, $whence ) = @_;
285  
286 ## no critic ( ProhibitMagicNumbers )
287  
288 8 ▶ _e('API::Invalid::Params')->throw('_seek() takes 2 params.')
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 3 1 7 8
289 if scalar @_ != 3;
290  
291 7 my $fh = $self->_stash->{filehandle};
292  
293 7 ▶ if ( 0 == $whence ) {
Branch: 3 branches
BranchTrueFalseTotal
if (0 == $whence) { } 6 1 7
elsif (1 == $whence) { } 0 1 1
elsif (2 == $whence) { } 0 1 1
294 6 $position = $self->_stash->{start_offset} + $position;
295 }
296 elsif ( 1 == $whence ) {
297 0 $whence = 0;
298 0 $position = $self->_stash->{current_offset} + $position;
299 }
300 elsif ( 2 == $whence ) {
301 }
302 else {
303 1 _e('API::Invalid::Whence')->throw('Expected whence values are 0,1,2');
304 }
305 6 my $return = seek $fh, $position, $whence;
306 6 $self->_set_pos();
307 6 return $return;
308 }
309  
310 3 ▶sub _tell {
Subroutine _tell: called CC=2 SCAR=6.9
311 3 my ($self) = shift;
312 3 ▶ _e('API::Invalid::Params')->throw('_tell() takes no params.') if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 2 3
313 2 return $self->_stash->{current_offset} - $self->_stash->{start_offset};
314 }
315  
316 43 ▶sub _eof {
Subroutine _eof: called CC=3 SCAR=11.0
317 43 my $self = shift;
318 43 ▶ _e('API::Invalid::Params')->throw("_eof() takes no params : @_ ")
Branch: 1 branch
BranchTrueFalseTotal
if @_ and $_[0] != 1 1 42 43
Condition66%2/3
@_ and $_[0] != 1
!ll&&!rl&&r
0421
MC/DC50%1/2
@_ and $_[0] != 1
@_ $_[0] != 1
Truth table66%2/3
A and B
A= @_B= $_[0] != 1
ABresult
0X0
100
111
319 if @_ && $_[0] != 1;
320 42 $self->_restore_pos();
321 42 return eof $self->_stash->{filehandle};
322 }
323  
324 102 ▶sub _restore_pos {
Subroutine _restore_pos: called CC=1 SCAR=0.0
325 102 my $self = shift;
326 102 return seek $self->_stash->{filehandle}, $self->_stash->{current_offset}, 0;
327 }
328  
329 66 ▶sub _set_pos {
Subroutine _set_pos: called CC=1 SCAR=0.0
330 66 my $self = shift;
331 66 return ( $self->_stash->{current_offset} = tell $self->_stash->{filehandle} );
332 }
333  
334 559 ▶sub _stash { return ${ *{ $_[0] } }{stash} }
Subroutine _stash: called CC=1 SCAR=0.0
335 1 ▶sub _fileno { return }
Subroutine _fileno: called CC=1 SCAR=0.0
336 25 ▶sub _e { return 'Data::Handle::Exception::' . shift }
Subroutine _e: called CC=1 SCAR=0.0
337 60 ▶sub _fh { return shift->_stash->{filehandle} }
Subroutine _fh: called CC=1 SCAR=0.0
338  
339 2 ▶sub _binmode {
Subroutine _binmode: called CC=1 SCAR=0.0
340 2 return _e('API::NotImplemented')->throw('_binmode() is difficult on Data::Handle and not implemented yet.');
341 }
342  
343 2 ▶sub _open {
Subroutine _open: called CC=1 SCAR=0.0
344 2 return _e('API::Invalid')->throw('_open() is invalid on Data::Handle.');
345 }
346  
347 2 ▶sub _close {
Subroutine _close: called CC=1 SCAR=0.0
348 2 return _e('API::Invalid')->throw('_close() is invalid on Data::Handle');
349 }
350  
351 2 ▶sub _printf {
Subroutine _printf: called CC=1 SCAR=0.0
352 2 return _e('API::Invalid')->throw('_printf() is invalid on Data::Handle.');
353 }
354  
355 2 ▶sub _print {
Subroutine _print: called CC=1 SCAR=0.0
356 2 return _e('API::Invalid')->throw('_print() is invalid on Data::Handle.');
357 }
358  
359 2 ▶sub _write {
Subroutine _write: called CC=1 SCAR=0.0
360 2 return _e('API::Invalid')->throw('_write() is invalid on Data::Handle.');
361 }
362  
363  
364  
365  
366  
367  
368  
369  
370  
371  
372  
373 5 1;
374  
375 __END__
« Handle/Exception.pm ↑ Summary Handle/IO.pm »