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