blib/lib/File/Edit.pm

filter: Statementstmt 63.8% 53 / 83 Branchbran 35.0% 7 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 71.4% 10 / 14 Podpod 16.6% 1 / 6 totaltotal 55.4% 71 / 128 SCAR 35.1
CC
15
Cov
57%
_find_block
56.0
_find_one
6.1
save
6.0
CRAP 33.4
↑ Summary
1 package File::Edit;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Path::Tiny qw/path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = '0.0.5';
6  
7 2 has 'file';
8 2 has 'found'; # Line numbers of found lines. ArrayRef.
9 2 has '_lines'; # Text in the form of lines. ArrayRef.
10 2 has '_line_re'; # Regex for _find_one
11  
12 sub new {
13 10 ▶ @_ > 1
Subroutine new: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 10 10
14 ? $_[0]->SUPER::new({ file => path($_[1]),
15 _lines => [path($_[1])->lines],
16 found => [] })
17 : $_[0]->SUPER::new
18 }
19 sub text {
20 3 ▶ my ($self, $text) = @_;
Subroutine text: called CC=1 SCAR=0.0
Pod undocumented
21  
22 3 $text =~ s/\n/\nx-x/g;
23 3 $self->{_lines} = [split('x-x',$text)];
24  
25 3 return $self;
26 }
27 sub replace {
28 1 ▶ my ($o, $orig, $repstr) = @_;
Subroutine replace: called CC=1 SCAR=0.0
Pod undocumented
29  
30 # Replaces one line
31 1 $o->_find_one($orig)
32 ->_replace_found($repstr);
33  
34 1 return $o;
35 }
36 sub get_block {
37 0 ▶ my ($o, %opt) = @_;
Subroutine get_block: not called CC=1 SCAR=6.9
Pod undocumented
38  
39 return $o->_find_block($opt{from},$opt{to})
40 0 ->_found_lines;
41 }
42 sub save {
43 0 ▶ my ($o, $file) = @_;
Subroutine save: not called CC=2 SCAR=17.9
Pod undocumented
44  
45 0 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 0 0
46 0 path($file)->spew(join('',@{$o->_lines}));
47 } else {
48 0 $o->file->spew(join('',@{$o->_lines}));
49 }
50  
51 0 return $o;
52 }
53  
54 sub _find_block {
55 0 ▶ my ($o, $begin_re, $end_re) = @_;
Subroutine _find_block: not called CC=7 SCAR=40.3
56 0 my $in_block = 0; # True if line is in block
57 0 my $line_begin = -1; # First line num of found block. -1 if not found
58 0 my $line_end = -1; # Last line num of found block. -1 if not found
59  
60 0 foreach my $n (0 .. $#{$o->_lines}) {
61 0 ▶ if (!$in_block) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_block) { } 0 0 0
62 0 ▶ if ($o->_lines->[$n] =~ $begin_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->_lines->[$n] =~ /$begin_re/u) 0 0 0
63 0 $line_begin = $n;
64 0 $in_block = 1;
65 }
66 } else {
67 0 ▶ if ($o->_lines->[$n] =~ $end_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->_lines->[$n] =~ /$end_re/u) 0 0 0
68 0 $line_end = $n;
69 0 $in_block = 0;
70 0 last;
71 }
72 }
73 }
74  
75 # Error if block not found
76 0 ▶ croak "Block not found." if $line_begin == -1 or $line_end == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $line_begin == -1 or $line_end == -1 0 0 0
Condition0%0/3
$line_begin == -1 or $line_end == -1
l!l&&r!l&&!r
000
MC/DC0%0/2
$line_begin == -1 or $line_end == -1
$line_begin == -1 $line_end == -1
Truth table0%0/3
A or B
A= $line_begin == -1B= $line_end == -1
ABresult
1X1
011
000
77  
78 0 $o->found([$line_begin, $line_end]);
79  
80 0 return $o;
81 }
82 sub _found_lines {
83 0 ▶ my ($o) = @_;
Subroutine _found_lines: not called CC=1 SCAR=6.9
84  
85 0 return [@{$o->_lines}[$o->found->[0] .. $o->found->[1]]];
86 }
87  
88 sub _find_one {
89 10 ▶ my ($o, $line_re) = @_;
Subroutine _find_one: called CC=6 SCAR=18.0
90 10 my $n = 0;
91  
92 # Init search result
93 10 $o->found([]);
94 10 ▶ $line_re = ref $line_re eq 'Regexp' ? $line_re : _qre($line_re);
Branch: 1 branch
BranchTrueFalseTotal
ref $line_re eq 'Regexp' ? : 0 10 10
95 10 $o->_line_re($line_re);
96  
97 10 foreach my $l (@{$o->_lines}) {
98 24 ▶ push @{$o->found}, $n if $l =~ $line_re;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /$line_re/u 9 15 24
99 24 $n++;
100 }
101  
102 # Error if more than one line found
103 0 croak "Multiple lines found: ".join(', ',@{$o->found})
104 10 ▶ if scalar(@{$o->found}) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$o->found;} > 1 0 10 10
105  
106 # Error if more than one line found
107 croak "Line not found."
108 10 ▶ if scalar(@{$o->found}) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$o->found;} == 0 1 9 10
109  
110 9 return $o;
111 }
112 sub _replace_found {
113 # Replaces all lines found (line numbers in $o->found)
114 2 ▶ my ($o, $repstr) = @_;
Subroutine _replace_found: called CC=2 SCAR=6.9
115  
116 2 my $line_re = $o->_line_re; # s// does not work with $o-> notation
117  
118 2 foreach my $n (@{$o->found}) {
119 2 $o->_lines->[$n] =~ s/$line_re/$repstr/;
120 }
121  
122 2 return $o;
123 }
124 sub _qre { ## ($string) :> regex
125 10 ▶ my $quoted = quotemeta(shift);
Subroutine _qre: called CC=1 SCAR=0.0
126 10 return qr/$quoted/;
127 }
128  
129 sub swap { ## ($s1 :>STRING, $s2 :>STRING) :> SELF
130 2 ▶ my ($self,$s1,$s2) = @_;
Subroutine swap: called CC=1 SCAR=0.0
Pod undocumented
131  
132 # Find the line indexes
133 2 my $idx_1 = $self->_find_one($s1)->found->[0];
134 2 my $idx_2 = $self->_find_one($s2)->found->[0];
135  
136 # Swap the lines
137 2 my $tmp = $self->_lines->[$idx_1];
138 2 $self->_lines->[$idx_1] = $self->_lines->[$idx_2];
139 2 $self->_lines->[$idx_2] = $tmp;
140  
141 2 return $self;
142 }
143  
144  
145 =head1 NAME
146  
147 File::Edit - A naive, probably buggy, file editor.
148  
149 =cut
150 =head1 SYNOPSIS
151  
152 use File::Edit;
153  
154 # Replace string in file
155 File::Edit->new('build.gradle')
156 ->replace('minSdkVersion 16', 'minSdkVersion 21')
157 ->save()
158 ;
159  
160 # Edit text, save to file
161 File::Edit->new()
162 ->text(" minSdkVersion 16\n targetSdkVersion 29")
163 ->replace('minSdkVersion 16', 'minSdkVersion 21')
164 ->save('build.gradle')
165 ;
166  
167 # Swap lines, save to file
168 File::Edit->new()
169 ->text(" Do this first\n Now do that\n Don't do this")
170 ->swap('Do this', 'do that')
171 ->save('todo.txt')
172 ;
173  
174 =cut
175 =head1 METHODS
176  
177 =head2 new
178  
179 my $fe = File::Edit->new("some_file.txt");
180  
181 Reads in a file for editing.
182  
183 =cut
184 =head2 text
185  
186 my $fe = File::Edit->new()->text(some_text);
187  
188 Reads in some text for editing.
189  
190 =cut
191 =head2 replace
192  
193 $fe->replace($old, $new);
194  
195 Replace the $old portion of a single line with $new.
196  
197 =cut
198 =head2 save
199  
200 my $fe = File::Edit->new("some_file.txt");
201 $fe->save(); # Saves to "some_file.txt"
202 $fe->save("other.txt") # Saves to "other.txt"
203  
204 =cut
205 =head2 swap( $text_1, $text_2 )
206  
207 The swap($s1, $s2) method finds the line containing string $s1 and finds
208 the line containg string $s2 and swaps both lines.
209  
210 =cut
211 =head1 AUTHOR
212  
213 Hoe Kit CHEW, C<< <hoekit at gmail.com> >>
214  
215 =head1 BUGS
216  
217 Please report any bugs or feature requests to C<bug-file-edit at rt.cpan.org>, or through
218 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Edit>. I will be notified, and then you'll
219 automatically be notified of progress on your bug as I make changes.
220  
221  
222  
223  
224 =head1 SUPPORT
225  
226 You can find documentation for this module with the perldoc command.
227  
228 perldoc File::Edit
229  
230  
231 You can also look for information at:
232  
233 =over 4
234  
235 =item * RT: CPAN's request tracker (report bugs here)
236  
237 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Edit>
238  
239 =item * CPAN Ratings
240  
241 L<https://cpanratings.perl.org/d/File-Edit>
242  
243 =item * Search CPAN
244  
245 L<https://metacpan.org/release/File-Edit>
246  
247 =back
248  
249  
250 =head1 LICENSE AND COPYRIGHT
251  
252 This software is Copyright (c) 2021 by Hoe Kit CHEW.
253  
254 This is free software, licensed under:
255  
256 The Artistic License 2.0 (GPL Compatible)
257  
258  
259 =cut
260  
261 2 1; # End of File::Edit
↑ Summary