Passing a $1 within a scalar to a s/// doesn't work. Why?

Discussion in 'Perl Misc' started by Rob Campbell, Jun 3, 2005.

  1. Rob Campbell

    Rob Campbell Guest

    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
    --
    remove "FERRET" to reply
     
    Rob Campbell, Jun 3, 2005
    #1
    1. Advertising

  2. Rob Campbell wrote:
    > 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.


    perldoc -q "expand variables"

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jun 3, 2005
    #2
    1. Advertising

  3. Jim Gibson <> wrote:

    > I am not quite sure why '$d =~ /$oldpart/$newpart/e;' doesn't work,



    Because the replacement part is now code rather than a string.

    If you instead said

    $foo = $newpart;

    you would expect a literal '$1' in $foo's value, as there is only
    one round of evaluation and that is used to fetch the value from $newpart.

    Same thing for the code above.


    > but
    > wrapping the replacement text in double quotes and forcing another
    > round of evaluation works:



    That would be like

    $foo = eval $newpart;

    were you _would_ expect variables in $newpart's value to be eval()uated.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 3, 2005
    #3
  4. Brian McCauley, Jun 4, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    935
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,130
    Smokey Grindel
    Dec 2, 2006
  3. aj
    Replies:
    1
    Views:
    290
    Ben Pfaff
    Jun 25, 2007
  4. Clint Olsen
    Replies:
    6
    Views:
    388
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  5. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    179
    Arndt Jonasson
    Jan 27, 2005
Loading...

Share This Page