| 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.13'; | |||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||
| 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 | 4 | ▶ | sub _croak { | ||||||||||||||||||||||||||||||
|
Subroutine
_croak: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 36 | 4 | require Carp; | |||||||||||||||||||||||||||||||
| 37 | 4 | goto &Carp::croak; | |||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 7 | ▶ | sub _dup_mode { | ||||||||||||||||||||||||||||||
|
Subroutine
_dup_mode: called CC=8 SCAR=21.9
| |||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 7 | my ( $fh ) = @_; | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 7 | ▶ | return '<&' if FileHandle::Fmode::is_RO( $fh ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 50 | 4 | $is_A = FileHandle::Fmode::is_A( $fh ); | |||||||||||||||||||||||||||||||
| 51 | 4 | ▶ | return '>>&' if $is_A; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | 3 | return '>&'; | |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 3 | ▶ | if ( FileHandle::Fmode::is_W( $fh ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
FileHandle::Fmode::is_W($fh) and FileHandle::Fmode::is_R($fh)
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)
| |||||||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||||||
| 59 | 3 | ▶ | return '+>>&' if $is_A; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | 2 | return '+<&'; | |||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 0 | return undef; | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 12 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 67 | 12 | my $class = shift; | |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 12 | my $obj = bless { dups => [] }, $class; | |||||||||||||||||||||||||||||||
| 70 | 12 | $obj->store( $_ ) for @_; | |||||||||||||||||||||||||||||||
| 71 | 8 | return $obj; | |||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 12 | ▶ | sub store { | ||||||||||||||||||||||||||||||
|
Subroutine
store: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 75 | 12 | my ( $self, $fh ) = @_; | |||||||||||||||||||||||||||||||
| 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
Condition100%3/3
ref $fh or "GLOB" eq ref \$fh
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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 94 | if !defined $mode; | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | # dup the filehandle | ||||||||||||||||||||||||||||||||
| 97 | 7 | ▶ | open my $dup, $mode, $fh | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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 | 12 | ▶ | sub restore { | ||||||||||||||||||||||||||||||
|
Subroutine
restore: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 123 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 12 | my $dups = $self->{dups}; | |||||||||||||||||||||||||||||||
| 126 | 12 | ▶ | while ( my $dup = pop @{$dups} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | 8 | ▶ | if ( exists $dup->{fd} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 129 | POSIX::dup2( $dup->{dup}, $dup->{fd} ) | ||||||||||||||||||||||||||||||||
| 130 | 1 | ▶ | or _croak( "error restoring file descriptor $dup->{fd}: $!\n" ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 137 | 7 | ▶ | close( $dup->{dup} ) or _croak( q{error closing dup'ed filehandle} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | 12 | return; | |||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 12 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||
| 144 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||
| 145 | 12 | ▶ | Try::Tiny::try { $self->restore } | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | 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__ | ||||||||||||||||||||||||||||||||