in reply to s/// ignoring subexpressions with variable replacement patterns

The problem is here:

$newpat = '_$1_';

That needs to be in double-quotes, or it gets interpreted literally.

That's the problem with the sample code, at least. It sounds like your production code might be different, since you mention the patterns are user-supplied. Do you have a sample of that?

- Matt Riffle

  • Comment on Re: s/// ignoring subexpressions with variable replacement patterns
  • Download Code

Replies are listed 'Best First'.
Re: Re: s/// ignoring subexpressions with variable replacement patterns
by Anonymous Monk on Oct 04, 2002 at 15:19 UTC

    No, that was indeed the problem. I was under the impression that double-quoting the patterns at that point would cause $vars to be interpolated immediately, rather than in the substitution - but thinking about it, $1 and the like can never be regular variables anyway.

    My mistake; thank you.

Re: Re: s/// ignoring subexpressions with variable replacement patterns
by thelenm (Vicar) on Oct 04, 2002 at 15:22 UTC
    If you just change those single quotes to double quotes, then the value of $1 from the previous pattern will be interpolated before the next substitution ever happens. If you really want the "variable pattern" to behave the same as the "static pattern", you may want to do something like this:
    $newname = 'foo-64-bar'; $oldpat = '-([0-9]+0-'; $newpat = q/'_'.$1.'_'/; $newname =~ s/$oldpat/$newpat/ee; print "variable patterns: $oldname becomes $newname\n";
    That's two /e modifiers, and it's not particularly pretty, but it works. I'm sure there's probably a better way, though. :-)

    -- Mike

    --
    just,my${.02}