blib/lib/IO/Zlib.pm

filter: Statementstmt 93.7% 166 / 177 Branchbran 72.6% 61 / 84 Conditioncond 52.9% 9 / 17 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 88.8% 32 / 36 Podpod 62.5% 10 / 16 totaltotal 82.6% 281 / 340 CC 49 SCAR 16.5
CC
49
Cov
85%
gzopen_external
13.8
_import
10.1
can_gunzip
6.0
CRAP 5.2
↑ Summary
1 # IO::Zlib.pm
2 #
3 # Copyright (c) 1998-2004 Tom Hughes <tom@compton.nu>.
4 # All rights reserved. This program is free software; you can redistribute
5 # it and/or modify it under the same terms as Perl itself.
6  
7 package IO::Zlib;
8  
9 9 $VERSION = "1.10_01";
10  
11 =head1 NAME
12  
13 IO::Zlib - IO:: style interface to L<Compress::Zlib>
14  
15 =head1 SYNOPSIS
16  
17 With any version of Perl 5 you can use the basic OO interface:
18  
19 use IO::Zlib;
20  
21 $fh = new IO::Zlib;
22 if ($fh->open("file.gz", "rb")) {
23 print <$fh>;
24 $fh->close;
25 }
26  
27 $fh = IO::Zlib->new("file.gz", "wb9");
28 if (defined $fh) {
29 print $fh "bar\n";
30 $fh->close;
31 }
32  
33 $fh = IO::Zlib->new("file.gz", "rb");
34 if (defined $fh) {
35 print <$fh>;
36 undef $fh; # automatically closes the file
37 }
38  
39 With Perl 5.004 you can also use the TIEHANDLE interface to access
40 compressed files just like ordinary files:
41  
42 use IO::Zlib;
43  
44 tie *FILE, 'IO::Zlib', "file.gz", "wb";
45 print FILE "line 1\nline2\n";
46  
47 tie *FILE, 'IO::Zlib', "file.gz", "rb";
48 while (<FILE>) { print "LINE: ", $_ };
49  
50 =head1 DESCRIPTION
51  
52 C<IO::Zlib> provides an IO:: style interface to L<Compress::Zlib> and
53 hence to gzip/zlib compressed files. It provides many of the same methods
54 as the L<IO::Handle> interface.
55  
56 Starting from IO::Zlib version 1.02, IO::Zlib can also use an
57 external F<gzip> command. The default behaviour is to try to use
58 an external F<gzip> if no C<Compress::Zlib> can be loaded, unless
59 explicitly disabled by
60  
61 use IO::Zlib qw(:gzip_external 0);
62  
63 If explicitly enabled by
64  
65 use IO::Zlib qw(:gzip_external 1);
66  
67 then the external F<gzip> is used B<instead> of C<Compress::Zlib>.
68  
69 =head1 CONSTRUCTOR
70  
71 =over 4
72  
73 =item new ( [ARGS] )
74  
75 Creates an C<IO::Zlib> object. If it receives any parameters, they are
76 passed to the method C<open>; if the open fails, the object is destroyed.
77 Otherwise, it is returned to the caller.
78  
79 =back
80  
81 =head1 OBJECT METHODS
82  
83 =over 4
84  
85 =item open ( FILENAME, MODE )
86  
87 C<open> takes two arguments. The first is the name of the file to open
88 and the second is the open mode. The mode can be anything acceptable to
89 L<Compress::Zlib> and by extension anything acceptable to I<zlib> (that
90 basically means POSIX fopen() style mode strings plus an optional number
91 to indicate the compression level).
92  
93 =item opened
94  
95 Returns true if the object currently refers to a opened file.
96  
97 =item close
98  
99 Close the file associated with the object and disassociate
100 the file from the handle.
101 Done automatically on destroy.
102  
103 =item getc
104  
105 Return the next character from the file, or undef if none remain.
106  
107 =item getline
108  
109 Return the next line from the file, or undef on end of string.
110 Can safely be called in an array context.
111 Currently ignores $/ ($INPUT_RECORD_SEPARATOR or $RS when L<English>
112 is in use) and treats lines as delimited by "\n".
113  
114 =item getlines
115  
116 Get all remaining lines from the file.
117 It will croak() if accidentally called in a scalar context.
118  
119 =item print ( ARGS... )
120  
121 Print ARGS to the file.
122  
123 =item read ( BUF, NBYTES, [OFFSET] )
124  
125 Read some bytes from the file.
126 Returns the number of bytes actually read, 0 on end-of-file, undef on error.
127  
128 =item eof
129  
130 Returns true if the handle is currently positioned at end of file?
131  
132 =item seek ( OFFSET, WHENCE )
133  
134 Seek to a given position in the stream.
135 Not yet supported.
136  
137 =item tell
138  
139 Return the current position in the stream, as a numeric offset.
140 Not yet supported.
141  
142 =item setpos ( POS )
143  
144 Set the current position, using the opaque value returned by C<getpos()>.
145 Not yet supported.
146  
147 =item getpos ( POS )
148  
149 Return the current position in the string, as an opaque object.
150 Not yet supported.
151  
152 =back
153  
154 =head1 USING THE EXTERNAL GZIP
155  
156 If the external F<gzip> is used, the following C<open>s are used:
157  
158 open(FH, "gzip -dc $filename |") # for read opens
159 open(FH, " | gzip > $filename") # for write opens
160  
161 You can modify the 'commands' for example to hardwire
162 an absolute path by e.g.
163  
164 use IO::Zlib ':gzip_read_open' => '/some/where/gunzip -c %s |';
165 use IO::Zlib ':gzip_write_open' => '| /some/where/gzip.exe > %s';
166  
167 The C<%s> is expanded to be the filename (C<sprintf> is used, so be
168 careful to escape any other C<%> signs). The 'commands' are checked
169 for sanity - they must contain the C<%s>, and the read open must end
170 with the pipe sign, and the write open must begin with the pipe sign.
171  
172 =head1 CLASS METHODS
173  
174 =over 4
175  
176 =item has_Compress_Zlib
177  
178 Returns true if C<Compress::Zlib> is available. Note that this does
179 not mean that C<Compress::Zlib> is being used: see L</gzip_external>
180 and L<gzip_used>.
181  
182 =item gzip_external
183  
184 Undef if an external F<gzip> B<can> be used if C<Compress::Zlib> is
185 not available (see L</has_Compress_Zlib>), true if an external F<gzip>
186 is explicitly used, false if an external F<gzip> must not be used.
187 See L</gzip_used>.
188  
189 =item gzip_used
190  
191 True if an external F<gzip> is being used, false if not.
192  
193 =item gzip_read_open
194  
195 Return the 'command' being used for opening a file for reading using an
196 external F<gzip>.
197  
198 =item gzip_write_open
199  
200 Return the 'command' being used for opening a file for writing using an
201 external F<gzip>.
202  
203 =back
204  
205 =head1 DIAGNOSTICS
206  
207 =over 4
208  
209 =item IO::Zlib::getlines: must be called in list context
210  
211 If you want read lines, you must read in list context.
212  
213 =item IO::Zlib::gzopen_external: mode '...' is illegal
214  
215 Use only modes 'rb' or 'wb' or /wb[1-9]/.
216  
217 =item IO::Zlib::import: '...' is illegal
218  
219 The known import symbols are the C<:gzip_external>, C<:gzip_read_open>,
220 and C<:gzip_write_open>. Anything else is not recognized.
221  
222 =item IO::Zlib::import: ':gzip_external' requires an argument
223  
224 The C<:gzip_external> requires one boolean argument.
225  
226 =item IO::Zlib::import: 'gzip_read_open' requires an argument
227  
228 The C<:gzip_external> requires one string argument.
229  
230 =item IO::Zlib::import: 'gzip_read' '...' is illegal
231  
232 The C<:gzip_read_open> argument must end with the pipe sign (|)
233 and have the C<%s> for the filename. See L</"USING THE EXTERNAL GZIP">.
234  
235 =item IO::Zlib::import: 'gzip_write_open' requires an argument
236  
237 The C<:gzip_external> requires one string argument.
238  
239 =item IO::Zlib::import: 'gzip_write_open' '...' is illegal
240  
241 The C<:gzip_write_open> argument must begin with the pipe sign (|)
242 and have the C<%s> for the filename. An output redirect (>) is also
243 often a good idea, depending on your operating system shell syntax.
244 See L</"USING THE EXTERNAL GZIP">.
245  
246 =item IO::Zlib::import: no Compress::Zlib and no external gzip
247  
248 Given that we failed to load C<Compress::Zlib> and that the use of
249 an external F<gzip> was disabled, IO::Zlib has not much chance of working.
250  
251 =item IO::Zlib::open: needs a filename
252  
253 No filename, no open.
254  
255 =item IO::Zlib::READ: NBYTES must be specified
256  
257 We must know how much to read.
258  
259 =item IO::Zlib::WRITE: too long LENGTH
260  
261 The LENGTH must be less than or equal to the buffer size.
262  
263 =back
264  
265 =head1 SEE ALSO
266  
267 L<perlfunc>,
268 L<perlop/"I/O Operators">,
269 L<IO::Handle>,
270 L<Compress::Zlib>
271  
272 =head1 HISTORY
273  
274 Created by Tom Hughes E<lt>F<tom@compton.nu>E<gt>.
275  
276 Support for external gzip added by Jarkko Hietaniemi E<lt>F<jhi@iki.fi>E<gt>.
277  
278 =head1 COPYRIGHT
279  
280 Copyright (c) 1998-2004 Tom Hughes E<lt>F<tom@compton.nu>E<gt>.
281 All rights reserved. This program is free software; you can redistribute
282 it and/or modify it under the same terms as Perl itself.
283  
284 =cut
285  
286 9 require 5.006;
287  
288 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
289 9 ▶use vars qw($VERSION $AUTOLOAD @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
290  
291 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
292 9 ▶use Fcntl qw(SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
293  
294 9 my $has_Compress_Zlib;
295 my $aliased;
296  
297 sub has_Compress_Zlib {
298 1 ▶ $has_Compress_Zlib;
Subroutine has_Compress_Zlib: called CC=1 SCAR=0.0
Pod documented
299 }
300  
301 BEGIN {
302 9 ▶ eval { require Compress::Zlib };
Subroutine BEGIN: called CC=3 SCAR=12.5
303 9 ▶ $has_Compress_Zlib = $@ || $Compress::Zlib::VERSION < 2.000 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ || $Compress::Zlib::VERSION < 2.0 ? : 0 9 9
Condition33%1/3
$@ or $Compress::Zlib::VERSION < 2
l!l&&r!l&&!r
009
MC/DC0%0/2
$@ or $Compress::Zlib::VERSION < 2
$@ $Compress::Zlib::VERSION < 2
Truth table33%1/3
A or B
A= $@B= $Compress::Zlib::VERSION < 2
ABresult
1X1
011
000
304 }
305  
306 9 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
307 9 ▶use Tie::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
308  
309 # These might use some $^O logic.
310 9 my $gzip_read_open = "gzip -dc %s |";
311 9 my $gzip_write_open = "| gzip > %s";
312  
313 9 my $gzip_external;
314 my $gzip_used;
315  
316 sub gzip_read_open {
317 2 ▶ $gzip_read_open;
Subroutine gzip_read_open: called CC=1 SCAR=0.0
Pod documented
318 }
319  
320 sub gzip_write_open {
321 2 ▶ $gzip_write_open;
Subroutine gzip_write_open: called CC=1 SCAR=0.0
Pod documented
322 }
323  
324 sub gzip_external {
325 3 ▶ $gzip_external;
Subroutine gzip_external: called CC=1 SCAR=0.0
Pod documented
326 }
327  
328 sub gzip_used {
329 1 ▶ $gzip_used;
Subroutine gzip_used: called CC=1 SCAR=0.0
Pod documented
330 }
331  
332 sub can_gunzip {
333 0 ▶ $has_Compress_Zlib || $gzip_external;
Subroutine can_gunzip: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$has_Compress_Zlib or $gzip_external 0 0 0
334 }
335  
336 sub _import {
337 8 ▶ my $import = shift;
Subroutine _import: called CC=10 SCAR=23.2
338 8 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 4 12
339 8 ▶ if ($_[0] eq ':gzip_external') {
Branch: 3 branches
BranchTrueFalseTotal
if ($_[0] eq ':gzip_external') { } 3 5 8
elsif ($_[0] eq ':gzip_read_open') { } 2 3 5
elsif ($_[0] eq ':gzip_write_open') { } 2 1 3
340 3 shift;
341 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 1 3
342 2 $gzip_external = shift;
343 } else {
344 1 croak "$import: ':gzip_external' requires an argument";
345 }
346 }
347 elsif ($_[0] eq ':gzip_read_open') {
348 2 shift;
349 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 0 2
350 2 $gzip_read_open = shift;
351 2 ▶ croak "$import: ':gzip_read_open' '$gzip_read_open' is illegal"
Branch: 1 branch
BranchTrueFalseTotal
unless $gzip_read_open =~ /^.+%s.+\|\s*$/ 1 1 2
352 unless $gzip_read_open =~ /^.+%s.+\|\s*$/;
353 } else {
354 0 croak "$import: ':gzip_read_open' requires an argument";
355 }
356 }
357 elsif ($_[0] eq ':gzip_write_open') {
358 2 shift;
359 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 0 2
360 2 $gzip_write_open = shift;
361 2 ▶ croak "$import: ':gzip_write_open' '$gzip_read_open' is illegal"
Branch: 1 branch
BranchTrueFalseTotal
unless $gzip_write_open =~ /^\s*\|.+%s.*$/ 1 1 2
362 unless $gzip_write_open =~ /^\s*\|.+%s.*$/;
363 } else {
364 0 croak "$import: ':gzip_write_open' requires an argument";
365 }
366 }
367 else {
368 1 last;
369 }
370 }
371 5 return @_;
372 }
373  
374 sub _alias {
375 13 ▶ my $import = shift;
Subroutine _alias: called CC=5 SCAR=16.3
376 13 ▶ if ((!$has_Compress_Zlib && !defined $gzip_external) || $gzip_external) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $has_Compress_Zlib || defined $gzip_external or $gzip_external) { } 3 10 13
Condition50%3/6
not $has_Compress_Zlib || defined $gzip_external or $gzip_external
l!l&&r!l&&!r
0310
$has_Compress_Zlib || defined $gzip_external
l!l&&r!l&&!r
1300
MC/DC33%1/3
not $has_Compress_Zlib || defined $gzip_external or $gzip_external
$has_Compress_Zlib defined $gzip_external $gzip_external
Truth table40%2/5
not(A || B) or C
A= $has_Compress_ZlibB= defined $gzip_externalC= $gzip_external
ABCresult
00X1
1X11
0111
1X00
0100
377 # The undef *gzopen is really needed only during
378 # testing where we eval several 'use IO::Zlib's.
379 3 undef *gzopen;
380 3 *gzopen = \&gzopen_external;
381 3 *IO::Handle::gzread = \&gzread_external;
382 3 *IO::Handle::gzwrite = \&gzwrite_external;
383 3 *IO::Handle::gzreadline = \&gzreadline_external;
384 3 *IO::Handle::gzeof = \&gzeof_external;
385 3 *IO::Handle::gzclose = \&gzclose_external;
386 3 $gzip_used = 1;
387 } else {
388 10 ▶ croak "$import: no Compress::Zlib and no external gzip"
Branch: 1 branch
BranchTrueFalseTotal
unless $has_Compress_Zlib 0 10 10
389 unless $has_Compress_Zlib;
390 10 *gzopen = \&Compress::Zlib::gzopen;
391 10 *gzread = \&Compress::Zlib::gzread;
392 10 *gzwrite = \&Compress::Zlib::gzwrite;
393 10 *gzreadline = \&Compress::Zlib::gzreadline;
394 10 *gzeof = \&Compress::Zlib::gzeof;
395 }
396 13 $aliased = 1;
397 }
398  
399 sub import {
400 16 ▶ shift;
Subroutine import: called CC=3 SCAR=11.0
401 16 my $import = "IO::Zlib::import";
402 16 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 8 16
403 8 ▶ if (_import($import, @_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_import($import, @_)) 1 4 5
404 1 croak "$import: '@_' is illegal";
405 }
406 }
407 12 _alias($import);
408 }
409  
410 9 @ISA = qw(Tie::Handle);
411  
412 sub TIEHANDLE
413 {
414 21 ▶ my $class = shift;
Subroutine TIEHANDLE: called CC=2 SCAR=6.9
415 21 my @args = @_;
416  
417 21 my $self = bless {}, $class;
418  
419 21 ▶ return @args ? $self->OPEN(@args) : $self;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 17 4 21
420 }
421  
422 sub DESTROY
423 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
424 }
425  
426 sub OPEN
427 {
428 21 ▶ my $self = shift;
Subroutine OPEN: called CC=3 SCAR=11.0
429 21 my $filename = shift;
430 21 my $mode = shift;
431  
432 21 ▶ croak "IO::Zlib::open: needs a filename" unless defined($filename);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 21 21
433  
434 21 $self->{'file'} = gzopen($filename,$mode);
435  
436 20 ▶ return defined($self->{'file'}) ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'file'} ? : 16 4 20
437 }
438  
439 sub CLOSE
440 {
441 14 ▶ my $self = shift;
Subroutine CLOSE: called CC=3 SCAR=11.3
442  
443 14 ▶ return undef unless defined($self->{'file'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"file"} 0 14 14
444  
445 14 my $status = $self->{'file'}->gzclose();
446  
447 14 delete $self->{'file'};
448  
449 14 ▶ return ($status == 0) ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$status == 0 ? : 14 0 14
450 }
451  
452 sub READ
453 {
454 12 ▶ my $self = shift;
Subroutine READ: called CC=5 SCAR=16.2
455 12 my $bufref = \$_[0];
456 12 my $nbytes = $_[1];
457 12 ▶ my $offset = $_[2] || 0;
Condition100%2/2
$_[2] || 0
l!l
111
MC/DC100%1/1
$_[2] || 0
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
458  
459 12 ▶ croak "IO::Zlib::READ: NBYTES must be specified" unless defined($nbytes);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nbytes 0 12 12
460  
461 12 ▶ $$bufref = "" unless defined($$bufref);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$bufref 11 1 12
462  
463 12 my $bytesread = $self->{'file'}->gzread(substr($$bufref,$offset),$nbytes);
464  
465 12 ▶ return undef if $bytesread < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bytesread < 0 0 12 12
466  
467 12 return $bytesread;
468 }
469  
470 sub READLINE
471 {
472 7 ▶ my $self = shift;
Subroutine READLINE: called CC=4 SCAR=13.9
473  
474 7 my $line;
475  
476 7 ▶ return () if $self->{'file'}->gzreadline($line) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"file"}->gzreadline($line) <= 0 1 6 7
477  
478 6 ▶ return $line unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 5 1 6
479  
480 1 my @lines = $line;
481  
482 1 ▶ while ($self->{'file'}->gzreadline($line) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"file"}->gzreadline($line) > 0) 3 1 4
483 {
484 3 push @lines, $line;
485 }
486  
487 1 return @lines;
488 }
489  
490 sub WRITE
491 {
492 7 ▶ my $self = shift;
Subroutine WRITE: called CC=2 SCAR=7.0
493 7 my $buf = shift;
494 7 my $length = shift;
495 7 my $offset = shift;
496  
497 7 ▶ croak "IO::Zlib::WRITE: too long LENGTH" unless $offset + $length <= length($buf);
Branch: 1 branch
BranchTrueFalseTotal
unless $offset + $length <= length $buf 0 7 7
498  
499 7 return $self->{'file'}->gzwrite(substr($buf,$offset,$length));
500 }
501  
502 sub EOF
503 {
504 14 ▶ my $self = shift;
Subroutine EOF: called CC=1 SCAR=0.0
505  
506 14 return $self->{'file'}->gzeof();
507 }
508  
509 sub FILENO
510 {
511 0 ▶ return undef;
Subroutine FILENO: not called CC=1 SCAR=6.9
512 }
513  
514 sub new
515 {
516 19 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
517 19 my @args = @_;
518  
519 19 ▶ _alias("new", @_) unless $aliased; # Some call new IO::Zlib directly...
Branch: 1 branch
BranchTrueFalseTotal
unless $aliased 1 18 19
520  
521 19 my $self = gensym();
522  
523 19 tie *{$self}, $class, @args;
524  
525 18 ▶ return tied(${$self}) ? bless $self, $class : undef;
Branch: 1 branch
BranchTrueFalseTotal
tied ${$self;} ? : 14 4 18
526 }
527  
528 sub getline
529 {
530 5 ▶ my $self = shift;
Subroutine getline: called CC=1 SCAR=0.0
Pod documented
531  
532 5 return scalar tied(*{$self})->READLINE();
533 }
534  
535 sub getlines
536 {
537 2 ▶ my $self = shift;
Subroutine getlines: called CC=2 SCAR=6.9
Pod documented
538  
539 2 ▶ croak "IO::Zlib::getlines: must be called in list context"
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 1 1 2
540 unless wantarray;
541  
542 1 return tied(*{$self})->READLINE();
543 }
544  
545 sub opened
546 {
547 12 ▶ my $self = shift;
Subroutine opened: called CC=1 SCAR=0.0
Pod documented
548  
549 12 return defined tied(*{$self})->{'file'};
550 }
551  
552 sub AUTOLOAD
553 {
554 46 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
555  
556 46 $AUTOLOAD =~ s/.*:://;
557 46 $AUTOLOAD =~ tr/a-z/A-Z/;
558  
559 46 return tied(*{$self})->$AUTOLOAD(@_);
560 }
561  
562 sub gzopen_external {
563 4 ▶ my ($filename, $mode) = @_;
Subroutine gzopen_external: called CC=11 SCAR=26.3
Pod documented
564 4 require IO::Handle;
565 4 my $fh = IO::Handle->new();
566 4 ▶ if ($mode =~ /r/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode =~ /r/) { } 2 2 4
elsif ($mode =~ /w/) { } 1 1 2
567 # Because someone will try to read ungzipped files
568 # with this we peek and verify the signature. Yes,
569 # this means that we open the file twice (if it is
570 # gzipped).
571 # Plenty of race conditions exist in this code, but
572 # the alternative would be to capture the stderr of
573 # gzip and parse it, which would be a portability nightmare.
574 2 ▶ if (-e $filename && open($fh, $filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename and open $fh, $filename) { } 1 1 2
Condition66%2/3
-e $filename and open $fh, $filename
!ll&&!rl&&r
101
MC/DC50%1/2
-e $filename and open $fh, $filename
-e $filename open $fh, $filename
Truth table66%2/3
A and B
A= -e $filenameB= open $fh, $filename
ABresult
0X0
100
111
575 1 binmode $fh;
576 1 my $sig;
577 1 my $rdb = read($fh, $sig, 2);
578 1 ▶ if ($rdb == 2 && $sig eq "\x1F\x8B") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rdb == 2 and $sig eq "\37\213") 1 0 1
Condition33%1/3
$rdb == 2 and $sig eq "\37\213"
!ll&&!rl&&r
001
MC/DC0%0/2
$rdb == 2 and $sig eq "\37\213"
$rdb == 2 $sig eq "\37\213"
Truth table33%1/3
A and B
A= $rdb == 2B= $sig eq "\37\213"
ABresult
0X0
100
111
579 1 my $ropen = sprintf $gzip_read_open, $filename;
580 1 ▶ if (open($fh, $ropen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $fh, $ropen) { } 1 0 1
581 1 binmode $fh;
582 1 return $fh;
583 } else {
584 0 return undef;
585 }
586 }
587 0 ▶ seek($fh, 0, SEEK_SET) or
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or die "IO::Zlib: open('${filename}', 'r'): seek: $!" 0 0 0
588 die "IO::Zlib: open('$filename', 'r'): seek: $!";
589 0 return $fh;
590 } else {
591 1 return undef;
592 }
593 } elsif ($mode =~ /w/) {
594 1 my $level = '';
595 1 ▶ $level = "-$1" if $mode =~ /([1-9])/;
Branch: 1 branch
BranchTrueFalseTotal
if $mode =~ /([1-9])/ 0 1 1
596 # To maximize portability we would need to open
597 # two filehandles here, one for "| gzip $level"
598 # and another for "> $filename", and then when
599 # writing copy bytes from the first to the second.
600 # We are using IO::Handle objects for now, however,
601 # and they can only contain one stream at a time.
602 1 my $wopen = sprintf $gzip_write_open, $filename;
603 1 ▶ if (open($fh, $wopen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $fh, $wopen) { } 1 0 1
604 1 $fh->autoflush(1);
605 1 binmode $fh;
606 1 return $fh;
607 } else {
608 0 return undef;
609 }
610 } else {
611 1 croak "IO::Zlib::gzopen_external: mode '$mode' is illegal";
612 }
613 0 return undef;
614 }
615  
616 sub gzread_external {
617 # Use read() instead of syswrite() because people may
618 # mix reads and readlines, and we don't want to mess
619 # the stdio buffering. See also gzreadline_external()
620 # and gzwrite_external().
621 1 ▶ my $nread = read($_[0], $_[1], @_ == 3 ? $_[2] : 4096);
Subroutine gzread_external: called CC=3 SCAR=12.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 ? : 1 0 1
622 1 ▶ defined $nread ? $nread : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $nread ? : 1 0 1
623 }
624  
625 sub gzwrite_external {
626 # Using syswrite() is okay (cf. gzread_external())
627 # since the bytes leave this process and buffering
628 # is therefore not an issue.
629 1 ▶ my $nwrote = syswrite($_[0], $_[1]);
Subroutine gzwrite_external: called CC=2 SCAR=7.2
Pod undocumented
630 1 ▶ defined $nwrote ? $nwrote : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $nwrote ? : 1 0 1
631 }
632  
633 sub gzreadline_external {
634 # See the comment in gzread_external().
635 0 ▶ $_[1] = readline($_[0]);
Subroutine gzreadline_external: not called CC=2 SCAR=17.9
Pod undocumented
636 0 ▶ return defined $_[1] ? length($_[1]) : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
637 }
638  
639 sub gzeof_external {
640 2 ▶ return eof($_[0]);
Subroutine gzeof_external: called CC=1 SCAR=0.0
Pod undocumented
641 }
642  
643 sub gzclose_external {
644 2 ▶ close($_[0]);
Subroutine gzclose_external: called CC=1 SCAR=0.0
Pod undocumented
645 # I am not entirely certain why this is needed but it seems
646 # the above close() always fails (as if the stream would have
647 # been already closed - something to do with using external
648 # processes via pipes?)
649 2 return 0;
650 }
651  
652 9 1;
↑ Summary