I have this perl command:
# Result: o
#
echo foo | perl -pe 's/f(o)o/$1/'
I'd like the replacement expression to be passed via env variable:
echo foo | REPLACE='$1' perl -pe 's/f(o)o/$ENV{REPLACE}/'
however, in this case, Perl does not interpret the matching variable $1
; instead, it just replaces it with the env variable value, so that the result is $1
.
I know I can work this around via Bash string interpolation:
REPLACE='$1'
echo foo | perl -pe 's/f(o)o/'"$REPLACE"'/'
however, this is hackish (and limited).
Is the a more idiomatic Perl way to accomplish this task?