How to trim a Perl string by speeding up a replacement string in s ///?

I'm not sure what exactly to call it, but I was able to reproduce my problem with two single line.

Starting with the test.txt file containing the following:

foo

After executing the following command (in bash):

perl -n -e "s/(\w)oo/$1ar/; print;" test.txt

output is equal to ' far'

However, when I enter a variable containing the replacement string,

perl -n -e '$bar = q($1ar); s/(\w)oo/$bar/; print;' test.txt

the output is equal to. ' $1ar'

What do I need to change so that the second program also displays ' far' and what keywords do I need to know so that this answer is answerable?

Also, I tried changing the second to s /// e, with no effect.

Edit: This was not the question I wanted to ask here .

+3
source share
3

( bash, ):

perl -n -e '$bar = "\${1} . ar"; s/(\w)oo/$bar/ee; print;' test.txt

ee . . perlop (1).

+6

, - - , :

perl -n -e '$bar=q($1."ar"); s/(\w)oo/eval($bar)/e; print;' test.txt

$bar , $1 'ar', ( e).

+1

/ ee by @maxelost is correct.

This works in a perl program.
$bar = q("${1}ar");
$str = "foo";
$str =~ s/(\w)oo/$bar/ee;
print $str;

so I assume this works in bash:

perl -e '$bar = q("${1}ar"); s/(\w)oo/$bar/ee; print;' test.txt

in the windows:

perl -e "$bar = q(\"${1}ar\"); s/(\w)oo/$bar/ee; print;" test.txt

+1
source

Source: https://habr.com/ru/post/1789619/


All Articles