blib/lib/clobber.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 122 CC 15 SCAR 36.8
CC
15
Cov
0%
OPEN
90.0
prompt
20.0
unimport
12.0
CRAP 39.6
1 package clobber;
2 use Carp;
3 use Fcntl;
4 use strict; no strict 'refs';
5 use vars '$VERSION'; $VERSION = 0.04;
6 eval "require Term::ReadKey";
7  
8 sub unimport { #no strict 'refs';
9 *{'CORE::GLOBAL::open'} = \&OPEN unless exists($^H{clobber});
10 *{'CORE::GLOBAL::sysopen'} = \&SYSOPEN unless exists($^H{clobber});
11 $^H{clobber} = $ENV{'clobber.pm'} || 0;
12 }
13  
14 sub import {
15 $^H{clobber} = 1;
16 }
17  
18  
19 sub OPEN(*;$@){
20 my($handle, $mode, $file) = @_;
21 my($testmode, $pipein) = $mode;
22  
23 if( scalar(@_) == 1 ){ #no strict 'refs';
24 $mode = ${caller(1).'::'.$handle};
25 }
26  
27 if( scalar(@_) == 2 ){
28 #Convert 2-arg to 3-arg...
29 #Initially tried to simply pass @_ through to CORE::open,
30 #but it's prototype didn't like that
31  
32 #put into sub for /x, and easier "testing"?
33 if( $mode =~ /^(\+?(?:>{1,2}|<)|(?:>&=?|<&=?|\|))?\s*(.+)\s*(\|)?$/ ){
34 ($testmode, $file, $pipein) = ($1, $2, $3);
35 }
36 else{
37 croak "Failed to parse EXPR of 2-arg open: $_[1]";
38 }
39  
40 $testmode = $1 eq '|' ? '|-' : $1;
41 unless( defined $testmode ){
42 $testmode = $pipein ? '-|' : '>';
43 }
44 }
45 elsif( scalar(@_) > 2 ){
46 ($testmode, $file) = @_[1,2];
47 }
48  
49 prompt($file) if -e $file && $testmode =~ /\+[<>](?!>)|^>(?!&|>)/;
50  
51 splice(@_, 0, 3);
52  
53 #no strict 'refs';
54 CORE::open(*{caller(0) . '::' . $handle}, $testmode, $file, @_);
55 }
56  
57 sub SYSOPEN(*$$;$){
58 my($handle, $file, $mode, $perms) = @_;
59  
60 #We don't use O_EXCL because sysopen's failure is not trappable
61 prompt($file) if -e $file && $mode&(O_WRONLY|O_RDWR|O_TRUNC);
62  
63 #no strict 'refs';
64 CORE::sysopen(*{caller(0) . '::' . $handle}, $file, $mode, $perms||0666);
65 }
66  
67 sub prompt{
68 my $clobber = 0;
69  
70 return if (caller 1)[10]->{clobber};
71  
72 if( -t STDIN && exists($INC{'Term/ReadKey.pm'}) ){
73  
74 select(STDERR); local $|=1;
75 print STDERR "Allow modification of '$_[0]'? [yN] ";
76  
77 Term::ReadKey::ReadMode('cbreak'); $clobber = Term::ReadKey::ReadKey(0);
78  
79 Term::ReadKey::ReadMode('restore'); print STDERR "\n";
80  
81 $clobber =~ y/yY/1/; $clobber =~ y/1/0/c;
82 }
83  
84 croak "$_[0]: File exists" unless $clobber;
85 }
86  
87  
88 1;
89 __END__