blib/lib/App/Greple/update.pm

filter: Statementstmt 96.0% 120 / 125 Branchbran 63.0% 29 / 46 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 95.8% 23 / 24 Podpod 0.0% 0 / 8 totaltotal 83.1% 173 / 208 CC 25 SCAR 17.3
CC
25
Cov
86%
update_file
13.8
update_diff
7.5
update_initialize
4.0
CRAP 5.7
↑ Summary
1  
2 =encoding utf8
3  
4 =head1 NAME
5  
6 update - Greple module to update file content
7  
8 =head1 SYNOPSIS
9  
10 greple -Mupdate
11  
12 Options:
13  
14 --update replace file content
15 --with-backup make backup files
16  
17 --diff produce diff output
18 -U#, --diff-context=# unified diff context length
19 --diff-command=# diff command
20  
21 --discard simply discard the output
22  
23 =head1 VERSION
24  
25 Version 1.05
26  
27 =head1 DESCRIPTION
28  
29 This B<greple> module replaces the content of the target file with the
30 output of the command. For example, the next command changes all words
31 in the file to uppercase.
32  
33 greple -Mupdate '\w+' --cs uc --update file
34  
35 This is a very simple example, but you can implement arbitrarily
36 complex functions in conjunction with other B<greple> options.
37  
38 You can check how the file will be edited with the B<--diff> option.
39  
40 greple -Mupdate '\w+' --cs uc --diff file
41  
42 Command B<sdif> or B<cdif> would be useful to see the difference
43 visually.
44  
45 greple -Mupdate '\w+' --cs uc --diff file | cdif
46  
47 To pass only the matched parts through an external command and write
48 the result back, use it with L<App::Greple::tee>. Give B<-Mupdate>
49 before B<-Mtee>, because the arguments of B<-Mtee> up to C<--> are the
50 command, and B<--update> after the C<-->:
51  
52 greple -Mupdate -Mtee tr a-z A-Z -- --update '\w+' file
53  
54 Calling a function, as in C<-Mtee '&ansicolumn'>, requires
55 L<Command::Run> 1.05 or later.
56  
57 This module has been spun off from the L<App::Greple::subst> module.
58 Consult it for more practical use cases.
59  
60 =head1 OPTIONS
61  
62 There are two kinds of options for this module, such as C<--diff> and
63 C<--update::diff>. This is to avoid option name conflicts when used
64 in combination with other modules. If you are using this module from
65 another module and want to use the C<--diff> option in it, call as
66 C<--update::diff>.
67  
68 =over 7
69  
70 =item B<--update>
71  
72 =item B<--update::update>
73  
74 Update the target file with the command output. The entire file
75 content is produced and any color effects are canceled. Without this
76 option, B<greple> behaves as usual, that is, only matched lines are
77 printed.
78  
79 The file is not touched as long as its content does not change.
80  
81 The file is also not updated if the output is empty. This is to
82 prevent the contents of the file from being erased if none of the
83 match strings are included. If you want to intentionally empty a
84 file, you need to think of another way.
85  
86 =item B<--with-backup>[=I<suffix>]
87  
88 Back up the original file with the C<.bak> suffix. If the optional
89 parameter is given, it is used as the suffix. If the backup file
90 exists, C<.bak_1>, C<.bak_2> ... are used.
91  
92 Give the suffix with C<=>. An argument that follows the option is
93 taken as the suffix, so in C<--with-backup file> the name of the file
94 is lost. Put another option after it or use
95 C<-Mupdate::config(with_backup)> in that case.
96  
97 =item B<--discard>
98  
99 =item B<--update::discard>
100  
101 Simply discard the command output without updating the file. This option
102 can be used when the output of the command is not needed and only side
103 effects are expected.
104  
105 =begin comment
106  
107 =item B<--create>
108  
109 =item B<--update::create>
110  
111 Create new file and write the result. Suffix ".new" is appended to
112 the original filename.
113  
114 =end comment
115  
116 =item B<--diff>
117  
118 =item B<--update::diff>
119  
120 Produce a diff of the original and the converted text.
121  
122 =item B<-U>I<n>, B<--diff-context>=I<n>
123  
124 Context length of the unified diff produced by B<--diff>.
125  
126 =item B<--diff-command>=I<command>
127  
128 Diff command used by B<--diff>. Default is C<diff -u>. If the context
129 length is given, C<diff -U>I<n> is used instead.
130  
131 =back
132  
133 =head1 CONFIGURATION
134  
135 Module parameters can be set with L<Getopt::EX::Config> using the
136 following syntax:
137  
138 greple -Mupdate::config(with_backup) ...
139 greple -Mupdate::config(with_backup=.orig) ...
140 greple -Mupdate::config(diff_context=3) ...
141  
142 Options given on the command line override them. Available
143 parameters are:
144  
145 with_backup back up the file, as --with-backup
146 diff_context context length of diff, as --diff-context
147 diff_command diff command, as --diff-command (default: diff -u)
148 update_suffix backup suffix (default: none)
149 remember compare with the content that was read, not with
150 the file, in --diff (default: 1)
151  
152 =head1 LIMITATIONS
153  
154 Files are always written in UTF-8. A file read with B<--icode> in
155 another encoding, such as Shift-JIS, is converted to UTF-8, and
156 B<--ocode> has no effect on the file.
157  
158 Binary data read with C<--icode=binary> is not preserved: bytes
159 outside of ASCII are changed when the file is written. A file that is
160 not valid UTF-8 is skipped unless B<--icode> is given.
161  
162 Greple's B<--of> option does not take effect with B<--update>: the
163 file is written without the filter.
164  
165 Standard input has no file to go back to: with B<--update>, the result
166 is written to a file named C<->.
167  
168 =head1 INSTALL
169  
170 =head2 CPANMINUS
171  
172 $ cpanm App::Greple::update
173  
174 =head2 GITHUB
175  
176 $ cpanm https://github.com/kaz-utashiro/greple-update.git
177  
178 =head1 SEE ALSO
179  
180 L<App::Greple>, L<https://github.com/kaz-utashiro/greple>
181  
182 L<App::Greple::update>, L<https://github.com/kaz-utashiro/greple-update>
183  
184 L<App::Greple::subst>, L<https://github.com/kaz-utashiro/greple-subst>
185  
186 L<App::sdif>, L<App::cdif>
187  
188 =head1 AUTHOR
189  
190 Kazumasa Utashiro
191  
192 =head1 LICENSE
193  
194 Copyright 2022-2026 Kazumasa Utashiro.
195  
196 This library is free software; you can redistribute it and/or modify
197 it under the same terms as Perl itself.
198  
199 =cut
200  
201 package App::Greple::update;
202 21 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
203 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
204 21 ▶use experimental 'refaliasing';
Subroutine BEGIN: called CC=1 SCAR=0.0
205  
206 21 our $VERSION = '1.05';
207  
208 21 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
209 21 ▶use open IO => ':utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
210  
211 21 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
212 21 our @EXPORT = qw(
213 &update_initialize
214 &update_begin
215 &update_diff
216 &update_divert
217 &update_file
218 );
219 21 our %EXPORT_TAGS = ( );
220 21 our @EXPORT_OK = qw();
221  
222 21 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
223 21 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
224 21 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
225 21 ▶use App::Greple::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
226 21 ▶use Text::ParseWords qw(shellwords);
Subroutine BEGIN: called CC=1 SCAR=0.0
227 21 ▶use Getopt::EX::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
228  
229 21 our $debug = 0;
230  
231 21 my $config = Getopt::EX::Config->new(
232 remember => 1,
233 diff_command => "diff -u",
234 update_suffix => '',
235 with_backup => undef,
236 diff_context => '',
237 );
238  
239 # The builtin options in __DATA__ and the config set the same values.
240 21 \our $remember_data = \$config->{remember};
241 21 \our $opt_update_diffcmd = \$config->{diff_command};
242 21 \our $opt_suffix = \$config->{update_suffix};
243 21 \our $opt_backup = \$config->{with_backup};
244 21 \our $opt_U = \$config->{diff_context};
245  
246 21 my $current_file;
247 my $contents;
248 21 my @update_diffcmd;
249  
250 0 ▶sub debug {
Subroutine debug: not called CC=1 SCAR=6.9
Pod undocumented
251 0 $debug = 1;
252 }
253  
254 19 ▶sub update_initialize {
Subroutine update_initialize: called CC=4 SCAR=13.9
Pod undocumented
255 19 @update_diffcmd = shellwords $opt_update_diffcmd;
256 19 ▶ if ($opt_U ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::update::opt_U ne "") 8 11 19
257 8 @update_diffcmd = ('diff', "-U$opt_U");
258 }
259 19 ▶ if (defined $opt_backup) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $App::Greple::update::opt_backup) 3 16 19
260 # no value: "" from --with-backup, 1 from config(with_backup)
261 3 ▶ $opt_suffix = $opt_backup =~ /^1?$/ ? '.bak' : $opt_backup;
Branch: 1 branch
BranchTrueFalseTotal
$App::Greple::update::opt_backup =~ /^1?$/u ? : 1 2 3
262 }
263 }
264  
265 19 ▶sub update_begin {
Subroutine update_begin: called CC=3 SCAR=11.7
Pod undocumented
266 19 my %arg = @_;
267 19 ▶ $current_file = delete $arg{&FILELABEL} or die;
Branch: 1 branch
BranchTrueFalseTotal
$current_file = delete $arg{&FILELABEL} or die 19 0 19
268 19 ▶ $contents = $_ if $remember_data;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Greple::update::remember_data 19 0 19
269 }
270  
271 #
272 # define &divert_stdout and &recover_stdout
273 #
274 {
275 21 my $divert;
276 21 my $bufref;
277  
278 6 ▶ sub divert_stdout {
Subroutine divert_stdout: called CC=2 SCAR=7.0
Pod undocumented
279 6 $bufref = shift;
280 6 ▶ return if $divert;
Branch: 1 branch
BranchTrueFalseTotal
if $divert 0 6 6
281 6 require IO::Divert;
282 6 $divert = IO::Divert->new(autoprint => 0);
283 }
284  
285 6 ▶ sub recover_stdout {
Subroutine recover_stdout: called CC=2 SCAR=7.0
Pod undocumented
286 6 ▶ $divert or return;
Branch: 1 branch
BranchTrueFalseTotal
$divert or return 6 0 6
287 6 $$bufref = $divert->cancel->content;
288 6 undef $divert;
289 6 1;
290 }
291 }
292  
293 21 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
294  
295 7 ▶sub update_diff {
Subroutine update_diff: called CC=7 SCAR=20.2
Pod undocumented
296 7 my $orig = $current_file;
297 7 my $fh;
298 7 state $fdpath = do {
299 7 my $fd = DATA->fileno;
300 7 ▶ first { -r "$_/$fd" } qw( /dev/fd /proc/self/fd );
Subroutine __ANON__: called CC=1 SCAR=0.0
301 };
302  
303 7 ▶ if ($fdpath and $remember_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fdpath and $App::Greple::update::remember_data) 7 0 7
Condition33%1/3
$fdpath and $App::Greple::update::remember_data
!ll&&!rl&&r
007
MC/DC0%0/2
$fdpath and $App::Greple::update::remember_data
$fdpath $App::Greple::update::remember_data
Truth table33%1/3
A and B
A= $fdpathB= $App::Greple::update::remember_data
ABresult
0X0
100
111
304 21 ▶ use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
305 21 ▶ use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
306 7 ▶ $fh = IO::File->new_tmpfile or die "new_tmpfile: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new_tmpfile or die "new_tmpfile: $!\n" 7 0 7
307 7 $fh->binmode(':encoding(utf8)');
308 7 ▶ my $fd = $fh->fcntl(F_GETFD, 0) or die "fcntl F_GETFD: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $fd = $fh->fcntl(1, 0) or die "fcntl F_GETFD: $!\n" 7 0 7
309 7 ▶ $fh->fcntl(F_SETFD, $fd & ~FD_CLOEXEC) or die "fcntl F_SETFD: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->fcntl(2, $fd & '18446744073709551614') or die "fcntl F_SETFD: $!\n" 7 0 7
310 7 $fh->printflush($contents);
311 7 $fh->seek(0, 0);
312 7 $orig = sprintf "%s/%d", $fdpath, $fh->fileno;
313 }
314  
315 7 ▶ @update_diffcmd or confess "Empty diff command";
Branch: 1 branch
BranchTrueFalseTotal
@update_diffcmd or confess("Empty diff command") 7 0 7
316 7 exec @update_diffcmd, $orig, "-";
317 0 die "exec: $!\n";
318 }
319  
320 21 my $divert_buffer;
321  
322 6 ▶sub update_divert {
Subroutine update_divert: called CC=1 SCAR=0.0
Pod undocumented
323 6 my %arg = @_;
324 6 my $filename = delete $arg{&FILELABEL};
325  
326 6 $divert_buffer = '';
327 6 divert_stdout(\$divert_buffer);
328 }
329  
330 5 ▶sub update_file {
Subroutine update_file: called CC=12 SCAR=26.3
Pod undocumented
331 5 my %arg = @_;
332 5 my $filename = delete $arg{&FILELABEL};
333 5 my $newname = '';
334  
335 5 ▶ recover_stdout() or die;
Branch: 1 branch
BranchTrueFalseTotal
recover_stdout() or die 5 0 5
336 5 ▶ return if $arg{discard};
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"discard"} 1 4 5
337 4 $divert_buffer = decode 'utf8', $divert_buffer;
338  
339 4 ▶ return if $divert_buffer eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $divert_buffer eq $_ 0 4 4
340 4 ▶ return if $divert_buffer eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $divert_buffer eq "" 0 4 4
341  
342 4 ▶ if (my $suffix = $opt_suffix) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $suffix = $App::Greple::update::opt_suffix) 3 1 4
343 3 $newname = $filename . $suffix;
344 3 ▶ for (my $i = 1; -f $newname; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $newname) 0 3 3
345 0 $newname = $filename . $suffix . "_$i";
346 }
347 }
348  
349 4 my $create = do {
350 4 ▶ if ($arg{replace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{'replace'}) { } 4 0 4
351 4 ▶ if ($newname ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($newname ne '') { } 3 1 4
352 3 warn "rename $filename -> $newname\n";
353 3 ▶ rename $filename, $newname or die "rename: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
rename $filename, $newname or die "rename: $!\n" 3 0 3
354 3 ▶ die if -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $filename 0 3 3
355 } else {
356 1 warn "overwrite $filename\n";
357 }
358 4 $filename;
359 } else {
360 0 warn "create $newname\n";
361 0 $newname;
362 }
363 };
364  
365 4 ▶ open my $fh, ">", $create or die "open: $create $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $create or die "open: $create $!\n" 4 0 4
366 4 $fh->print($divert_buffer);
367 4 $fh->close;
368 }
369  
370 21 1;
371  
372 __DATA__
↑ Summary