File Coverage

blib/lib/File/Replace/SingleHandle.pm
Criterion Covered Total %
statement 51 51 100.0
branch 14 14 100.0
condition 6 6 100.0
subroutine 14 14 100.0
pod 0 3 0.0
total 85 88 96.5


line stmt bran cond sub pod time code
1             #!perl
2             package # hide from pause
3             File::Replace::SingleHandle;
4 2     2   114111 use warnings;
  2         6  
  2         57  
5 2     2   9 use strict;
  2         3  
  2         33  
6 2     2   6 use Carp;
  2         4  
  2         117  
7 2     2   12 use warnings::register;
  2         11  
  2         214  
8 2     2   12 use Scalar::Util qw/blessed weaken/;
  2         2  
  2         195  
9              
10             # For AUTHOR, COPYRIGHT, AND LICENSE see the bottom of this file
11              
12             our $VERSION = '0.18';
13              
14             ## no critic (RequireFinalReturn, RequireArgUnpacking)
15              
16             BEGIN {
17 2     2   11 require Tie::Handle::Base;
18 2         956 our @ISA = qw/ Tie::Handle::Base /; ## no critic (ProhibitExplicitISA)
19             }
20              
21             sub TIEHANDLE {
22 37 100   37   48203 @_==3 or croak __PACKAGE__."->TIEHANDLE: bad number of args";
23 36         76 my ($class,$repl,$mode) = @_;
24 36 100 100     401 croak "$class->TIEHANDLE: argument must be a File::Replace object"
25             unless blessed($repl) && $repl->isa('File::Replace');
26 34         57 my ($innerhandle,$other);
27 34 100       84 if ($mode eq 'in') {
    100          
    100          
28 15         42 $innerhandle = $repl->in_fh;
29 15         61 $other = $repl->out_fh; }
30             elsif ($mode eq 'out') {
31 15         35 $innerhandle = $repl->out_fh;
32 15         47 $other = $repl->in_fh; }
33             elsif ($mode eq 'onlyout') {
34 3         9 $innerhandle = $repl->out_fh; }
35 1         97 else { croak "bad mode" }
36 33         118 my $self = $class->SUPER::TIEHANDLE($innerhandle);
37 33         291 $self->{repl} = $repl;
38 33         49 $self->{other} = $other;
39 33         76 weaken( $self->{other} );
40 33         76 return $self;
41             }
42              
43 9     9 0 209 sub replace { return shift->{repl} }
44 1     1 0 332 sub in_fh { return shift->{repl}->in_fh }
45 1     1 0 5 sub out_fh { return shift->{repl}->out_fh }
46              
47 1     1   92 sub OPEN { croak "Can't reopen a ".ref($_[0])." handle" }
48              
49             sub CLOSE {
50 21     21   7135 my $self = shift;
51 21 100 100     93 if ( defined($self->{other}) && defined(fileno($self->{other})) ) {
52             # the other file is still open, so just close this one
53 10 100       35 my $rv = $self->SUPER::CLOSE()
54             or croak "couldn't close handle: $!";
55 8         193 return $rv;
56             }
57             else { # the other file is closed, trigger the replacement now
58 11         34 return !!$self->{repl}->finish }
59             }
60              
61             sub UNTIE {
62 2     2   12 my $self = shift;
63 2         243 warnings::warnif("Please don't untie ".ref($self)." handles");
64 2         69 $self->{other} = undef;
65 2         3 $self->{repl} = undef;
66 2         10 $self->SUPER::UNTIE(@_);
67             }
68              
69             sub DESTROY {
70 33     33   9581 my $self = shift;
71             # File::Replace destructor will warn on unclosed file
72 33         55 $self->{other} = undef;
73 33         60 $self->{repl} = undef;
74 33         591 $self->SUPER::DESTROY(@_);
75             }
76              
77             1;
78             __END__