in reply to substitution interpolation?

Okay, I should have continued searching more carefully on the site- I found the answer to my question: How can I combine backreferences and regex interpolation?

However, I have no idea why some the mechanisms described work- anyone care to enlighten me?

Edit by tye

Replies are listed 'Best First'.
Re: Re: substitution interpolation?
by lestrrat (Deacon) on May 31, 2001 at 06:35 UTC

    First off it's wrong to use qr// on rhs of s///

    You must realize that the left side of s/// is the regular expression, and that the right side is merely the the substitute string ( or that which produces the substitute string)

    So in effect what you are doing is:

    $var =~ s/ < regex $expr > / < scalar representation of $sub > /

    This is why you end up with the string (?-xism: spooge), which is the scalar value of $sub


    In the node you referred to, they eval'ed the rhs one way or the other. mdillon eval'ed the rhs only ( $1 . "ar" ) by using /ee ( read perlop ), and knight eval'ed the entire perl expression ( "s/$lhs/$rhs/" ).

      Thanks for pointing out my totally clueless use of qr// (was going on the advice of someone on IRC when I went down that path, instead of RTFM'ing). What I don't understand is- why does the evaling the entire expression work? Does it force interpolation of $lhs and $rhs before the expression is evaluated? In experiments,
      $_ = $var; eval "s/$lhs/$rhs/";
      works, but:
      eval "$var =~ /$lhs/$rhs/;";
      doesn't work- what am I missing?

        try this:

        eval "\$var =~ s/$lhs/$rhs/";

        update:fixed typo