perl -s -e "$data='abcdef'; $data=~s#$r#$s#; print $data" -- -r=(abc) -s=$1helloproduces
$1hellodefI'd rather it produce
abchellodefAdding an eval
perl -s -e "$data='abcdef'; eval qq{\$data=~s#$r#$s#}; print $data" -- -r=(abc) -s=$1hellodoes the trick, but uses eval. Seems like it would be nice to have an option to "reinterpolate" a string. Am I missing another way to do this? I suppose, with block interpolation "${\func()}" that interpolation isn't any safer than eval, but it would be easier to use... especially if it were a substitution operation modifier s///R like e, possibly usable more than once s///RRR And yes, I could force the user to write it as code, and use s///e, but that is cumbersome for the user:
perl -s -e "$data='abcdef'; $data=~s#$r#$s#ee; print $data" -- -r=(abc) -s=$1.'hello'Might be nice to have qR{$foo} that would do the interpolation of qq{$foo} and then reinterpolate. And maybe qRRR{$foo} for 3 reinterpolations...
The solution
Anonymous Monk's suggestion of String::Interpolate seems to suffice:
perl -s -MString::Interpolate -e "$data='abcdef'; $data=~s#$r#$s#; $data = String::Interpolate->interpolate( $data ); print $data" -- -r=(abc) -s=$1hello
In reply to reinterpolation of regexp (and strings?) by AlwaysLearning
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |