Please look at your regex. When you do /$foo/, it interpolates $foo to the string '$bar'. This makes your regex try to match end-of-line ($), followed by 'bar'. That can't happen.

If $foo is '\$bar', then you have a different case. You try to match '\$', which is a literal dollar-sign, followed by 'bar'. That works.

Watch:
friday:~ $ perl -mre=debug $re = '$bar'; print map ">$_\n", grep /$re/, qw( $foo $bar ); compiling RE `$bar' size 5 first at 1 1: EOL(2) 2: EXACT <bar>(5) 5: END(0) anchored `bar' at 0 (checking anchored) minlen 3 Matching `$bar' against `bar' Setting an EVAL scope, savestack=12 1 <$> <bar> | 1: EOL friday:~ $ perl -mre=debug $re = '\$bar'; print map ">$_\n", grep /$re/, qw( $foo $bar ); compiling RE `\$bar' size 4 first at 1 1: EXACT <$bar>(4) 4: END(0) anchored `$bar' at 0 (checking anchored isall) minlen 4 >$bar


japhy -- Perl and Regex Hacker

In reply to Re: Double Interpolation in code///code operator? by japhy
in thread Double Interpolation in // operator? by BlueLines

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.