App-inplace

 view release on metacpan or  search on metacpan

script/inplace  view on Meta::CPAN

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 '--') {

script/inplace  view on Meta::CPAN

        }
    }

    # 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

script/inplace  view on Meta::CPAN

 % 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



( run in 0.538 second using v1.01-cache-2.11-cpan-49f99fa48dc )