I first didn't understand what you want to do, but cdarke pointed me to it.

It seems you want to solve two problems:

Firstly, you want to use the value in $regexp as some kind of template which later on uses the value of $thing to replace the string $thing within some$thing. This does not happen, because regular expressions are, to Perl, more or less like double-quoted strings (see perlop, section Quotes and quote-like operators). So, your $regexp will always remain some$thing and never access the value of the variable named $thing.

\Q...\E don't do double interpolation, they do quoting, so the regex meta characters are not seen as meta characters anymore and thus don't help you with your problem.

Having double interpolation the default in Perl would be horrendous, because after double interpolation immediately follows triple interpolation whenever you're using a double-interpolated string in any string context, which would make it nearly impossible to use any modules that construct strings or regular expressions.

To solve your problem at hand, doing template-like replacement on a string, you don't want to use a variable (and its name), but a hash which contains the name/value pairs:

my $regexp = 'some$thing'; my %values = ( 'thing' => 'THING', # ... other value mappings ); print $regexp; $regexp =~ s/\$(\w+)/$values{$1}/ge; print $regexp;

If you're still wondering, yes, it's possible to do the replacement on variables by name too, but Dominus has explained far much better why it's stupid to use a variable as a variable name.

To address your last question, the regular expression formed by $regexp = qr/some\$thing/ matches any string that contains the substring some$thing literally, that is, with a dollar sign in the middle.


In reply to Re^3: variable interpolation in regexps by Corion
in thread variable interpolation in regexps by Locutus

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.