blib/lib/IO/ReStoreFH.pm

filter: Statementstmt 97.3% 73 / 75 Branchbran 75.0% 24 / 32 Conditioncond 53.3% 8 / 15 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 94.1% 16 / 17 Podpod 100% 3 / 3 totaltotal 83.5% 127 / 152 CC 23 SCAR 16.9
CC
23
Cov
86%
store
9.2
_dup_mode
8.9
restore
6.1
CRAP 5.4
↑ Summary Build »
1 package IO::ReStoreFH;
2  
3 # ABSTRACT: store/restore file handles
4  
5 5 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '0.12';
11  
12 # In Perl 5.10.1 a use or require of FileHandle or something in the
13 # FileHandle hierarchy (like FileHandle::Fmode, below) will cause the
14 # compiler to creat a stash for FileHandle. Then, there's some
15 # code in Perl_newio which checks if FileHandle has been loaded (just
16 # by checking for the stash) and aliases it to IO::Handle.
17 #
18 # This it mucks up method calls on filehandles if FileHandle isn't
19 # actually loaded, resulting in errors such as
20 #
21 # Can't locate object method "getline" via package "FileHandle"
22 #
23 # see http://perlmonks.org/?node_id=1073753, and tobyink's reply
24  
25 # So, we explicitly load FileHandle on 5.10.x to avoid these action
26 # at a distance problems.
27 5 ▶use if $^V >= v5.10.0 && $^V <= v5.11.0, 'FileHandle';
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
$^V >= v5.10.0 && $^V <= v5.11.0
!ll&&!rl&&r
050
MC/DC0%0/2
$^V >= v5.10.0 && $^V <= v5.11.0
$^V >= v5.10.0 $^V <= v5.11.0
Truth table33%1/3
A && B
A= $^V >= v5.10.0B= $^V <= v5.11.0
ABresult
0X0
100
111
28  
29 5 ▶use FileHandle::Fmode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use Try::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 sub _croak {
36 4 ▶ require Carp;
Subroutine _croak: called CC=1 SCAR=0.0
37 4 goto &Carp::croak;
38 }
39  
40 sub _dup_mode {
41  
42 7 ▶ my ( $fh ) = @_;
Subroutine _dup_mode: called CC=8 SCAR=21.9
43  
44 7 ▶ return '<&' if FileHandle::Fmode::is_RO( $fh );
Branch: 1 branch
BranchTrueFalseTotal
if FileHandle::Fmode::is_RO($fh) 0 7 7
45  
46 7 my $is_WO = FileHandle::Fmode::is_WO( $fh );
47 7 my $is_A;
48  
49 7 ▶ if ( $is_WO ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_WO) 4 3 7
50 4 $is_A = FileHandle::Fmode::is_A( $fh );
51 4 ▶ return '>>&' if $is_A;
Branch: 1 branch
BranchTrueFalseTotal
if $is_A 1 3 4
52 3 return '>&';
53 }
54  
55 3 ▶ if ( FileHandle::Fmode::is_W( $fh )
Branch: 1 branch
BranchTrueFalseTotal
if (FileHandle::Fmode::is_W($fh) and FileHandle::Fmode::is_R($fh)) 3 0 3
Condition33%1/3
FileHandle::Fmode::is_W($fh) and FileHandle::Fmode::is_R($fh)
!ll&&!rl&&r
003
MC/DC0%0/2
FileHandle::Fmode::is_W($fh) and FileHandle::Fmode::is_R($fh)
FileHandle::Fmode::is_W($fh) FileHandle::Fmode::is_R($fh)
Truth table33%1/3
A and B
A= FileHandle::Fmode::is_W($fh)B= FileHandle::Fmode::is_R($fh)
ABresult
0X0
100
111
56 && FileHandle::Fmode::is_R( $fh ) )
57 {
58 3 ▶ $is_A //= FileHandle::Fmode::is_A( $fh );
Condition33%1/3
$is_A //= FileHandle::Fmode::is_A($fh)
l!l&&r!l&&!r
030
MC/DC0%0/2
$is_A //= FileHandle::Fmode::is_A($fh)
$is_A FileHandle::Fmode::is_A($fh)
Truth table33%1/3
A //= B
A= $is_AB= FileHandle::Fmode::is_A($fh)
ABresult
1X1
011
000
59 3 ▶ return '+>>&' if $is_A;
Branch: 1 branch
BranchTrueFalseTotal
if $is_A 1 2 3
60 2 return '+<&';
61 }
62  
63 0 return undef;
64 }
65  
66 sub new {
67 12 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
68  
69 12 my $obj = bless { dups => [] }, $class;
70 12 $obj->store( $_ ) for @_;
71 8 return $obj;
72 }
73  
74 sub store {
75 12 ▶ my ( $self, $fh ) = @_;
Subroutine store: called CC=9 SCAR=22.2
Pod documented
76  
77 # if $fh is a reference, or a GLOB, it's probably
78 # a filehandle object of somesort
79  
80 12 ▶ if ( ref( $fh ) || 'GLOB' eq ref( \$fh ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $fh or 'GLOB' eq ref \$fh) { } 10 2 12
elsif (&Scalar::Util::looks_like_number($fh) and POSIX::ceil($fh) == POSIX::floor($fh)) { } 1 1 2
Condition100%3/3
ref $fh or "GLOB" eq ref \$fh
l!l&&r!l&&!r
912
MC/DC100%2/2
ref $fh or "GLOB" eq ref \$fh
ref $fh "GLOB" eq ref \$fh
Truth table100%3/3
A or B
A= ref $fhB= "GLOB" eq ref \$fh
ABresult
1X1
011
000
81  
82 # now that we are sure that everything is loaded,
83 # check if it is an open filehandle; this doesn't disambiguate
84 # between objects that aren't filehandles or closed filehandles.
85 10 ▶ _croak( "\$fh is not an open filehandle\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless FileHandle::Fmode::is_FH($fh) 3 7 10
86 unless FileHandle::Fmode::is_FH( $fh );
87  
88 # get access mode; open documentation says mode must
89 # match that of original filehandle; do the best we can
90 7 my $mode = _dup_mode( $fh );
91  
92 # give up
93 7 ▶ _croak( "inexplicable error: unable to determine mode for \$fh;\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 0 7 7
94 if !defined $mode;
95  
96 # dup the filehandle
97 7 ▶ open my $dup, $mode, $fh
Branch: 1 branch
BranchTrueFalseTotal
open my $dup, $mode, $fh or _croak("error fdopening \$fh: $!\n") 0 7 7
98 or _croak( "error fdopening \$fh: $!\n" );
99  
100 7 push @{ $self->{dups} }, { fh => $fh, mode => $mode, dup => $dup };
101 }
102  
103 ▶ elsif (Scalar::Util::looks_like_number( $fh )
Condition66%2/3
&Scalar::Util::looks_like_number($fh) and POSIX::ceil($fh) == POSIX::floor($fh)
!ll&&!rl&&r
011
MC/DC50%1/2
&Scalar::Util::looks_like_number($fh) and POSIX::ceil($fh) == POSIX::floor($fh)
&Scalar::Util::looks_like_number($fh) POSIX::ceil($fh) == POSIX::floor($fh)
Truth table66%2/3
A and B
A= &Scalar::Util::looks_like_number($fh)B= POSIX::ceil($fh) == POSIX::floor($fh)
ABresult
0X0
100
111
104 && POSIX::ceil( $fh ) == POSIX::floor( $fh ) )
105 {
106  
107 # as the caller specifically used an fd, don't go through Perl's
108 # IO system
109 1 ▶ my $dup = POSIX::dup( $fh )
Branch: 1 branch
BranchTrueFalseTotal
my $dup = POSIX::dup($fh) or _croak("error dup'ing file descriptor ${fh}: $!\n") 0 1 1
110 or _croak( "error dup'ing file descriptor $fh: $!\n" );
111  
112 1 push @{ $self->{dups} }, { fd => $fh, dup => $dup };
113 }
114  
115 else {
116 1 _croak( '$fh must be opened Perl filehandle or object or integer file descriptor', );
117 }
118  
119 8 return;
120 }
121  
122 sub restore {
123 12 ▶ my $self = shift;
Subroutine restore: called CC=6 SCAR=18.1
Pod documented
124  
125 12 my $dups = $self->{dups};
126 12 ▶ while ( my $dup = pop @{$dups} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dup = pop @{$dups;}) 8 12 20
127  
128 8 ▶ if ( exists $dup->{fd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dup->{'fd'}) { } 1 7 8
129 POSIX::dup2( $dup->{dup}, $dup->{fd} )
130 1 ▶ or _croak( "error restoring file descriptor $dup->{fd}: $!\n" );
Branch: 1 branch
BranchTrueFalseTotal
POSIX::dup2($dup->{"dup"}, $dup->{"fd"}) or _croak(qq[error restoring file descriptor $dup->{"fd"}: $!\n]) 0 1 1
131 1 POSIX::close( $dup->{dup} );
132 }
133  
134 else {
135 open( $dup->{fh}, $dup->{mode}, $dup->{dup} )
136 7 ▶ or _croak( "error restoring file handle $dup->{fh}: $!" );
Branch: 1 branch
BranchTrueFalseTotal
open $dup->{"fh"}, $dup->{"mode"}, $dup->{"dup"} or _croak(qq[error restoring file handle $dup->{"fh"}: $!]) 0 7 7
137 7 ▶ close( $dup->{dup} ) or _croak( q{error closing dup'ed filehandle} );
Branch: 1 branch
BranchTrueFalseTotal
close $dup->{"dup"} or _croak("error closing dup'ed filehandle") 0 7 7
138 }
139 }
140 12 return;
141 }
142  
143 sub DESTROY {
144 12 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.1
145 12 ▶ Try::Tiny::try { $self->restore }
Subroutine __ANON__: called CC=1 SCAR=0.0
146 12 ▶ Try::Tiny::catch { _croak $_ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
147 12 return;
148 }
149  
150 5 1;
151  
152 #
153 # This file is part of IO-ReStoreFH
154 #
155 # This software is Copyright (c) 2012 by Smithsonian Astrophysical Observatory.
156 #
157 # This is free software, licensed under:
158 #
159 # The GNU General Public License, Version 3, June 2007
160 #
161  
162 __END__
↑ Summary Build »