| 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.11'; | |||||||||||||||||||||||||||||||||||
| 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 | sub _croak { | ||||||||||||||||||||||||||||||||||||
| 36 | 4 | ▶ | require Carp; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_croak: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 37 | 4 | goto &Carp::croak; | |||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||
| 40 | sub new { | ||||||||||||||||||||||||||||||||||||
| 41 | 10 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||
| 43 | 10 | my $obj = bless { dups => [] }, $class; | |||||||||||||||||||||||||||||||||||
| 44 | 10 | $obj->store( $_ ) for @_; | |||||||||||||||||||||||||||||||||||
| 45 | 6 | return $obj; | |||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||
| 48 | sub store { | ||||||||||||||||||||||||||||||||||||
| 49 | 10 | ▶ | my ( $self, $fh ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
store: called CC=14 SCAR=27.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||
| 51 | # if $fh is a reference, or a GLOB, it's probably | ||||||||||||||||||||||||||||||||||||
| 52 | # a filehandle object of somesort | ||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||
| 54 | 10 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||
| 56 | # now that we are sure that everything is loaded, | ||||||||||||||||||||||||||||||||||||
| 57 | # check if it is an open filehandle; this doesn't disambiguate | ||||||||||||||||||||||||||||||||||||
| 58 | # between objects that aren't filehandles or closed filehandles. | ||||||||||||||||||||||||||||||||||||
| 59 | 8 | ▶ | _croak( "\$fh is not an open filehandle\n" ) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 60 | unless FileHandle::Fmode::is_FH( $fh ); | ||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||
| 62 | # get access mode; open documentation says mode must | ||||||||||||||||||||||||||||||||||||
| 63 | # match that of original filehandle; do the best we can | ||||||||||||||||||||||||||||||||||||
| 64 | 5 | ▶ | my $mode | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
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)
| |||||||||||||||||||||||||||||||||||||
| 65 | = FileHandle::Fmode::is_RO( $fh ) ? '<' | ||||||||||||||||||||||||||||||||||||
| 66 | : FileHandle::Fmode::is_WO( $fh ) ? '>' | ||||||||||||||||||||||||||||||||||||
| 67 | : FileHandle::Fmode::is_W( $fh ) | ||||||||||||||||||||||||||||||||||||
| 68 | && FileHandle::Fmode::is_R( $fh ) ? '+<' | ||||||||||||||||||||||||||||||||||||
| 69 | : undef; | ||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||
| 71 | # give up | ||||||||||||||||||||||||||||||||||||
| 72 | 5 | ▶ | _croak( "inexplicable error: unable to determine mode for \$fh;\n" ) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 73 | if !defined $mode; | ||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||
| 75 | 5 | ▶ | $mode .= '>' if FileHandle::Fmode::is_A( $fh ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||
| 77 | # dup the filehandle | ||||||||||||||||||||||||||||||||||||
| 78 | 5 | ▶ | open my $dup, $mode . q{&}, $fh | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 79 | or _croak( "error fdopening \$fh: $!\n" ); | ||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||
| 81 | 5 | push @{ $self->{dups} }, { fh => $fh, mode => $mode, dup => $dup }; | |||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||
| 84 | ▶ | 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)
| |||||||||||||||||||||||||||||||||||||
| 85 | && POSIX::ceil( $fh ) == POSIX::floor( $fh ) ) | ||||||||||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||
| 88 | # as the caller specifically used an fd, don't go through Perl's | ||||||||||||||||||||||||||||||||||||
| 89 | # IO system | ||||||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | my $dup = POSIX::dup( $fh ) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 91 | or _croak( "error dup'ing file descriptor $fh: $!\n" ); | ||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||
| 93 | 1 | push @{ $self->{dups} }, { fd => $fh, dup => $dup }; | |||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||||||||||
| 97 | 1 | _croak( '$fh must be opened Perl filehandle or object or integer file descriptor', ); | |||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||
| 100 | 6 | return; | |||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||
| 103 | sub restore { | ||||||||||||||||||||||||||||||||||||
| 104 | 10 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
restore: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | 10 | my $dups = $self->{dups}; | |||||||||||||||||||||||||||||||||||
| 107 | 10 | ▶ | while ( my $dup = pop @{$dups} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||
| 109 | 6 | ▶ | if ( exists $dup->{fd} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 110 | POSIX::dup2( $dup->{dup}, $dup->{fd} ) | ||||||||||||||||||||||||||||||||||||
| 111 | 1 | ▶ | or _croak( "error restoring file descriptor $dup->{fd}: $!\n" ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 112 | 1 | POSIX::close( $dup->{dup} ); | |||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||||||||||
| 116 | open( $dup->{fh}, $dup->{mode} . q{&}, $dup->{dup} ) | ||||||||||||||||||||||||||||||||||||
| 117 | 5 | ▶ | or _croak( "error restoring file handle $dup->{fh}: $!" ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 118 | 5 | ▶ | close( $dup->{dup} ) or _croak( q{error closing dup'ed filehandle} ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||
| 121 | 10 | return; | |||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||
| 124 | sub DESTROY { | ||||||||||||||||||||||||||||||||||||
| 125 | 10 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||||
| 126 | 10 | ▶ | Try::Tiny::try { $self->restore } | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 127 | 10 | ▶ | Try::Tiny::catch { _croak $_ }; | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||
| 128 | 10 | return; | |||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||
| 131 | 5 | 1; | |||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||
| 133 | # | ||||||||||||||||||||||||||||||||||||
| 134 | # This file is part of IO-ReStoreFH | ||||||||||||||||||||||||||||||||||||
| 135 | # | ||||||||||||||||||||||||||||||||||||
| 136 | # This software is Copyright (c) 2012 by Smithsonian Astrophysical Observatory. | ||||||||||||||||||||||||||||||||||||
| 137 | # | ||||||||||||||||||||||||||||||||||||
| 138 | # This is free software, licensed under: | ||||||||||||||||||||||||||||||||||||
| 139 | # | ||||||||||||||||||||||||||||||||||||
| 140 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||||||||||
| 141 | # | ||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||||||||||