App-Greple-update
view release on metacpan or search on metacpan
update - Greple module to update file content
# SYNOPSIS
greple -Mupdate
Options:
--update replace file content
--with-backup make backup files
--diff produce diff output
-U#, --diff-context=# unified diff context length
--diff-command=# diff command
--discard simply discard the output
# VERSION
Version 1.05
option, **greple** behaves as usual, that is, only matched lines are
printed.
The file is not touched as long as its content does not change.
The file is also not updated if the output is empty. This is to
prevent the contents of the file from being erased if none of the
match strings are included. If you want to intentionally empty a
file, you need to think of another way.
- **--with-backup**\[=_suffix_\]
Back up the original file with the `.bak` suffix. If the optional
parameter is given, it is used as the suffix. If the backup file
exists, `.bak_1`, `.bak_2` ... are used.
Give the suffix with `=`. An argument that follows the option is
taken as the suffix, so in `--with-backup file` the name of the file
is lost. Put another option after it or use
`-Mupdate::config(with_backup)` in that case.
- **--discard**
- **--update::discard**
Simply discard the command output without updating the file. This option
can be used when the output of the command is not needed and only side
effects are expected.
- **--diff**
- **--update::diff**
- **--diff-command**=_command_
Diff command used by **--diff**. Default is `diff -u`. If the context
length is given, `diff -U`_n_ is used instead.
# CONFIGURATION
Module parameters can be set with [Getopt::EX::Config](https://metacpan.org/pod/Getopt%3A%3AEX%3A%3AConfig) using the
following syntax:
greple -Mupdate::config(with_backup) ...
greple -Mupdate::config(with_backup=.orig) ...
greple -Mupdate::config(diff_context=3) ...
Options given on the command line override them. Available
parameters are:
with_backup back up the file, as --with-backup
diff_context context length of diff, as --diff-context
diff_command diff command, as --diff-command (default: diff -u)
update_suffix backup suffix (default: none)
remember compare with the content that was read, not with
the file, in --diff (default: 1)
# LIMITATIONS
Files are always written in UTF-8. A file read with **--icode** in
another encoding, such as Shift-JIS, is converted to UTF-8, and
**--ocode** has no effect on the file.
Binary data read with `--icode=binary` is not preserved: bytes
lib/App/Greple/update.pm view on Meta::CPAN
update - Greple module to update file content
=head1 SYNOPSIS
greple -Mupdate
Options:
--update replace file content
--with-backup make backup files
--diff produce diff output
-U#, --diff-context=# unified diff context length
--diff-command=# diff command
--discard simply discard the output
=head1 VERSION
Version 1.05
lib/App/Greple/update.pm view on Meta::CPAN
option, B<greple> behaves as usual, that is, only matched lines are
printed.
The file is not touched as long as its content does not change.
The file is also not updated if the output is empty. This is to
prevent the contents of the file from being erased if none of the
match strings are included. If you want to intentionally empty a
file, you need to think of another way.
=item B<--with-backup>[=I<suffix>]
Back up the original file with the C<.bak> suffix. If the optional
parameter is given, it is used as the suffix. If the backup file
exists, C<.bak_1>, C<.bak_2> ... are used.
Give the suffix with C<=>. An argument that follows the option is
taken as the suffix, so in C<--with-backup file> the name of the file
is lost. Put another option after it or use
C<-Mupdate::config(with_backup)> in that case.
=item B<--discard>
=item B<--update::discard>
Simply discard the command output without updating the file. This option
can be used when the output of the command is not needed and only side
effects are expected.
=begin comment
lib/App/Greple/update.pm view on Meta::CPAN
Diff command used by B<--diff>. Default is C<diff -u>. If the context
length is given, C<diff -U>I<n> is used instead.
=back
=head1 CONFIGURATION
Module parameters can be set with L<Getopt::EX::Config> using the
following syntax:
greple -Mupdate::config(with_backup) ...
greple -Mupdate::config(with_backup=.orig) ...
greple -Mupdate::config(diff_context=3) ...
Options given on the command line override them. Available
parameters are:
with_backup back up the file, as --with-backup
diff_context context length of diff, as --diff-context
diff_command diff command, as --diff-command (default: diff -u)
update_suffix backup suffix (default: none)
remember compare with the content that was read, not with
the file, in --diff (default: 1)
=head1 LIMITATIONS
Files are always written in UTF-8. A file read with B<--icode> in
another encoding, such as Shift-JIS, is converted to UTF-8, and
B<--ocode> has no effect on the file.
Binary data read with C<--icode=binary> is not preserved: bytes
lib/App/Greple/update.pm view on Meta::CPAN
use App::Greple::Common;
use Text::ParseWords qw(shellwords);
use Getopt::EX::Config;
our $debug = 0;
my $config = Getopt::EX::Config->new(
remember => 1,
diff_command => "diff -u",
update_suffix => '',
with_backup => undef,
diff_context => '',
);
# The builtin options in __DATA__ and the config set the same values.
\our $remember_data = \$config->{remember};
\our $opt_update_diffcmd = \$config->{diff_command};
\our $opt_suffix = \$config->{update_suffix};
\our $opt_backup = \$config->{with_backup};
\our $opt_U = \$config->{diff_context};
my $current_file;
my $contents;
my @update_diffcmd;
sub debug {
$debug = 1;
}
sub update_initialize {
@update_diffcmd = shellwords $opt_update_diffcmd;
if ($opt_U ne '') {
@update_diffcmd = ('diff', "-U$opt_U");
}
if (defined $opt_backup) {
# no value: "" from --with-backup, 1 from config(with_backup)
$opt_suffix = $opt_backup =~ /^1?$/ ? '.bak' : $opt_backup;
}
}
sub update_begin {
my %arg = @_;
$current_file = delete $arg{&FILELABEL} or die;
$contents = $_ if $remember_data;
}
#
lib/App/Greple/update.pm view on Meta::CPAN
}
1;
__DATA__
builtin diff-command=s $opt_update_diffcmd
builtin update-suffix=s $opt_suffix
builtin diff-context|U=i $opt_U
builtin remember! $remember_data
builtin with-backup:s $opt_backup
option default \
--prologue update_initialize \
--begin update_begin
expand ++dump --all -h --color=never --no-newline --no-line-number
option --update::diff ++dump --of &update_diff
option --update::create ++dump --begin update_divert --end update_file() --update-suffix=.new
option --update::update ++dump --begin update_divert --end update_file(replace)
option --update::discard --begin update_divert --end update_file(discard)
t/04_config.t view on Meta::CPAN
unlink glob "$file*";
open my $fh, '>', $file or die;
print $fh "the fox\nsecond line\n";
close $fh;
$file;
}
sub config { greple("-Mupdate::config($_[0])", @_[1..$#_]) }
my $file = target();
config('with_backup', qw(fox --cm sub{uc} --update), $file)->run;
ok(-f "$file.bak", 'config(with_backup): backup with the default suffix');
like(slurp($file), qr/FOX/, 'config(with_backup): the file is updated');
like(slurp("$file.bak"), qr/fox/, 'config(with_backup): the backup has the original');
$file = target();
config('with_backup=.orig', qw(fox --cm sub{uc} --update), $file)->run;
ok(-f "$file.orig", 'config(with_backup=.orig): given suffix');
ok(!-f "$file.bak", 'config(with_backup=.orig): no default suffix');
$file = target();
my $out = config('diff_context=0', qw(fox --cm sub{uc} --diff), $file)->run->{stdout};
like($out, qr/^\@\@ -1 \+1 \@\@/m, 'config(diff_context=0): context length of diff');
for my $opts ([qw(--diff-context=0)], [qw(-U0)], [qw(-U 0)]) {
$file = target();
my $out = update(qw(fox --cm sub{uc} --diff), @$opts, $file)->run->{stdout};
like($out, qr/^\@\@ -1 \+1 \@\@/m, "@$opts: context length of diff");
}
for my $opts ([q(--diff-command=diff -c)]) {
$file = target();
my $out = update(qw(fox --cm sub{uc} --diff), @$opts, $file)->run->{stdout};
like($out, qr/^\*{15}$/m, "@$opts: diff command");
}
$file = target();
update(qw(fox --cm sub{uc} --update --with-backup=.opt), $file)->run;
ok(-f "$file.opt", '--with-backup=.opt still works');
$file = target();
update('fox', qw(--cm sub{uc} --update --), $file)->run;
like(slurp($file), qr/FOX/, 'a -- before the file is left to greple');
done_testing;
( run in 4.125 seconds using v1.01-cache-2.11-cpan-99fa8243a42 )