File Coverage

blib/lib/File/Replace/DualHandle.pm
Criterion Covered Total %
statement 51 51 100.0
branch 14 14 100.0
condition 12 12 100.0
subroutine 17 17 100.0
pod 0 3 0.0
total 94 97 96.9


line stmt bran cond sub pod time code
1             #!perl
2             package # hide from pause
3             File::Replace::DualHandle;
4 2     2   110432 use warnings;
  2         8  
  2         55  
5 2     2   9 use strict;
  2         3  
  2         32  
6 2     2   8 use Carp;
  2         3  
  2         105  
7 2     2   12 use warnings::register;
  2         3  
  2         185  
8 2     2   10 use Scalar::Util qw/blessed/;
  2         4  
  2         157  
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   13 require Tie::Handle::Base;
18 2         1151 our @ISA = qw/ Tie::Handle::Base /; ## no critic (ProhibitExplicitISA)
19             }
20              
21             sub TIEHANDLE {
22 17 100   17   39565 @_==2 or croak __PACKAGE__."->TIEHANDLE: bad number of args";
23 16         38 my ($class,$repl) = @_;
24 16 100 100     306 croak "$class->TIEHANDLE: argument must be a File::Replace object"
25             unless blessed($repl) && $repl->isa('File::Replace');
26 14         43 my $self = $class->SUPER::TIEHANDLE($repl->in_fh);
27 14         168 $self->{repl} = $repl;
28 14         44 return $self;
29             }
30              
31 4     4 0 91 sub replace { return shift->{repl} }
32 4     4 0 5732 sub in_fh { return shift->{repl}->in_fh }
33 4     4 0 1381 sub out_fh { return shift->{repl}->out_fh }
34              
35             sub OPEN {
36 8     8   3824 my $self = shift;
37 8 100 100     191 croak "this handle only supports 2- or 3-arg open" unless @_==1||@_==2;
38 6 100       205 croak "layers/filename may not contain an open mode (<, >, etc.)"
39             if $_[0]=~/^\s*\+?[<>]/;
40 4         18 my $opts = $self->{repl}->options; # old options to copy over
41 4 100       92 $opts->{layers} = @_==2 ? shift : undef;
42 4         8 my $filename = shift;
43             # just let the previous $self->{repl} get destroyed here
44 4         23 $self->{repl} = File::Replace->new($filename, %$opts);
45 4         2063 $self->set_inner_handle($self->{repl}->in_fh);
46 4         381 return 1;
47             }
48              
49             sub CLOSE {
50 11     11   2958 my $self = shift;
51 11         36 return !!$self->{repl}->finish;
52             }
53              
54             sub WRITE {
55 13     13   4812 my $self = shift;
56 13         41 $self->inner_write($self->{repl}->out_fh, @_);
57             }
58              
59             sub BINMODE {
60 11     11   2646 my $self = shift;
61 11 100       40 if (@_)
62             { return binmode($self->{repl}->in_fh, $_[0])
63 5   100     17 && binmode($self->{repl}->out_fh, $_[0]) }
64             else
65             { return binmode($self->{repl}->in_fh)
66 6   100     19 && binmode($self->{repl}->out_fh) }
67             }
68             # fileno: "If there is no real file descriptor at the OS level, ... -1 is returned."
69             # since we have two underlying handles, which one the user wants is ambiguous, so just return -1,
70             # this way the check defined(fileno($fh)) for whether the file is open still works
71 2 100   2   1234 sub FILENO { return shift->{repl}->is_open ? -1 : undef }
72              
73             sub UNTIE {
74 1     1   3 my $self = shift;
75 1         134 warnings::warnif("Please don't untie ".ref($self)." handles");
76 1         35 $self->{repl} = undef;
77 1         4 $self->SUPER::UNTIE(@_);
78             }
79              
80             sub DESTROY {
81 14     14   7681 my $self = shift;
82             # File::Replace destructor will warn on unclosed file
83 14         37 $self->{repl} = undef;
84 14         600 $self->SUPER::DESTROY(@_);
85             }
86              
87             1;
88             __END__