blib/lib/File/CodeSearch/Replacer.pm

filter: Statementstmt 100% 51 / 51 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 100% 67 / 67 CC 5 SCAR 7.7
CC
5
Cov
100%
highlight
4.0
make_replace_re
2.0
BEGIN
1.0
CRAP 2.2
« Build ↑ Summary
1 package File::CodeSearch::Replacer;
2  
3 # Created on: 2009-08-07 18:42:16
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Term::ANSIColor qw/:constants/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $VERSION = version->new('0.7.5');
17  
18 2 extends 'File::CodeSearch::Highlighter';
19  
20 2 has replace_re => (
21 is => 'rw',
22 );
23 2 has replace => (
24 is => 'rw',
25 );
26 2 has all => (
27 is => 'rw',
28 isa => 'Int',
29 );
30  
31 sub make_replace_re {
32 4 ▶ my ($self) = @_;
Subroutine make_replace_re: called CC=2 SCAR=6.9
Pod documented
33  
34 4 ▶ return $self->replace_re if $self->replace_re;
Branch: 1 branch
BranchTrueFalseTotal
if $self->replace_re 1 3 4
35  
36 3 my $re = $self->make_regex;
37  
38 # make sure that all brackets are for non capture groups
39 3 $re =~ s/ (?<! \\ | \[ ) [(] (?! [?] ) /(?:/gxms;
40  
41 3 return $self->replace_re($re);
42 }
43  
44 sub highlight {
45 3 ▶ my ($self, $string) = @_;
Subroutine highlight: called CC=4 SCAR=13.9
Pod documented
46 3 my $re = $self->make_highlight_re;
47 3 my $replace_re = $self->make_replace_re;
48 3 my $replace = $self->replace;
49 3 my $before = '';
50 3 my $after = '';
51 3 my $changed = '';
52  
53 3 my @parts = split /($re)/, $string;
54  
55 3 for my $i ( 0 .. @parts - 1 ) {
56 11 ▶ if ( $i % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 2) { } 4 7 11
57 4 $before .= $self->before_match . $parts[$i] . $self->after_match;
58 4 my $part = $parts[$i];
59 4 $part =~ s/$replace_re/$replace/;
60 4 $after .= $self->before_match . $part . $self->after_match;
61 4 $changed .= $part;
62 }
63 else {
64 7 $before .= $self->before_nomatch . $parts[$i] . $self->after_nomatch;
65 7 $after .= $self->before_nomatch . $parts[$i] . $self->after_nomatch;
66 7 $changed .= $parts[$i];
67 }
68 }
69  
70 3 ▶ if ($string !~ /\n/xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /\n/msx) 2 1 3
71 2 $before .= "\\N\n";
72 2 $after .= "\\N\n";
73 }
74  
75 3 return ( '', $before, $after, $changed );
76 }
77  
78 2 1;
79  
80 __END__
81  
82 =head1 NAME
83  
84 File::CodeSearch::Replacer - Sorts out file content that should be changed.
85  
86 =head1 VERSION
87  
88 This documentation refers to File::CodeSearch::Replacer version 0.7.5.
89  
90 =head1 SYNOPSIS
91  
92 use File::CodeSearch::Replacer;
93  
94 # Brief but working code example(s) here showing the most common usage(s)
95 # This section will be as far as many users bother reading, so make it as
96 # educational and exemplary as possible.
97  
98 =head1 DESCRIPTION
99  
100 =head1 ATTRIBUTES
101  
102 =over 4
103  
104 =item C<replace_re>
105  
106 The regular expression to replace text with
107  
108 =item C<replace>
109  
110 The text to be used for replacement
111  
112 =item C<all (Int)>
113  
114 Answer yes to all questions
115  
116 =back
117  
118 =head1 SUBROUTINES/METHODS
119  
120 =head3 C<highlight ( $search, )>
121  
122 Param: C<$search> - type (detail) - description
123  
124 Return: File::CodeSearch::Replacer -
125  
126 Description:
127  
128 =head3 C<make_replace_re ( )>
129  
130 Creates the regular expression for replacing the searched for text.
131  
132 =head1 DIAGNOSTICS
133  
134 =head1 CONFIGURATION AND ENVIRONMENT
135  
136 =head1 DEPENDENCIES
137  
138 =head1 INCOMPATIBILITIES
139  
140 =head1 BUGS AND LIMITATIONS
141  
142 There are no known bugs in this module.
143  
144 Please report problems to Ivan Wills (ivan.wills@gmail.com).
145  
146 Patches are welcome.
147  
148 =head1 AUTHOR
149  
150 Ivan Wills - (ivan.wills@gmail.com)
151  
152 =head1 LICENSE AND COPYRIGHT
153  
154 Copyright (c) 2009 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
155 All rights reserved.
156  
157 This module is free software; you can redistribute it and/or modify it under
158 the same terms as Perl itself. See L<perlartistic>. This program is
159 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
160 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
161 PARTICULAR PURPOSE.
162  
163 =cut
« Build ↑ Summary