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   134712 use warnings;
  2         8  
  2         71  
5 2     2   16 use strict;
  2         34  
  2         48  
6 2     2   10 use Carp;
  2         6  
  2         130  
7 2     2   16 use warnings::register;
  2         4  
  2         243  
8 2     2   13 use Scalar::Util qw/blessed/;
  2         5  
  2         349  
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   42 require Tie::Handle::Base;
18 2         1354 our @ISA = qw/ Tie::Handle::Base /; ## no critic (ProhibitExplicitISA)
19             }
20              
21             sub TIEHANDLE {
22 17 100   17   49809 @_==2 or croak __PACKAGE__."->TIEHANDLE: bad number of args";
23 16         47 my ($class,$repl) = @_;
24 16 100 100     365 croak "$class->TIEHANDLE: argument must be a File::Replace object"
25             unless blessed($repl) && $repl->isa('File::Replace');
26 14         54 my $self = $class->SUPER::TIEHANDLE($repl->in_fh);
27 14         224 $self->{repl} = $repl;
28 14         45 return $self;
29             }
30              
31 4     4 0 115 sub replace { return shift->{repl} }
32 4     4 0 7057 sub in_fh { return shift->{repl}->in_fh }
33 4     4 0 1733 sub out_fh { return shift->{repl}->out_fh }
34              
35             sub OPEN {
36 8     8   4888 my $self = shift;
37 8 100 100     226 croak "this handle only supports 2- or 3-arg open" unless @_==1||@_==2;
38 6 100       259 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       119 $opts->{layers} = @_==2 ? shift : undef;
42 4         9 my $filename = shift;
43             # just let the previous $self->{repl} get destroyed here
44 4         28 $self->{repl} = File::Replace->new($filename, %$opts);
45 4         2606 $self->set_inner_handle($self->{repl}->in_fh);
46 4         475 return 1;
47             }
48              
49             sub CLOSE {
50 11     11   3811 my $self = shift;
51 11         44 return !!$self->{repl}->finish;
52             }
53              
54             sub WRITE {
55 13     13   6110 my $self = shift;
56 13         47 $self->inner_write($self->{repl}->out_fh, @_);
57             }
58              
59             sub BINMODE {
60 11     11   3244 my $self = shift;
61 11 100       34 if (@_)
62             { return binmode($self->{repl}->in_fh, $_[0])
63 5   100     22 && binmode($self->{repl}->out_fh, $_[0]) }
64             else
65             { return binmode($self->{repl}->in_fh)
66 6   100     22 && 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   1486 sub FILENO { return shift->{repl}->is_open ? -1 : undef }
72              
73             sub UNTIE {
74 1     1   3 my $self = shift;
75 1         154 warnings::warnif("Please don't untie ".ref($self)." handles");
76 1         43 $self->{repl} = undef;
77 1         5 $self->SUPER::UNTIE(@_);
78             }
79              
80             sub DESTROY {
81 14     14   9445 my $self = shift;
82             # File::Replace destructor will warn on unclosed file
83 14         48 $self->{repl} = undef;
84 14         781 $self->SUPER::DESTROY(@_);
85             }
86              
87             1;
88             __END__