Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: substitution interpolation?

by h0mee (Acolyte)
on May 31, 2001 at 05:20 UTC ( [id://84428]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://84428]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-16 04:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found