s0ttle has asked for the wisdom of the Perl Monks concerning the following question:

Monks,
This works as expected:
perl -le '$_=q(ab cdefg);s/(^\w+)/$1,/;print;'
but I cannot seem to find a way to get this to work:
perl -le '$_=q(ab cdefg);$x=q($1,);s/(^\w+)/$x/;print;'
I thought about using eval:
perl -le '$_=q(ab cdefg);$x=q($1,);s/(^\w+)/eval$x/e;print;'
but that did not do what I needed which is an output of: ab, cdefg
I tried using qq("") with /ee:
perl -le '$_=q(ab cdefg);$x=q($1,);s/(^\w+)/qq("$x")/ee;print;'
it worked!, but unfortunately puts "" around regular strings. Any suggestions on how I can interpolate $1 inside of a variable with out any side effects similar to qq("")? -tengo mas que aprender

Replies are listed 'Best First'.
Re: Interpolating $1 within a variable
by tachyon (Chancellor) on Oct 27, 2001 at 17:20 UTC

    Oops I missed the fact that you lose the comma doing that - yellow is not the best highlight color BTW. This preserves the comma for you:

    $_=q(ab cdefg); $x=q($1,); s#(^\w+)# my $sub = $1; $x=~s/\$1/$sub/; $x #e; print;

    Update

    That will permanently change the value in $x which you probably don't want so I should have used a scoped temp var like this:

    s#(^\w+)# my $sub = $1; (my $subx = $x) =~ s/\$1/$sub/; $subx #e;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Interpolating $1 within a variable
by Zaxo (Archbishop) on Oct 27, 2001 at 18:08 UTC

    This method may not suit you, since it does not capture the initial string, but it evades the string evaluation technicalities:

    $ perl -le '$_=q(ab cdefg);s/(?<=\w)\b/,/;print;'
    This inserts a comma at the first word-nonword boundary in the string

    After Compline,
    Zaxo

Re: Interpolating $1 within a variable
by tachyon (Chancellor) on Oct 27, 2001 at 17:11 UTC

    You need the /ee modifier. The first /e changes $x to $1, and the next interpolates in the value in $1. You do not need to go qq("$x"). qq/foo/ is the same as "foo" so you effectively have "\"foo\"" You only need to go $x vis

    $_=q(ab cdefg); $x=q($1,); s/(^\w+)/$x/ee; print;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Yes, I realized all of that, but I still don't get the comma in the output.
      :\
Re: Interpolating $1 within a variable
by chromatic (Archbishop) on Oct 28, 2001 at 02:16 UTC
    For almost every class of problem where this is a possible answer, it is the wrong answer. Zaxo's suggestion is the best so far. Perhaps if you were to describe your intent, we would be able to give you a better kind of solution.

    (By wrong, I mean "fragile", "prone to unforseen flaws", and "swatting flies with a sledgehammer".)

Re: Interpolating $1 within a variable
by CharlesClarkson (Curate) on Oct 28, 2001 at 11:21 UTC
    Er, I'm confused:
    $_ = q(ab cdefg); $x = q($1,); s/(^\w+)/qq("$x")/ee; print;
    produced:
    ab, cdefg

    What do you mean by: it worked!, but unfortunately puts "" around regular strings. Any suggestions on how I can interpolate $1 inside of a variable with out any side effects similar to qq("")? What side effects?

    Charles K. Clarkson
      What I meant was that when $x=q(ab,); the output was: "ab," cdefg
      but I found out why it was doing this. I had forgotten to add the extra /e and so I kept running:
      perl -le '$_=q(ab cdefg);$x=q(ab,);s/(^\w+)/qq("$x")/e;print;'
      But the problem is fixed now. Thank you monks.

      -tengo mas que aprender