> > It is not so easy to get $1 to work in the way required here
> > with the replace SCALAR_OP since perl does only one
> > level of variable interpolation.
>
> Yes, it's a tricky one. There's a couple of items in the TODO list
> which read:
>
> * Richard Tietjen's patch for stash replace. Allows back references
> (e.g. $1) but it would be nice to find a rock-solid way to implement
> it without relying on unusual ^A delimiter character.
>
If I had read this in advance, I could have listed it in the
category "works, but is ugly" ;-)
> * Further to the above, Craig Barratt has this solution which will be
> going into the next verion (2.05b) unless anyone has any further
> suggestions to make before then.
>
> It would be great if replace handled backreferences. I don't like
> the ^A solution since the string could contain ^A, plus it is a
> security hole. The attempt I posted only works for up to 9
> backreferences and doesn't handle an escaped '\$' and uses nested
> evals:
>
> $str =~ s{$search}{
> my $r = $replace;
> my @d = (0, $1, $2, $3, $4, $5, $6, $7, $8, $9);
> $r =~ s/\$(\d+)/$d[$1]/eg;
>
> $r =~ s/\$(\d+)/$d[$1]/g; # will do without /e here
>
> $r;
> }eg;
>
> I wish there was a perl predefined variable array containing all
> the backreferences (is there one?). You can avoid the hard-coded
> limit of 9 with extra evals, and a bit of work on the re could
> handle the escaped '\$' case, so maybe that would be good enough.
>
That would put the ugly part into the perl module :-[
I thought that a simpler solution would be to create
a eval $string, which would as well support escaped
$ and $' etc., but this looks tedious if you want to
avoid security holes.
...but as already discussed by Andy and Randal,
for my initial problem I don't need replacing at all.
Simply having access to the @matches list would
do...
--
Cheers,
haj