in reply to help declaring variables within perl one-liner

`...` is equilvalent to readpipe(qq`...`) (basically readpipe("...")), which is to say it interpolates. As such, you wanted the following:

my $num_subs = `perl -w -0777 -pi -e 'my \$c=s/foo/bar/g;print STDOUT +"\$c\\n"' ./foo_test`; die("Can't execute child: $!\n" if $? == -1' die("Child killed by signal ".( $? & 0x7F )."\n") if $? & 0x7F; die("Child exited with error ".( $? >> 8 )."\n") if $? >> 8; chomp($num_subs);
The program can be simplified.
my $num_subs = `perl -i -0777wpe'print STDOUT s/foo/bar/g' foo_test`; die("Can't execute child: $!\n" if $? == -1' die("Child killed by signal ".( $? & 0x7F )."\n") if $? & 0x7F; die("Child exited with error ".( $? >> 8 )."\n") if $? >> 8;

An approach that would make it easier to escape (if we still needed to do so):

use String::ShellQuote qw( shell_quote ); my $cmd = shell_quote("perl", "-i", "-0777wpe", 'print STDOUT s/foo/ba +r/g', 'foo_test'); my $num_subs = `$cmd`; die("Can't execute child: $!\n" if $? == -1' die("Child killed by signal ".( $? & 0x7F )."\n") if $? & 0x7F; die("Child exited with error ".( $? >> 8 )."\n") if $? >> 8;

Why are we even invoking a shell?

use IPC::System::Simple qw( capturex ); # Core module my @cmd = ( 'perl', '-i', '-0777wpe', 'print STDOUT s/foo/bar/g', 'foo +_test' ); my $num_subs = capturex(@cmd); die("Child exited with error ".( $? >> 8 )."\n") if $? >> 8;

Now, one would also quite reasonably argue that one shouldn't execute perl either. However, -i is quite good at avoiding data loss in the event of an error[1], and recreating this wouldn't be trivial.

Finally, why execute a new perl at all?

my $num_subs = 0; { local @ARGV = "foo_test"; local $^I = ""; local $/; $num_subs += s/foo/bar/g while <>; }

As an added bonus, this last version handles the file not being found better. It also handles multiple files in @ARGV.


  1. This is a recent improvement.

Seeking work! You can reach me at ikegami@adaelis.com

Replies are listed 'Best First'.
Re^2: help declaring variables within perl one-liner
by haukex (Bishop) on Apr 01, 2021 at 19:54 UTC
    Now, one would also quite reasonably argue that one shouldn't execute perl either. However, -i is quite good at avoiding data loss (due to recent improvements), and recreating this wouldn't be trivial.

    $^I

      For some reason, I thought you could only use that once per program. That's not true.

      $ printf 'foo\nbar\n' >file $ perl -e' for (1..2) { local @ARGV = qw( file ); local $^I = ""; print ">$_" while <>; } ' $ cat file >>foo >>bar

      Seeking work! You can reach me at ikegami@adaelis.com