File Coverage

blib/lib/File/Replace/Inplace.pm
Criterion Covered Total %
statement 82 82 100.0
branch 26 26 100.0
condition 6 6 100.0
subroutine 15 15 100.0
pod 0 2 0.0
total 129 131 98.4


line stmt bran cond sub pod time code
1             #!perl
2             package File::Replace::Inplace;
3 2     2   166725 use warnings;
  2         17  
  2         67  
4 2     2   11 use strict;
  2         6  
  2         91  
5 2     2   12 use Carp;
  2         4  
  2         859  
6              
7             # For AUTHOR, COPYRIGHT, AND LICENSE see the bottom of this file
8              
9             our $VERSION = '0.16';
10              
11             our @CARP_NOT = qw/ File::Replace /;
12              
13             # this var is used by File::Replace::import
14             our $GlobalInplace; ## no critic (ProhibitPackageVars)
15              
16             sub new { ## no critic (RequireArgUnpacking)
17 26     26 0 385370 my $class = shift;
18 26 100       243 croak "Useless use of $class->new in void context" unless defined wantarray;
19 25 100       221 croak "$class->new: bad number of args" if @_%2;
20 24         88 my %args = @_; # really just so we can inspect the debug option
21             my $self = {
22             _debug => ref($args{debug}) ? $args{debug} : ( $args{debug} ? *STDERR{IO} : undef),
23             _h_argv => *ARGV{IO},
24 24 100       161 };
    100          
25 24         55 tie *{$self->{_h_argv}}, 'File::Replace::Inplace::TiedArgv', @_;
  24         230  
26 23         68 bless $self, $class;
27 23         128 $self->_debug("$class->new: tied ARGV\n");
28 23         158 return $self;
29             }
30             *_debug = \&File::Replace::_debug; ## no critic (ProtectPrivateVars)
31             sub cleanup {
32 26     26 0 84 my $self = shift;
33 26 100 100     98 if ( defined($self->{_h_argv}) && defined( my $tied = tied(*{$self->{_h_argv}}) ) ) {
  24         158  
34 21 100       130 if ( $tied->isa('File::Replace::Inplace::TiedArgv') ) {
35 20         108 $self->_debug(ref($self)."->cleanup: untieing ARGV\n");
36 20         104 untie *{$self->{_h_argv}};
  20         98  
37             }
38 21         166 delete $self->{_h_argv};
39             }
40 26         52 delete $self->{_debug};
41 26         150 return 1;
42             }
43 23     23   94974 sub DESTROY { return shift->cleanup }
44              
45             {
46             ## no critic (ProhibitMultiplePackages)
47             package # hide from pause
48             File::Replace::Inplace::TiedArgv;
49 2     2   16 use Carp;
  2         12  
  2         116  
50 2     2   486 use File::Replace;
  2         10031  
  2         13  
51            
52             BEGIN {
53 2     2   603 require Tie::Handle::Argv;
54 2         1308 our @ISA = qw/ Tie::Handle::Argv /; ## no critic (ProhibitExplicitISA)
55             }
56            
57             # this is mostly the same as %NEW_KNOWN_OPTS from File::Replace,
58             # except without "in_fh" (note "debug" is also passed to the superclass)
59             my %TIEHANDLE_KNOWN_OPTS = map {$_=>1} qw/ debug layers create chmod
60             perms autocancel autofinish backup files filename /;
61            
62             sub TIEHANDLE { ## no critic (RequireArgUnpacking)
63 26 100 100 26   1813 croak __PACKAGE__."->TIEHANDLE: bad number of args" unless @_ && @_%2;
64 24         78 my ($class,%args) = @_;
65 24         95 for (keys %args) { croak "$class->tie/new: unknown option '$_'"
66 30 100       246 unless $TIEHANDLE_KNOWN_OPTS{$_} }
67 23 100       62 my %superargs = map { exists($args{$_}) ? ($_=>$args{$_}) : () }
  69         201  
68             qw/ files filename debug /;
69 23         66 delete @args{qw/ files filename /};
70 23         131 my $self = $class->SUPER::TIEHANDLE( %superargs );
71 23         70 $self->{_repl_opts} = \%args;
72 23         80 return $self;
73             }
74            
75             sub OPEN {
76 48     48   879 my $self = shift;
77 48 100       403 croak "bad number of arguments to open" unless @_==1;
78 46         86 my $filename = shift;
79 46 100       98 if ($filename eq '-') {
80 1         6 $self->_debug(ref($self).": Reading from STDIN, writing to STDOUT");
81 1         5 $self->set_inner_handle(*STDIN{IO});
82 1         7 select(STDOUT); ## no critic (ProhibitOneArgSelect)
83             }
84             else {
85 45         69 $self->{_repl} = File::Replace->new($filename, %{$self->{_repl_opts}} );
  45         333  
86 44         27448 $self->set_inner_handle($self->{_repl}->in_fh);
87 44         434 *ARGVOUT = $self->{_repl}->out_fh; ## no critic (RequireLocalizedPunctuationVars)
88 44         265 select(ARGVOUT); ## no critic (ProhibitOneArgSelect)
89             }
90 45         165 return 1;
91             }
92            
93             sub inner_close {
94 49     49   82 my $self = shift;
95 49 100       120 if ( $self->{_repl} ) {
96 43         168 $self->{_repl}->finish;
97 43         8153 $self->{_repl} = undef;
98             }
99 49         1622 return 1;
100             }
101            
102             sub sequence_end {
103 21     21   39 my $self = shift;
104 1         9 $self->set_inner_handle(\do{local*HANDLE;*HANDLE}) ## no critic (RequireInitializationForLocalVars)
  1         6  
105 21 100       76 if $self->innerhandle==*STDIN{IO};
106 21         147 select(STDOUT); ## no critic (ProhibitOneArgSelect)
107 21         42 return;
108             }
109            
110             sub UNTIE {
111 22     22   6322 my $self = shift;
112 22         68 select(STDOUT); ## no critic (ProhibitOneArgSelect)
113 22         96 delete @$self{ grep {/^_[^_]/} keys %$self };
  149         437  
114 22         600 return $self->SUPER::UNTIE(@_);
115             }
116            
117             sub DESTROY {
118 23     23   93 my $self = shift;
119 23         61 select(STDOUT); ## no critic (ProhibitOneArgSelect)
120             # File::Replace destructor will warn on unclosed file
121 23         70 delete @$self{ grep {/^_[^_]/} keys %$self };
  6         41  
122 23         96 return $self->SUPER::DESTROY(@_);
123             }
124            
125             }
126              
127             1;