I'm on the perl faq-a-day mailing list and the faq for today 'How can I expand variables in text strings?' has me a bit puzzled.

I understand the first example using symrefs and globals, but the second example with lexicals and /e-evals tripped me up.

They suggest the following for expanding lexicals $foo and $bar:

$text = 'this has a $foo in it and a $bar'; $text =~ s/(\$\w+)/$1/eeg; # needs /ee, not /e
Its that double /ee that gets me. I did some testing and they are correct, a single /e wont work. In fact, a single /e doesn't seem to modify the default behaviour at all.
#!/usr/bin/perl -wT use strict; no strict 'refs'; my $foo = 'switch'; my $bar = 'button'; my $text1 = my $text2 = my $text3 = 'this has a $foo in it and a $bar' +; $text1 =~ s/(\$\w+)/$1/g; $text2 =~ s/(\$\w+)/$1/ge; $text3 =~ s/(\$\w+)/$1/gee; print "T1 /g : $text1\n"; print "T2 /ge : $text2\n"; print "T3 /gee : $text3\n"; =OUTPUT T1 /g : this has a $foo in it and a $bar T2 /ge : this has a $foo in it and a $bar T3 /gee : this has a switch in it and a button
Why do /g and /ge above produce the same string? Is it doing an eval '$1' instead of a eval "$1"?

-Blake


In reply to /ee regex modifier by blakem

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.