`...` is equilvalent to readpipe(qq`...`) (basically readpipe("...")), which is to say it interpolates. As such, you wanted the following:
The program can be simplified.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);
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.
Seeking work! You can reach me at ikegami@adaelis.com
In reply to Re: help declaring variables within perl one-liner
by ikegami
in thread help declaring variables within perl one-liner
by Special_K
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |