App-inplace
view release on metacpan or search on metacpan
script/inplace view on Meta::CPAN
#!perl
use strict;
use warnings;
use Fcntl;
use Getopt::Long qw(:config gnu_compat bundling no_ignore_case no_permute);
use IPC::Run;
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2023-02-15'; # DATE
our $DIST = 'App-inplace'; # DIST
our $VERSION = '0.002'; # VERSION
my %Opts = (
backup => "~",
);
my $Command;
my $File;
sub parse_cmdline {
my $res = GetOptions(
'backup|b:s' => \$Opts{backup},
'help|h' => sub {
print <<USAGE;
Usage:
inplace [INPLACE_OPTS] <COMMAND> <FILE> [CMD_OPTS]...
inplace --help
inplace options:
--backup[=.ext], -b
For more details, see the manpage/documentation.
USAGE
exit 0;
},
);
@ARGV >= 2 or die "inplace: Usage: inplace [INPLACE_OPTS] <COMMAND> <FILE> [CMD_OPTS]...\n";
$Command = shift @ARGV;
if (grep {$_ eq "--"} @ARGV) {
for (0..$#ARGV) {
if ($ARGV[$_] eq '--') {
if ($_ == $#ARGV) {
die "inplace: Must specify file after '--'\n";
}
$File = $ARGV[0];
splice @ARGV, $_, 1;
last;
}
}
} else {
$File = $ARGV[0];
}
exit 99 if !$res;
}
sub run {
# check that file exists
(-f $File) or die "inplace: File '$File' does not exist or not a file\n";
# create a temporary file
my ($tempfile, $tempfh);
my $i = 0;
while (1) {
$i++;
my $rand = sprintf("%06d", 10_000*rand());
$tempfile = "$File.$rand";
last if sysopen $tempfh, $tempfile, O_WRONLY | O_CREAT | O_EXCL;
if ($i > 100) {
die "inplace: Can't create tempfile '$tempfile' after 100 retries: $!\n";
}
}
# run command
IPC::Run::run([$Command, @ARGV], \*STDIN, $tempfh, \*STDERR)
or die "inplace: Command '$Command' failed ($?), not replacing file '$File'\n";
close $tempfh
or die "inplace: Failed writing to tempfile '$tempfile': $!\n";
# if there is a backup extension, move the original file to backup
if (defined $Opts{backup} && $Opts{backup} ne '') {
my $bakfile = "$File$Opts{backup}";
rename $File, $bakfile
or die "inplace: Failed moving '$File' to '$bakfile': $!\n";
}
# replace original file with temporary file
rename $tempfile, $File
or die "inplace: Failed replacing '$File' with '$tempfile': $!\n";
}
# MAIN
parse_cmdline();
run();
1;
# ABSTRACT: Replace file with output of command
# PODNAME: inplace
__END__
=pod
=encoding UTF-8
=head1 NAME
inplace - Replace file with output of command
=head1 VERSION
This document describes version 0.002 of inplace (from Perl distribution App-inplace), released on 2023-02-15.
=head1 SYNOPSIS
% inplace [INPLACE_OPTS] <COMMAND> <FILE> [CMD_OPTS]...
Example:
% inplace csv2ansitable myfile.txt
If command is successful, then F<myfile.txt> will contain the output of the
command. F<myfile.txt~> will contain the original content. The file to be
replaced must be specified as the first argument to the command.
If you do not want any backup:
% inplace -b csv2ansitable myfile.txt
If you want another backup extension other than the default C<~>:
% inplace -b.bak csv2ansitable myfile.txt
If the file is not the first argument of the command, you can use C<-->:
% inplace csv2ansitable -t -- myfile.txt
=head1 DESCRIPTION
B<inplace> is a command wrapper to give "in-place editing" capability to another
command. Sometimes a command that manipulates file does not allow you to
overwrite an input file or even specify an output path; they output to stdout.
With B<inplace>, you can replace the content of the input file with the output
of the command in one go.
=head1 HOMEPAGE
Please visit the project's homepage at L<https://metacpan.org/release/App-inplace>.
=head1 SOURCE
Source repository is at L<https://github.com/perlancar/perl-App-inplace>.
=head1 SEE ALSO
L<sponge> (a Unix utility, also a Perl implementation in L<App::sponge>), but
with this command you need to specify the filename twice (but useful when your
command is a pipeline), e.g.:
% sed '...' FILE | grep '...' | sponge FILE
=head1 AUTHOR
perlancar <perlancar@cpan.org>
=head1 CONTRIBUTING
To contribute, you can send patches by email/via RT, or send pull requests on
GitHub.
Most of the time, you don't need to build the distribution yourself. You can
simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your
system), you can install L<Dist::Zilla>,
L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
that are considered a bug and can be reported to me.
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2023, 2019 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
( run in 1.215 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )