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   139443 use warnings;
  2         9  
  2         56  
4 2     2   9 use strict;
  2         4  
  2         31  
5 2     2   8 use Carp;
  2         4  
  2         699  
6              
7             # For AUTHOR, COPYRIGHT, AND LICENSE see Inplace.pod
8              
9             our $VERSION = '0.18';
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 319246 my $class = shift;
18 26 100       244 croak "Useless use of $class->new in void context" unless defined wantarray;
19 25 100       278 croak "$class->new: bad number of args" if @_%2;
20 24         85 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       153 };
    100          
25 24         48 tie *{$self->{_h_argv}}, 'File::Replace::Inplace::TiedArgv', @_;
  24         211  
26 23         54 bless $self, $class;
27 23         121 $self->_debug("$class->new: tied ARGV\n");
28 23         134 return $self;
29             }
30             *_debug = \&File::Replace::_debug; ## no critic (ProtectPrivateVars)
31             sub cleanup {
32 26     26 0 59 my $self = shift;
33 26 100 100     97 if ( defined($self->{_h_argv}) && defined( my $tied = tied(*{$self->{_h_argv}}) ) ) {
  24         138  
34 21 100       106 if ( $tied->isa('File::Replace::Inplace::TiedArgv') ) {
35 20         103 $self->_debug(ref($self)."->cleanup: untieing ARGV\n");
36 20         85 untie *{$self->{_h_argv}};
  20         82  
37             }
38 21         142 delete $self->{_h_argv};
39             }
40 26         46 delete $self->{_debug};
41 26         120 return 1;
42             }
43 23     23   78978 sub DESTROY { return shift->cleanup }
44              
45             {
46             ## no critic (ProhibitMultiplePackages)
47             package # hide from pause
48             File::Replace::Inplace::TiedArgv;
49 2     2   26 use Carp;
  2         5  
  2         96  
50 2     2   382 use File::Replace;
  2         8102  
  2         11  
51            
52             BEGIN {
53 2     2   486 require Tie::Handle::Argv;
54 2         1155 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   1526 croak __PACKAGE__."->TIEHANDLE: bad number of args" unless @_ && @_%2;
64 24         70 my ($class,%args) = @_;
65 24         91 for (keys %args) { croak "$class->tie/new: unknown option '$_'"
66 30 100       234 unless $TIEHANDLE_KNOWN_OPTS{$_} }
67 23 100       53 my %superargs = map { exists($args{$_}) ? ($_=>$args{$_}) : () }
  69         172  
68             qw/ files filename debug /;
69 23         119 delete @args{qw/ files filename /};
70 23         153 my $self = $class->SUPER::TIEHANDLE( %superargs );
71 23         50 $self->{_repl_opts} = \%args;
72 23         72 return $self;
73             }
74            
75             sub OPEN {
76 48     48   916 my $self = shift;
77 48 100       664 croak "bad number of arguments to open" unless @_==1;
78 46         65 my $filename = shift;
79 46 100       90 if ($filename eq '-') {
80 1         5 $self->_debug(ref($self).": Reading from STDIN, writing to STDOUT");
81 1         4 $self->set_inner_handle(*STDIN{IO});
82 1         6 select(STDOUT); ## no critic (ProhibitOneArgSelect)
83             }
84             else {
85 45         70 $self->{_repl} = File::Replace->new($filename, %{$self->{_repl_opts}} );
  45         282  
86 44         22918 $self->set_inner_handle($self->{_repl}->in_fh);
87 44         372 *ARGVOUT = $self->{_repl}->out_fh; ## no critic (RequireLocalizedPunctuationVars)
88 44         243 select(ARGVOUT); ## no critic (ProhibitOneArgSelect)
89             }
90 45         144 return 1;
91             }
92            
93             sub inner_close {
94 49     49   78 my $self = shift;
95 49 100       121 if ( $self->{_repl} ) {
96 43         166 $self->{_repl}->finish;
97 43         7480 $self->{_repl} = undef;
98             }
99 49         1346 return 1;
100             }
101            
102             sub sequence_end {
103 21     21   33 my $self = shift;
104 1         6 $self->set_inner_handle(\do{local*HANDLE;*HANDLE}) ## no critic (RequireInitializationForLocalVars)
  1         5  
105 21 100       81 if $self->innerhandle==*STDIN{IO};
106 21         129 select(STDOUT); ## no critic (ProhibitOneArgSelect)
107 21         35 return;
108             }
109            
110             sub UNTIE {
111 22     22   5459 my $self = shift;
112 22         52 select(STDOUT); ## no critic (ProhibitOneArgSelect)
113 22         89 delete @$self{ grep {/^_[^_]/} keys %$self };
  149         362  
114 22         472 return $self->SUPER::UNTIE(@_);
115             }
116            
117             sub DESTROY {
118 23     23   102 my $self = shift;
119 23         51 select(STDOUT); ## no critic (ProhibitOneArgSelect)
120             # File::Replace destructor will warn on unclosed file
121 23         63 delete @$self{ grep {/^_[^_]/} keys %$self };
  6         33  
122 23         96 return $self->SUPER::DESTROY(@_);
123             }
124            
125             }
126              
127             1;