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   137507 use warnings;
  2         7  
  2         67  
5 2     2   12 use strict;
  2         5  
  2         37  
6 2     2   10 use Carp;
  2         4  
  2         105  
7 2     2   22 use warnings::register;
  2         4  
  2         258  
8 2     2   13 use Scalar::Util qw/blessed weaken/;
  2         5  
  2         222  
9              
10             # For AUTHOR, COPYRIGHT, AND LICENSE see the bottom of this file
11              
12             our $VERSION = '0.16';
13              
14             ## no critic (RequireFinalReturn, RequireArgUnpacking)
15              
16             BEGIN {
17 2     2   14 require Tie::Handle::Base;
18 2         1111 our @ISA = qw/ Tie::Handle::Base /; ## no critic (ProhibitExplicitISA)
19             }
20              
21             sub TIEHANDLE {
22 37 100   37   57790 @_==3 or croak __PACKAGE__."->TIEHANDLE: bad number of args";
23 36         90 my ($class,$repl,$mode) = @_;
24 36 100 100     477 croak "$class->TIEHANDLE: argument must be a File::Replace object"
25             unless blessed($repl) && $repl->isa('File::Replace');
26 34         63 my ($innerhandle,$other);
27 34 100       102 if ($mode eq 'in') {
    100          
    100          
28 15         48 $innerhandle = $repl->in_fh;
29 15         74 $other = $repl->out_fh; }
30             elsif ($mode eq 'out') {
31 15         38 $innerhandle = $repl->out_fh;
32 15         58 $other = $repl->in_fh; }
33             elsif ($mode eq 'onlyout') {
34 3         11 $innerhandle = $repl->out_fh; }
35 1         101 else { croak "bad mode" }
36 33         141 my $self = $class->SUPER::TIEHANDLE($innerhandle);
37 33         348 $self->{repl} = $repl;
38 33         58 $self->{other} = $other;
39 33         93 weaken( $self->{other} );
40 33         91 return $self;
41             }
42              
43 9     9 0 244 sub replace { return shift->{repl} }
44 1     1 0 434 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   105 sub OPEN { croak "Can't reopen a ".ref($_[0])." handle" }
48              
49             sub CLOSE {
50 21     21   8731 my $self = shift;
51 21 100 100     120 if ( defined($self->{other}) && defined(fileno($self->{other})) ) {
52             # the other file is still open, so just close this one
53 10 100       47 my $rv = $self->SUPER::CLOSE()
54             or croak "couldn't close handle: $!";
55 8         219 return $rv;
56             }
57             else { # the other file is closed, trigger the replacement now
58 11         43 return !!$self->{repl}->finish }
59             }
60              
61             sub UNTIE {
62 2     2   12 my $self = shift;
63 2         268 warnings::warnif("Please don't untie ".ref($self)." handles");
64 2         80 $self->{other} = undef;
65 2         5 $self->{repl} = undef;
66 2         11 $self->SUPER::UNTIE(@_);
67             }
68              
69             sub DESTROY {
70 33     33   11578 my $self = shift;
71             # File::Replace destructor will warn on unclosed file
72 33         61 $self->{other} = undef;
73 33         71 $self->{repl} = undef;
74 33         670 $self->SUPER::DESTROY(@_);
75             }
76              
77             1;
78             __END__