blib/lib/FileHandle/Unget.pm

filter: Statementstmt 84.7% 262 / 309 Branchbran 58.8% 53 / 90 Conditioncond 65.3% 17 / 26 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 89.7% 35 / 39 Podpod 87.5% 7 / 8 totaltotal 78.0% 381 / 488 CC 57 SCAR 21.0
CC
57
Cov
78%
seek
30.7
getlines
12.4
getline
12.3
CRAP 8.2
↑ Summary
1 package FileHandle::Unget;
2  
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 24 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 24 ▶use vars qw( @ISA $VERSION $AUTOLOAD @EXPORT @EXPORT_OK );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 24 @ISA = qw( Exporter FileHandle );
14  
15 24 $VERSION = sprintf "%d.%02d%02d", q/0.16.34/ =~ /(\d+)/g;
16  
17 24 @EXPORT = @FileHandle::EXPORT;
18 24 @EXPORT_OK = @FileHandle::EXPORT_OK;
19  
20 # Based on dump_methods from this most helpful post by MJD:
21 # http://groups.google.com/groups?selm=20020621182734.15920.qmail%40plover.com
22 # We can't just use AUTOLOAD because AUTOLOAD is not called for inherited
23 # methods
24 sub wrap_methods
25 168 ▶{
Subroutine wrap_methods: called CC=9 SCAR=22.0
Pod undocumented
26 24 ▶ no strict 'refs'; ## no critic (strict)
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 168 ▶ my $class = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $class = shift() or return 168 0 168
29 168 ▶ my $seen = shift || {};
Condition100%2/2
shift() || {}
l!l
14424
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
30  
31 # Locate methods in this class
32 168 my $symtab = \%{"$class\::"};
33 168 my @names = keys %$symtab;
34 168 for my $method (keys %$symtab)
35 {
36 5784 my $fullname = "$class\::$method";
37  
38 5784 ▶ next unless defined &$fullname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &$fullname 1584 4200 5784
39 4200 ▶ next if defined &{__PACKAGE__ . "::$method"};
Branch: 1 branch
BranchTrueFalseTotal
if defined &{'FileHandle::Unget' . "::$method";} 2520 1680 4200
40 1680 ▶ next if $method eq 'import';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "import" 96 1584 1680
41  
42 1584 ▶ unless ($seen->{$method})
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen->{$method}) 1584 0 1584
43 {
44 1584 $seen->{$method} = $fullname;
45  
46 1584 *{$method} = sub
47 183 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
48 183 my $self = $_[0];
49  
50 183 ▶ if (ref $self eq __PACKAGE__)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq __PACKAGE__) { } 181 2 183
51 {
52 181 shift @_;
53 181 my $super = "SUPER::$method";
54 181 $self->$super(@_);
55 }
56 else
57 {
58 2 $method = "FileHandle::$method";
59 2 &$method(@_);
60 }
61 1584 };
62 }
63 }
64  
65 # Traverse parent classes of this one
66 168 my @ISA = @{"$class\::ISA"};
67 168 for my $class (@ISA)
68 {
69 144 wrap_methods($class, $seen);
70 }
71 }
72  
73 24 wrap_methods('FileHandle');
74  
75 #-------------------------------------------------------------------------------
76  
77 sub DESTROY
78 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
79 }
80  
81 #-------------------------------------------------------------------------------
82  
83 sub new
84 49 ▶{
Subroutine new: called CC=4 SCAR=13.9
Pod documented
85 49 my $class = shift;
86  
87 49 my $self;
88  
89 49 ▶ if (defined $_[0] && defined fileno $_[0])
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and defined CORE::fileno $_[0]) { } 13 36 49
Condition100%3/3
defined $_[0] and defined CORE::fileno $_[0]
!ll&&!rl&&r
92713
MC/DC100%2/2
defined $_[0] and defined CORE::fileno $_[0]
defined $_[0] defined CORE::fileno $_[0]
Truth table100%3/3
A and B
A= defined $_[0]B= defined CORE::fileno $_[0]
ABresult
0X0
100
111
90 {
91 13 $self = shift;
92 }
93 else
94 {
95 36 $self = $class->SUPER::new(@_);
96 36 ▶ return undef unless defined $self; ## no critic (ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 36 36
97 }
98  
99 49 my $values =
100 {
101 'fh' => $self,
102 'eof_called' => 0,
103 'filehandle_unget_buffer' => '',
104 };
105  
106 49 weaken($values->{'fh'});
107
108 49 tie *$self, "${class}::Tie", $values;
109  
110 49 bless $self, $class;
111 49 return $self;
112 }
113  
114 #-------------------------------------------------------------------------------
115  
116 sub new_from_fd
117 1 ▶{
Subroutine new_from_fd: called CC=2 SCAR=6.9
Pod documented
118 1 my $class = shift;
119  
120 1 my $self;
121  
122 # if (defined $_[0] && defined fileno $_[0])
123 # {
124 # $self = shift;
125 # }
126 # else
127 {
128 1 $self = $class->SUPER::new_from_fd(@_);
129 1 ▶ return undef unless defined $self; ## no critic (ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 1 1
130 }
131  
132 1 my $values =
133 {
134 'fh' => $self,
135 'eof_called' => 0,
136 'filehandle_unget_buffer' => '',
137 };
138  
139 1 weaken($values->{'fh'});
140
141 1 tie *$self, "${class}::Tie", $values;
142  
143 1 bless $self, $class;
144 1 return $self;
145 }
146  
147 #-------------------------------------------------------------------------------
148  
149 sub ungetc
150 23 ▶{
Subroutine ungetc: called CC=1 SCAR=0.0
Pod documented
151 23 my $self = shift;
152 23 my $ord = shift;
153  
154 23 substr(tied(*$self)->{'filehandle_unget_buffer'},0,0) = chr($ord);
155 }
156  
157 #-------------------------------------------------------------------------------
158  
159 sub ungets
160 12 ▶{
Subroutine ungets: called CC=1 SCAR=0.0
Pod documented
161 12 my $self = shift;
162 12 my $string = shift;
163  
164 12 substr(tied(*$self)->{'filehandle_unget_buffer'},0,0) = $string;
165 }
166  
167 #-------------------------------------------------------------------------------
168  
169 sub buffer
170 3 ▶{
Subroutine buffer: called CC=2 SCAR=6.9
Pod documented
171 3 my $self = shift;
172  
173 3 ▶ tied(*$self)->{'filehandle_unget_buffer'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 2 3
174 3 return tied(*$self)->{'filehandle_unget_buffer'};
175 }
176  
177 #-------------------------------------------------------------------------------
178  
179 sub input_record_separator
180 3 ▶{
Subroutine input_record_separator: called CC=3 SCAR=11.3
Pod documented
181 3 my $self = shift;
182  
183 3 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 0 3
184 {
185 3 tied(*$self)->{'input_record_separator'} = shift;
186 }
187  
188 3 ▶ return undef unless exists tied(*$self)->{'input_record_separator'}; ## no critic (ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
unless exists tied(*$self)->{"input_record_separator"} 0 3 3
189 3 return tied(*$self)->{'input_record_separator'};
190 }
191  
192 #-------------------------------------------------------------------------------
193  
194 sub clear_input_record_separator
195 1 ▶{
Subroutine clear_input_record_separator: called CC=1 SCAR=0.0
Pod documented
196 1 my $self = shift;
197  
198 1 delete tied(*$self)->{'input_record_separator'};
199 }
200  
201 ###############################################################################
202  
203 package FileHandle::Unget::Tie;
204  
205 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
206 24 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
207 24 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
208  
209 24 ▶use 5.000;
Subroutine BEGIN: called CC=1 SCAR=0.0
210  
211 24 ▶use vars qw( $VERSION $AUTOLOAD @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
212  
213 24 @ISA = qw( IO::Handle );
214  
215 24 $VERSION = '0.10';
216  
217 #-------------------------------------------------------------------------------
218  
219 24 my %tie_mapping = (
220 PRINT => 'print', PRINTF => 'printf', WRITE => 'syswrite',
221 READLINE => 'getline_wrapper', GETC => 'getc', READ => 'read', CLOSE => 'close',
222 BINMODE => 'binmode', OPEN => 'open', EOF => 'eof', FILENO => 'fileno',
223 SEEK => 'seek', TELL => 'tell', FETCH => 'fetch',
224 );
225  
226 #-------------------------------------------------------------------------------
227  
228 sub AUTOLOAD
229 61 ▶{
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
230 61 my $name = $AUTOLOAD;
231 61 $name =~ s/.*://;
232  
233 61 ▶ die "Unhandled function $name!" unless exists $tie_mapping{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tie_mapping{$name} 0 61 61
234  
235 61 my $sub = $tie_mapping{$name};
236  
237 # Alias the anonymous subroutine to the name of the sub we want ...
238 24 ▶ no strict 'refs'; ## no critic (strict)
Subroutine BEGIN: called CC=1 SCAR=0.0
239 61 *{$name} = sub
240 146 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
241 146 my $self = shift;
242  
243 146 ▶ if (defined &$sub)
Branch: 1 branch
BranchTrueFalseTotal
if (defined &$sub) { } 95 51 146
244 {
245 95 &$sub($self,@_);
246 }
247 else
248 {
249 # Prevent recursion
250 # Temporarily disable warnings so that we don't get "untie attempted
251 # while 1 inner references still exist". Not sure what's the "right
252 # thing" to do here.
253 {
254 51 local $^W = 0;
255 51 untie *{$self->{'fh'}};
256 }
257  
258 51 $self->{'fh'}->$sub(@_);
259  
260 51 tie *{$self->{'fh'}}, __PACKAGE__, $self;
261 }
262 61 };
263  
264 # ... and go to it.
265 61 goto &$name;
266 }
267  
268 #-------------------------------------------------------------------------------
269  
270 sub DESTROY
271 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
272 }
273  
274 #-------------------------------------------------------------------------------
275  
276 sub TIEHANDLE
277 196 ▶{
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
278 196 my $class = shift;
279 196 my $self = shift;
280  
281 196 bless($self, $class);
282  
283 196 return $self;
284 }
285  
286 #-------------------------------------------------------------------------------
287  
288 sub binmode
289 2 ▶{
Subroutine binmode: called CC=3 SCAR=11.1
290 2 my $self = shift;
291  
292 warn "Under windows, calling binmode after eof exposes a bug that exists in some versions of Perl.\n"
293 2 ▶ if $self->{'eof_called'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eof_called"} 1 1 2
294  
295 # Prevent recursion
296 # Temporarily disable warnings so that we don't get "untie attempted
297 # while 1 inner references still exist". Not sure what's the "right
298 # thing" to do here.
299 {
300 2 local $^W = 0;
301 2 untie *{$self->{'fh'}};
302 }
303  
304 2 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 2 2
305 {
306 0 binmode $self->{'fh'}, @_;
307 }
308 else
309 {
310 2 binmode $self->{'fh'};
311 }
312  
313 2 tie *{$self->{'fh'}}, __PACKAGE__, $self;
314 }
315  
316 #-------------------------------------------------------------------------------
317  
318 sub fileno
319 1 ▶{
Subroutine fileno: called CC=1 SCAR=0.0
320 1 my $self = shift;
321  
322 # Prevent recursion
323 # Temporarily disable warnings so that we don't get "untie attempted
324 # while 1 inner references still exist". Not sure what's the "right
325 # thing" to do here.
326 {
327 1 local $^W = 0;
328 1 untie *{$self->{'fh'}};
329 }
330  
331 1 my $fileno = fileno $self->{'fh'};
332  
333 1 tie *{$self->{'fh'}}, __PACKAGE__, $self;
334  
335 1 return $fileno;
336 }
337  
338 #-------------------------------------------------------------------------------
339  
340 sub getline_wrapper
341 63 ▶{
Subroutine getline_wrapper: called CC=2 SCAR=6.9
342 63 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 4 59 63
343 {
344 4 goto &getlines;
345 }
346 else
347 {
348 59 goto &getline;
349 }
350 }
351  
352 #-------------------------------------------------------------------------------
353  
354 sub getline
355 59 ▶{
Subroutine getline: called CC=12 SCAR=25.1
356 59 my $self = shift;
357  
358 # Prevent recursion
359 # Temporarily disable warnings so that we don't get "untie attempted
360 # while 1 inner references still exist". Not sure what's the "right
361 # thing" to do here.
362 {
363 59 local $^W = 0;
364 59 untie *{$self->{'fh'}};
365 }
366  
367 59 my $line;
368  
369 local $/ = $self->{'input_record_separator'}
370 59 ▶ if exists $self->{'input_record_separator'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"input_record_separator"} 3 56 59
371 59 my $input_record_separator = $/;
372  
373 59 ▶ if (defined $input_record_separator &&
Branch: 2 branches
BranchTrueFalseTotal
if (defined $input_record_separator and $self->{'filehandle_unget_buffer'} =~ /(.*?$input_record_separator)/) { } 10 49 59
elsif (not defined $input_record_separator) { } 5 44 49
Condition100%3/3
defined $input_record_separator and $self->{"filehandle_unget_buffer"} =~ /(.*?$input_record_separator)/
!ll&&!rl&&r
54410
MC/DC100%2/2
defined $input_record_separator and $self->{"filehandle_unget_buffer"} =~ /(.*?$input_record_separator)/
defined $input_record_separator $self->{"filehandle_unget_buffer"} =~ /(.*?$input_record_separator)/
Truth table100%3/3
A and B
A= defined $input_record_separatorB= $self->{"filehandle_unget_buffer"} =~ /(.*?$input_record_separator)/
ABresult
0X0
100
111
374 $self->{'filehandle_unget_buffer'} =~ /(.*?$input_record_separator)/)
375 {
376 10 $line = $1;
377 10 substr($self->{'filehandle_unget_buffer'},0,length $line) = '';
378 }
379 # My best guess at a fix for failures like these:
380 # http://www.cpantesters.org/cpan/report/2185d342-b14c-11e4-9727-fcccf9ba27bb
381 # http://www.cpantesters.org/cpan/report/74a6f9b6-95db-11e4-8169-9f55a5948d86
382 # It seems like even though $/ == undef, we're not reading all the rest of
383 # the file. Unfortunately I can't repro this, so I'll change it and see if
384 # the CPAN-Testers tests start passing.
385 elsif (!defined($input_record_separator))
386 {
387 5 $line = $self->{'filehandle_unget_buffer'};
388 5 $self->{'filehandle_unget_buffer'} = '';
389 5 my @other_lines = $self->{'fh'}->getlines(@_);
390  
391 # Not sure if this is necessary. The code in getlines() below seems to
392 # suggest so.
393 5 ▶ @other_lines = () if @other_lines && !defined($other_lines[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @other_lines and not defined $other_lines[0] 0 5 5
Condition33%1/3
@other_lines and not defined $other_lines[0]
!ll&&!rl&&r
050
MC/DC0%0/2
@other_lines and not defined $other_lines[0]
@other_lines not defined $other_lines[0]
Truth table33%1/3
A and not(B)
A= @other_linesB= not defined $other_lines[0]
ABresult
0X0
100
111
394  
395 5 ▶ if ($line eq '' && !@other_lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq '' and not @other_lines) { } 0 5 5
Condition66%2/3
$line eq "" and not @other_lines
!ll&&!rl&&r
140
MC/DC0%0/2
$line eq "" and not @other_lines
$line eq "" not @other_lines
Truth table66%2/3
A and not(B)
A= $line eq ""B= not @other_lines
ABresult
0X0
100
111
396 {
397 0 $line = undef;
398 }
399 else
400 {
401 5 $line .= join('', @other_lines);
402 }
403 }
404 else
405 {
406 44 $line = $self->{'filehandle_unget_buffer'};
407 44 $self->{'filehandle_unget_buffer'} = '';
408 44 my $templine = $self->{'fh'}->getline(@_);
409  
410 44 ▶ if ($line eq '' && !defined $templine)
Branch: 2 branches
BranchTrueFalseTotal
if ($line eq '' and not defined $templine) { } 2 42 44
elsif (defined $templine) { } 42 0 42
Condition100%3/3
$line eq "" and not defined $templine
!ll&&!rl&&r
1412
MC/DC100%2/2
$line eq "" and not defined $templine
$line eq "" not defined $templine
Truth table100%3/3
A and not(B)
A= $line eq ""B= not defined $templine
ABresult
0X0
100
111
411 {
412 2 $line = undef;
413 }
414 elsif (defined $templine)
415 {
416 42 $line .= $templine;
417 }
418 }
419  
420 59 tie *{$self->{'fh'}}, __PACKAGE__, $self;
421  
422 59 return $line;
423 }
424  
425 #-------------------------------------------------------------------------------
426  
427 sub getlines
428 4 ▶{
Subroutine getlines: called CC=8 SCAR=25.2
429 4 my $self = shift;
430  
431 # Prevent recursion
432 # Temporarily disable warnings so that we don't get "untie attempted
433 # while 1 inner references still exist". Not sure what's the "right
434 # thing" to do here.
435 {
436 4 local $^W = 0;
437 4 untie *{$self->{'fh'}};
438 }
439  
440 4 my @buffer_lines;
441  
442 local $/ = $self->{'input_record_separator'}
443 4 ▶ if exists $self->{'input_record_separator'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"input_record_separator"} 0 4 4
444 4 my $input_record_separator = $/;
445  
446 4 ▶ if (defined $input_record_separator)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_record_separator) { } 4 0 4
447 {
448 4 $self->{'filehandle_unget_buffer'} =~
449 0 s/^(.*$input_record_separator)/push @buffer_lines, $1;''/mge;
450  
451 4 my @other_lines = $self->{'fh'}->getlines(@_);
452  
453 4 ▶ if (@other_lines)
Branch: 1 branch
BranchTrueFalseTotal
if (@other_lines) { } 3 1 4
454 {
455 3 ▶ if (defined $other_lines[0])
Branch: 1 branch
BranchTrueFalseTotal
if (defined $other_lines[0]) 3 0 3
456 {
457 3 substr($other_lines[0],0,0) = $self->{'filehandle_unget_buffer'};
458 }
459 }
460 else
461 {
462 1 ▶ if ($self->{'filehandle_unget_buffer'} ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"filehandle_unget_buffer"} ne "") 0 1 1
463 {
464 0 unshift @other_lines, $self->{'filehandle_unget_buffer'};
465 }
466 }
467  
468 4 $self->{'filehandle_unget_buffer'} = '';
469  
470 4 push @buffer_lines, @other_lines;
471 }
472 else
473 {
474 0 $buffer_lines[0] = $self->{'filehandle_unget_buffer'};
475 0 $self->{'filehandle_unget_buffer'} = '';
476 # Not sure why this isn't working for some platforms. If $/ is undef, then
477 # all the lines should be in [0].
478 # my $templine = ($self->{'fh'}->getlines(@_))[0];
479 0 my @other_lines = $self->{'fh'}->getlines(@_);
480  
481 0 ▶ if ($buffer_lines[0] eq '' && !defined $other_lines[0])
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer_lines[0] eq '' and not defined $other_lines[0]) { } 0 0 0
Condition0%0/3
$buffer_lines[0] eq "" and not defined $other_lines[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$buffer_lines[0] eq "" and not defined $other_lines[0]
$buffer_lines[0] eq "" not defined $other_lines[0]
Truth table0%0/3
A and not(B)
A= $buffer_lines[0] eq ""B= not defined $other_lines[0]
ABresult
0X0
100
111
482 {
483 # Should this really be "(undef)" and not just "undef"? Leaving it for
484 # now, to avoid changing the API until I know the answer.
485 0 $buffer_lines[0] = undef;
486 }
487 else
488 {
489 0 $buffer_lines[0] .= join('', @other_lines);
490 }
491 }
492  
493 4 tie *{$self->{'fh'}}, __PACKAGE__, $self;
494  
495 4 return @buffer_lines;
496 }
497  
498 #-------------------------------------------------------------------------------
499  
500 sub getc
501 0 ▶{
Subroutine getc: not called CC=2 SCAR=17.9
502 0 my $self = shift;
503  
504 # Prevent recursion
505 # Temporarily disable warnings so that we don't get "untie attempted
506 # while 1 inner references still exist". Not sure what's the "right
507 # thing" to do here.
508 {
509 0 local $^W = 0;
510 0 untie *{$self->{'fh'}};
511 }
512  
513 0 my $char;
514  
515 0 ▶ if ($self->{'filehandle_unget_buffer'} ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'filehandle_unget_buffer'} ne '') { } 0 0 0
516 {
517 0 $char = substr($self->{'filehandle_unget_buffer'},0,1);
518 0 substr($self->{'filehandle_unget_buffer'},0,1) = '';
519 }
520 else
521 {
522 0 $char = $self->{'fh'}->getc(@_);
523 }
524  
525 0 tie *{$self->{'fh'}}, __PACKAGE__, $self;
526  
527 0 return $char;
528 }
529  
530 #-------------------------------------------------------------------------------
531  
532 sub read
533 10 ▶{
Subroutine read: called CC=6 SCAR=18.3
534 10 my $self = shift;
535  
536 # Prevent recursion
537 # Temporarily disable warnings so that we don't get "untie attempted
538 # while 1 inner references still exist". Not sure what's the "right
539 # thing" to do here.
540 {
541 10 local $^W = 0;
542 10 untie *{$self->{'fh'}};
543 }
544  
545 10 my $scalar = \$_[0];
546 10 my $length = $_[1];
547 10 my $offset = $_[2];
548  
549 10 my $num_bytes_read = 0;
550  
551 10 ▶ if ($self->{'filehandle_unget_buffer'} ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'filehandle_unget_buffer'} ne '') { } 3 7 10
552 {
553 3 my $read_string = substr($self->{'filehandle_unget_buffer'},0,$length);
554 3 substr($self->{'filehandle_unget_buffer'},0,$length) = '';
555  
556 3 my $num_bytes_buffer = length $read_string;
557  
558 # Try to read the rest
559 3 ▶ if (length($read_string) < $length)
Branch: 1 branch
BranchTrueFalseTotal
if (length $read_string < $length) 0 3 3
560 {
561 0 $num_bytes_read = read($self->{'fh'}, $read_string,
562 $length - $num_bytes_buffer, $num_bytes_buffer);
563 }
564  
565 3 ▶ if (defined $offset)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offset) { } 0 3 3
566 {
567 0 ▶ $$scalar = '' unless defined $$scalar;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$scalar 0 0 0
568 0 substr($$scalar,$offset) = $read_string;
569 }
570 else
571 {
572 3 $$scalar = $read_string;
573 }
574  
575 3 $num_bytes_read += $num_bytes_buffer;
576 }
577 else
578 {
579 7 ▶ if (defined $_[2])
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[2]) { } 3 4 7
580 {
581 3 $num_bytes_read = read($self->{'fh'},$_[0],$_[1],$_[2]);
582 }
583 else
584 {
585 4 $num_bytes_read = read($self->{'fh'},$_[0],$_[1]);
586 }
587 }
588  
589 10 tie *{$self->{'fh'}}, __PACKAGE__, $self;
590  
591 10 return $num_bytes_read;
592 }
593  
594 #-------------------------------------------------------------------------------
595  
596 sub seek
597 3 ▶{
Subroutine seek: called CC=10 SCAR=34.3
598 3 my $self = shift;
599 3 my $position = $_[0];
600 3 my $whence = $_[1];
601  
602 # Prevent recursion
603 # Temporarily disable warnings so that we don't get "untie attempted
604 # while 1 inner references still exist". Not sure what's the "right
605 # thing" to do here.
606 {
607 3 local $^W = 0;
608 3 untie *{$self->{'fh'}};
609 }
610  
611 3 ▶ if($whence != 0 && $whence != 1 && $whence != 2)
Branch: 1 branch
BranchTrueFalseTotal
if ($whence != 0 and $whence != 1 and $whence != 2) 0 3 3
Condition50%3/6
$whence != 0 and $whence != 1 and $whence != 2
!ll&&!rl&&r
300
$whence != 0 and $whence != 1
!ll&&!rl&&r
210
MC/DC0%0/3
$whence != 0 and $whence != 1 and $whence != 2
$whence != 0 $whence != 1 $whence != 2
Truth table50%2/4
A and B and C
A= $whence != 0B= $whence != 1C= $whence != 2
ABCresult
0XX0
10X0
1100
1111
612 {
613 0 tie *{$self->{'fh'}}, __PACKAGE__, $self;
614 0 return 0;
615 }
616  
617 3 my $status;
618  
619 # First try to seek using the built-in seek
620 3 ▶ if (seek($self->{'fh'},$position,$whence))
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::seek $self->{'fh'}, $position, $whence) { } 3 0 3
621 {
622 3 $self->{'filehandle_unget_buffer'} = '';
623 3 $status = 1;
624 }
625 else
626 {
627 0 my $absolute_position;
628  
629 0 ▶ $absolute_position = $position if $whence == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $whence == 0 0 0 0
630 0 ▶ $absolute_position = $self->tell + $position if $whence == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $whence == 1 0 0 0
631 0 ▶ $absolute_position = -s $self->{'fh'} + $position if $whence == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $whence == 2 0 0 0
632  
633 0 ▶ if ($absolute_position <= tell $self->{'fh'})
Branch: 1 branch
BranchTrueFalseTotal
if ($absolute_position <= CORE::tell $self->{'fh'}) { } 0 0 0
634 {
635 0 ▶ if ($absolute_position >= $self->tell)
Branch: 1 branch
BranchTrueFalseTotal
if ($absolute_position >= $self->tell) { } 0 0 0
636 {
637 0 substr($self->{'filehandle_unget_buffer'}, 0,
638 $absolute_position - $self->tell) = '';
639 0 $status = 1;
640 }
641 else
642 {
643 # Can't seek backward!
644 0 $status = 0;
645 }
646 }
647 else
648 {
649 # Shouldn't the built-in seek handle this?!
650 0 warn "Seeking forward is not yet implemented in " . __PACKAGE__ . "\n";
651 0 $status = 0;
652 }
653 }
654  
655 3 tie *{$self->{'fh'}}, __PACKAGE__, $self;
656  
657 3 return $status;
658 }
659  
660 #-------------------------------------------------------------------------------
661  
662 sub tell
663 12 ▶{
Subroutine tell: called CC=2 SCAR=7.2
664 12 my $self = shift;
665  
666 # Prevent recursion
667 # Temporarily disable warnings so that we don't get "untie attempted
668 # while 1 inner references still exist". Not sure what's the "right
669 # thing" to do here.
670 {
671 12 local $^W = 0;
672 12 untie *{$self->{'fh'}};
673 }
674  
675 12 my $file_position = tell $self->{'fh'};
676  
677 12 ▶ if ($file_position == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($file_position == -1) 0 12 12
678 {
679 0 tie *{$self->{'fh'}}, __PACKAGE__, $self;
680 0 return -1;
681 }
682  
683 12 $file_position -= length($self->{'filehandle_unget_buffer'});
684  
685 12 tie *{$self->{'fh'}}, __PACKAGE__, $self;
686  
687 12 return $file_position;
688 }
689  
690 #-------------------------------------------------------------------------------
691  
692 sub eof
693 4 ▶{
Subroutine eof: called CC=2 SCAR=7.0
694 4 my $self = shift;
695  
696 # Prevent recursion
697 # Temporarily disable warnings so that we don't get "untie attempted
698 # while 1 inner references still exist". Not sure what's the "right
699 # thing" to do here.
700 {
701 4 local $^W = 0;
702 4 untie *{$self->{'fh'}};
703 }
704  
705 4 my $eof;
706  
707 4 ▶ if ($self->{'filehandle_unget_buffer'} ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'filehandle_unget_buffer'} ne '') { } 0 4 4
708 {
709 0 $eof = 0;
710 }
711 else
712 {
713 4 $eof = $self->{'fh'}->eof();
714 }
715  
716 4 tie *{$self->{'fh'}}, __PACKAGE__, $self;
717  
718 4 $self->{'eof_called'} = 1;
719  
720 4 return $eof;
721 }
722  
723 #-------------------------------------------------------------------------------
724  
725 sub fetch
726 0 ▶{
Subroutine fetch: not called CC=1 SCAR=6.9
727 0 my $self = shift;
728 0 return $self;
729 }
730  
731 24 1;
732  
733 __END__
↑ Summary