G'day xaphod,

[Assumption: As already stated, you need to tell us what you do expect, not just that you didn't get what you expected. I'm assuming that, in the three blocks of output, you want "huey & donald", "dewey & donald" and "louis & donald", respectively.]

AM has already pointed out the issue. In Perl v5.14, non-destructive substitution was introduced, which could get around this problem (see "perl5140delta: Non-destructive substitution" for details and additional information).

It looks like you've probably over-engineered your solution and, as result, generated the unexpected output. Your nested loops could have been written like the following: I've commented out all the over-engineered code so you can see how much simpler it could have been.

foreach my $who (@list) { #my $sub = "\$ln =~ s/scrooge (& donald)/$who \$1/g;"; print "$who\n"; foreach my $ln (@lines) { #eval $sub; #print "$ln\n"; print $ln =~ s/scrooge (& donald)/$who $1/r, "\n"; } print "\n"; }

That produces the output as per my "Assumption".

If your version of Perl is older than 5.14, which is now over six years old[perlhist], consider upgrading but, for now, you'll need to code this the old way by assigning the original text to a new variable first, then performing the substitution on that new variable, leaving the original unchanged.

By the way, in your original regex, you used the 'g' modifier. This is completely unnecessary: it's for performing multiple matches in a single regex; not for single matches that happen to be performed on multiple occasions. These three one-liners should show the difference:

$ perl -E 'my $x = "AAA"; $x =~ s/A/B/; say $x' BAA $ perl -E 'my $x = "AAA"; $x =~ s/A/B/ for 0 .. length($x) - 1; say $x +' BBB $ perl -E 'my $x = "AAA"; $x =~ s/A/B/g; say $x' BBB

That's just a guess as to why you thought you needed a 'g' modifier. See "perlre: Modifiers" for details.

— Ken


In reply to Re: variables in substition/eval by kcott
in thread variables in substition/eval by xaphod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.