Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: help declaring variables within perl one-liner

by ikegami (Patriarch)
on Apr 01, 2021 at 19:48 UTC ( [id://11130688]=note: print w/replies, xml ) Need Help??


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 (Archbishop) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130688]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-20 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found