First, I've got to say that I didn't know that a global pattern match returned an array by default (i.e. no memorization with brackets) of all the matches. I am only beginning to imagine what that could do in conjunction with map.

So, all you really need is a better regexp:       @stuff = m#(?:[^\\]|^)(\$(?:[a-zA-z_][a-zA-z0-9]*|[0-9]))#g; This ensures that the variable is not prefixed by a backslash, or that it is at the beginning of the line ('^'). The 'questionable' brackets '(?: ...)' are just groupings that aren't memorized, in case you aren't familiar with that modifier.

Still, you're open to scenarios where things that look like variables, but aren't, are picked up for whatever reason. This could include something you have in comments, single-quoted or qX-quoted strings, or wierd stuff getting pulled out of regular expressions.

So, the regexp above is intentionally a bit tame, and it won't pick up on some common things, such as:
\$x; # A reference to $x, but ignored by regexp ${x}; # Same as $x, but ignored $x[10]; # Shows up as $x, but is really @x
So, the utility of this match is limited, but hopefully it will suit your needs.

In reply to Re: regex query by tadman
in thread regex query by cghost23

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.