blib/lib/File/Searcher/Interactive.pm

filter: Statementstmt 14.8% 19 / 128 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 52 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 71.4% 5 / 7 Podpod 0.0% 0 / 1 totaltotal 8.4% 24 / 283 CC 52 SCAR 78.1
CC
52
Cov
8%
_process_file
2756.0
interactive
2.0
BEGIN
1.0
CRAP 2471.0
↑ Summary
1 package File::Searcher::Interactive;
2 1 ▶use File::Searcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Term::Prompt;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Term::ANSIColor qw/:constants/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 $Term::ANSIColor::AUTORESET = 1;
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 @ISA = qw(File::Searcher);
9 1 $VERSION = '0.9';
10
11
12 0 ▶sub interactive{
Subroutine interactive: not called CC=1 SCAR=6.9
Pod undocumented
13 0 my $self = shift;
14 0 $self->{_interactive} = 1;
15 0 $self->start;
16 }
17
18 0 ▶sub _process_file{
Subroutine _process_file: not called CC=52 SCAR=79.2
19 0 my $self = shift;
20 0 my ($file) = @_;
21 0 my @expressions = @{$self->{_expressions}};
22
23 0 my $contents='';
24 0 ▶ $self->_backup($file) if $self->do_backup;
Branch: 1 branch
BranchTrueFalseTotal
if $self->do_backup 0 0 0
25 0 ▶ $self->_archive($file) if $self->do_archive;
Branch: 1 branch
BranchTrueFalseTotal
if $self->do_archive 0 0 0
26 0 $self->{_search}->add_files_matched($file);
27
28 0 my $lock = new File::Flock $file;
29 0 ▶ open(FILE, $file) || die "Cannot read file $file\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or die "Cannot read file $file\n" 0 0 0
30 0 my @contents = <FILE>;
31 0 $contents = join("", @contents);
32 0 foreach my $expression (@expressions)
33 {
34 0 ▶ next if $self->{_search}->expression($expression)->skip_expression;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_search"}->expression($expression)->skip_expression 0 0 0
35 0 my ($match_cnt,$replace_cnt,$skip_next,$do_file,$do_all,$new_match_cnt,$re_do) = (0)x7;
36 0 $self->{_search}->expression($expression)->add_files_matched($file);
37 0 $do_all = $self->expression($expression)->do_all;
38 0 my $preview = '';
39 0 my $more = 1;
40 0 my ($search, $replace, $options_search, $options_replace) = $self->_get_search($expression);
41 0 $search = "(?$options_search)$search";
42 0 ▶ while($contents =~ m/$search/g)
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /$search/g) 0 0 0
43 {
44 0 my $match = Match->new(match=>$&,pre=>$`,post=>$',last=>$+,start_offset=>@-,end_offset=>@+,contents=>$contents);
45
46 0 my $return_status = 0;
47 0 ▶ $return_status = $self->{_on_expression_match}->($match, $self->{_search}->expression($expression)) if $do_file != 1 && $do_all != 1 && $new_match_cnt < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $do_file != 1 and $do_all != 1 and $new_match_cnt < 1 0 0 0
Condition0%0/6
$do_file != 1 and $do_all != 1 and $new_match_cnt < 1
!ll&&!rl&&r
000
$do_file != 1 and $do_all != 1
!ll&&!rl&&r
000
MC/DC0%0/3
$do_file != 1 and $do_all != 1 and $new_match_cnt < 1
$do_file != 1 $do_all != 1 $new_match_cnt < 1
Truth table0%0/4
A and B and C
A= $do_file != 1B= $do_all != 1C= $new_match_cnt < 1
ABCresult
0XX0
10X0
1100
1111
48 0 ▶ $contents = $return_status and next if $return_status !~ /\d+/ && $return_status ne '';
Branch: 1 branch
BranchTrueFalseTotal
if not $return_status =~ /\d+/ and $return_status ne "" 0 0 0
Condition0%0/8
!($return_status =~ /\d+/) && $return_status ne '' && (($contents = $return_status) && (next))
!ll&&!rl&&r
000
not $return_status =~ /\d+/ and $return_status ne ""
!ll&&!rl&&r
000
$contents = $return_status and next
!ll
00
MC/DC0%0/3
!($return_status =~ /\d+/) && $return_status ne '' && (($contents = $return_status) && (next))
not $return_status =~ /\d+/ $return_status ne "" $contents = $return_status
Truth table0%0/4
not(A) and B && C
A= not $return_status =~ /\d+/B= $return_status ne ""C= $contents = $return_status
ABCresult
0XX0
10X0
1100
1111
49 0 ▶ $self->expression($expression)->do_all(1) and $do_all = 1 if $return_status == 100;
Branch: 1 branch
BranchTrueFalseTotal
if $return_status == 100 0 0 0
Condition0%0/6
$return_status == 100 && ($self->expression($expression)->do_all(1) && ($do_all = 1))
!ll&&!rl&&r
000
$self->expression($expression)->do_all(1) and $do_all = 1
!ll&&!rl&&r
000
MC/DC0%0/3
$return_status == 100 && ($self->expression($expression)->do_all(1) && ($do_all = 1))
$return_status == 100 $self->expression($expression)->do_all(1) $do_all = 1
Truth table0%0/4
A && B and C
A= $return_status == 100B= $self->expression($expression)->do_all(1)C= $do_all = 1
ABCresult
0XX0
10X0
1100
1111
50 0 ▶ $do_file = 1 if $return_status == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $return_status == 10 0 0 0
51 0 ▶ $self->expression($expression)->skip_expression(1) and last if $return_status == -100;
Branch: 1 branch
BranchTrueFalseTotal
if $return_status == -100 0 0 0
Condition0%0/5
$return_status == -100 && ($self->expression($expression)->skip_expression(1) && (last))
!ll&&!rl&&r
000
$self->expression($expression)->skip_expression(1) and last
!ll
00
MC/DC0%0/2
$return_status == -100 && ($self->expression($expression)->skip_expression(1) && (last))
$return_status == -100 $self->expression($expression)->skip_expression(1)
Truth table0%0/3
A && B
A= $return_status == -100B= $self->expression($expression)->skip_expression(1)
ABresult
0X0
100
111
52 0 ▶ last if $return_status == -10;
Branch: 1 branch
BranchTrueFalseTotal
if $return_status == -10 0 0 0
53 0 ▶ $skip_next = 1 if $return_status == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $return_status == -1 0 0 0
54
55 0 ▶ $skip_next = 1 if $match->match eq $replace;
Branch: 1 branch
BranchTrueFalseTotal
if $match->match eq $replace 0 0 0
56 0 ▶ $skip_next = 1 if $new_match_cnt > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $new_match_cnt > 0 0 0 0
57
58 0 my $short_pre = substr($match->pre, (-25*$more), (25*$more));
59 0 my $short_post = substr($match->post, 0, 25*$more);
60 0 ▶ my $message = $short_pre; $message .= $re_do ? BOLD $preview : BOLD $match->match; $message .= $short_post;
Branch: 1 branch
BranchTrueFalseTotal
$re_do ? : 0 0 0
61 0 ▶ unless($do_file || $do_all || $skip_next)
Branch: 1 branch
BranchTrueFalseTotal
unless ($do_file or $do_all or $skip_next) 0 0 0
Condition0%0/6
$do_file or $do_all or $skip_next
l!l&&r!l&&!r
000
$do_file or $do_all
l!l&&r!l&&!r
000
MC/DC0%0/3
$do_file or $do_all or $skip_next
$do_file $do_all $skip_next
Truth table0%0/4
A or B or C
A= $do_fileB= $do_allC= $skip_next
ABCresult
1XX1
01X1
0011
0000
62 {
63 0 my $out = BOLD $file ."\n"; $out.=$message; $out.="\n--------------\n"; $out .= BOLD $replace . "\n";
64 0 print $out;
65 }
66 0 my $prompt = BOLD "[Y]"; $prompt .= "Replace "; $prompt .= BOLD "[A]"; $prompt .= "All In File "; $prompt .= BOLD "[Z]"; $prompt .= "All Files\n "; $prompt .= BOLD "[P]"; $prompt .= "Preview "; $prompt .= BOLD "[M]"; $prompt .= "More\n";
67 0 my $result = '';
68 0 ▶ $result = "Y" if $do_file || $do_all;
Branch: 1 branch
BranchTrueFalseTotal
if $do_file or $do_all 0 0 0
Condition0%0/3
$do_file or $do_all
l!l&&r!l&&!r
000
MC/DC0%0/2
$do_file or $do_all
$do_file $do_all
Truth table0%0/3
A or B
A= $do_fileB= $do_all
ABresult
1X1
011
000
69 0 ▶ $result = "Next" if $skip_next;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_next 0 0 0
70 0 ▶ $result = &Term::Prompt::prompt("x", "$prompt", "", "Next") unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
71 0 ▶ $more = 1 if $result !~ /M/i && $skip_next != 1;
Branch: 1 branch
BranchTrueFalseTotal
if not $result =~ /M/i and $skip_next != 1 0 0 0
Condition0%0/3
not $result =~ /M/i and $skip_next != 1
!ll&&!rl&&r
000
MC/DC0%0/2
not $result =~ /M/i and $skip_next != 1
not $result =~ /M/i $skip_next != 1
Truth table0%0/3
not(A) and B
A= not $result =~ /M/iB= $skip_next != 1
ABresult
0X0
100
111
72 0 ▶ $re_do = 0 unless $skip_next;
Branch: 1 branch
BranchTrueFalseTotal
unless $skip_next 0 0 0
73
74 0 ▶ if($result =~ /A/i){
Branch: 4 branches
BranchTrueFalseTotal
if ($result =~ /A/i) { } 0 0 0
elsif ($result =~ /Z/i) { } 0 0 0
elsif ($result =~ /P/i) { } 0 0 0
elsif ($result =~ /m/i) { } 0 0 0
75 0 my $v_message = "Are you sure you want to replace "; $v_message .= BOLD "ALL "; $v_message .= "occurances of $search in "; $v_message .= BOLD "THIS "; $v_message .= "file";
76 0 my $verify = &prompt("y", "$v_message", "", "n");
77 0 ▶ $do_file = 1 if $verify == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verify == 1 0 0 0
78 0 ▶ $contents = $match->pre . $match->match . $match->post if $verify != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verify != 1 0 0 0
79 }
80 elsif($result =~ /Z/i){
81 0 my $v_message = "Are you sure you want to replace "; $v_message .= BOLD "ALL "; $v_message .= "occurances of $search in "; $v_message .= BOLD "ALL "; $v_message .= "remaining files";
82 0 my $verify = &prompt("y", "$v_message", "", "No");
83 0 ▶ $do_all = 1 if $verify == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verify == 1 0 0 0
84 0 ▶ $self->expression($expression)->do_all(1) if $verify == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verify == 1 0 0 0
85 0 ▶ $contents = $match->pre . $match->match . $match->post if $verify != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verify != 1 0 0 0
86 }
87 elsif($result =~ /P/i){
88 0 $re_do = 1;
89 0 my $body = $match->match;
90 0 eval ("\$body =~ s/$search/$replace/$options_replace");
91 0 $contents = $match->pre . $match->match . $match->post;
92 0 $new_match_cnt = $match_cnt+1;
93 0 $preview = $body;
94 }
95 elsif($result =~ /m/i){
96 0 $re_do = 1;
97 0 $more = $more*2;
98 0 $contents = $match->pre . $match->match . $match->post;
99 0 $new_match_cnt = $match_cnt+1;
100 0 $preview = $match->match;
101 }
102
103 0 ▶ $match_cnt++ unless $skip_next == 1 || $re_do == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $skip_next == 1 or $re_do == 1 0 0 0
Condition0%0/3
$skip_next == 1 or $re_do == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$skip_next == 1 or $re_do == 1
$skip_next == 1 $re_do == 1
Truth table0%0/3
A or B
A= $skip_next == 1B= $re_do == 1
ABresult
1X1
011
000
104 0 ▶ if($result =~ /y/i || ($result =~ /Z/i && $do_all) || ($result =~ /A/i && $do_file))
Branch: 1 branch
BranchTrueFalseTotal
if ($result =~ /y/i or $result =~ /Z/i and $do_all or $result =~ /A/i and $do_file) 0 0 0
Condition0%0/12
$result =~ /y/i or $result =~ /Z/i and $do_all or $result =~ /A/i and $do_file
l!l&&r!l&&!r
000
$result =~ /y/i or $result =~ /Z/i and $do_all
l!l&&r!l&&!r
000
$result =~ /Z/i and $do_all
!ll&&!rl&&r
000
$result =~ /A/i and $do_file
!ll&&!rl&&r
000
MC/DC0%0/5
$result =~ /y/i or $result =~ /Z/i and $do_all or $result =~ /A/i and $do_file
$result =~ /y/i $result =~ /Z/i $do_all $result =~ /A/i $do_file
Truth table0%0/8
A or B and C or D and E
A= $result =~ /y/iB= $result =~ /Z/iC= $do_allD= $result =~ /A/iE= $do_file
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
105 {
106 0 my $body = $match->match;
107 0 eval ("\$body =~ s/$search/$replace/$options_replace");
108 0 $replace_cnt++;
109 0 $contents = $match->pre . $body . $match->post;
110 0 $new_match_cnt = $match_cnt+1;
111 }
112 0 $new_match_cnt--;
113 0 $skip_next = 0;
114 }
115 0 ▶ $self->{_search}->expression($expression)->add_files_replaced($file) if $replace_cnt > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $replace_cnt > 0 0 0 0
116 0 $self->{_search}->expression($expression)->replacements($file, $replace_cnt);
117 0 $self->{_search}->expression($expression)->matches($file, $match_cnt);
118 }
119 0 close(FILE);
120 0 ▶ if($self->{_search}->do_replace)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_search"}->do_replace) 0 0 0
121 {
122 0 ▶ open(FILE, ">$file") || die "Cannot read file $file\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">$file" or die "Cannot read file $file\n" 0 0 0
123 0 print FILE $contents;
124 0 close(FILE);
125 }
126 }
127
128
129 1 1;
130 __END__
↑ Summary