When using /m with a regexp, $ becomes particularly ambiguous; it means both 'end of line' and 'interpolate a variable'. (Although this is true without /m, it causes me grief much less often in that case.) I've never seen the rules documented for how Perl disambiguates. I've looked. What I find is that they're explained as "magic'; 'Perl usually does what you mean.'

What are the rules? And what's the best idiom for influencing the outcome?

For example, consider /foo$.*^bar/ms - is this $. (the variable) or EOL followed by random stuff?

I find myself introducing non-capturing parens, as it seems that if a paren can be matched it won't be used as a variable name.

A more realistic example:
$QQ->get_private_key_string =~ /(^-----BEGIN (?:(RSA) )?PRIVATE KEY-----$(?:.*)^-----END (?:RSA )?PRIVATE KEY-----$(?:\s*))/ms;

This gets quite entertaining, considering that $. $( and $) are variables.

It would be nice to have a deterministic way to code these cases. And may I suggest that this should be documented?

Thanks.

This communication may not represent my employer's views, if any, on the matters discussed.


In reply to Disambuating $ in (especially /m) regexps by tlhackque

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.