R
Rob Campbell
Hi,
Still in the early stages of this Perl business but haven't been able to
find an answer to this question anywhere. Perhaps someone here can help.
I have a little function which takes as its inputs two regular expressions.
The first line of the function is:
my($simulate,$oldpart,$newpart,@rest) = @ARGV;
The function just uses these to perform a substitution:
$d =~ s/$oldpart/$newpart/;
The problem is that when I want to capture text in the first expression to
use it as a "$1" in the second, it don't work: the '$1' is interpretted
literally.
i.e.
if $oldpart contains 'binoise\w*(G1_[0-9]+)\.src', and and $newpart contains
'_binoise.src'; then I get, for example:
../binoise9G1_12.src -> ./_binoise.src
Not what I want, but fine, it works. However, if $newpart now contains
'$1_binoise.src' then I get:
../binoise9G1_12.src -> ./$1_binoise.src
Huh? Why is the $1 being interpreted literally. I've not done anything else
silly since if I hard-code my substitution so that it looks like this:
$d =~ s/$oldpart/$1_binoise/;
Then I get what I want:
../binoise9G1_12.src -> ./G1_12_binoise
I've tried escaping the $ in $newpart but that doesn't work either. Why does
Perl interpret my substitution command differently when I feed it a
variable compared to when I feed it the regexp directly? How do I get it to
play ball and allow a $1 to be interpretted correctly when it's passed to
my s/// command in a scalar.
Thanks!
Rob
Still in the early stages of this Perl business but haven't been able to
find an answer to this question anywhere. Perhaps someone here can help.
I have a little function which takes as its inputs two regular expressions.
The first line of the function is:
my($simulate,$oldpart,$newpart,@rest) = @ARGV;
The function just uses these to perform a substitution:
$d =~ s/$oldpart/$newpart/;
The problem is that when I want to capture text in the first expression to
use it as a "$1" in the second, it don't work: the '$1' is interpretted
literally.
i.e.
if $oldpart contains 'binoise\w*(G1_[0-9]+)\.src', and and $newpart contains
'_binoise.src'; then I get, for example:
../binoise9G1_12.src -> ./_binoise.src
Not what I want, but fine, it works. However, if $newpart now contains
'$1_binoise.src' then I get:
../binoise9G1_12.src -> ./$1_binoise.src
Huh? Why is the $1 being interpreted literally. I've not done anything else
silly since if I hard-code my substitution so that it looks like this:
$d =~ s/$oldpart/$1_binoise/;
Then I get what I want:
../binoise9G1_12.src -> ./G1_12_binoise
I've tried escaping the $ in $newpart but that doesn't work either. Why does
Perl interpret my substitution command differently when I feed it a
variable compared to when I feed it the regexp directly? How do I get it to
play ball and allow a $1 to be interpretted correctly when it's passed to
my s/// command in a scalar.
Thanks!
Rob