Besides the fact that someone has already done all the hard work for you with the B::Xref module, the other reason that home-brewed regular expressions aren't going to be a good approach is that there are so many special cases. You cannot simply assume that anything following a $ is a scalar variable. And you cannot assume that scalar variables always consist of $ followed immediately by an alphanumeric name.

Take the following examples, for example:

'This might $look like a scalar, but because of the single quotes, it' +s not to be interpolated' /\w+$/ # Looks like the scalar $/, but isn't. "You owe me \$$money dollars!" # The first $ is escaped. ${$hello[10]} # Which scalar do you want? $hello[10] # Does an array element count as a scalar? $hello{Fred} # Does a hash element count as a scalar? @array = split /\$/, "Hello$world$here$I$come!"; $text =~ s/(.)(.+)/$2$1/; # $1 and $2 are scalars, do you want them?

Ok, enough. There are infinately more examples of cruel and usual situations where your regexp searching for scalars is going to have to be grotesque, for it to do what you want. And even then it probably won't always work.

merlyn suggested using the B::Xref module. I can't think of better advice. My point to this post was to try to convey why that is good advice.

And though it's a moot point, since you're going to use B::Xref (right?) I did want to comment on your question of capturing only unique instances of a scalar, because the discussion applies to so many other situations...

Pushing matches onto a stack (into an array) will do nothing for guaranteeing uniqueness. But using a hash will. Hash keys are always unique. Therefore, hashes provide a perfect way of checking to see if a given key already exists, and a foolproof method of ensuring that duplicates can't possibly be made to exist. Any time you're considering uniqueness to be an essential attribute of something you're storing, think of using a hash.

Update: Thanks Not_a_Number for pointing out my misspelling of merlyn. It's been corrected.

Dave

"If I had my life to do over again, I'd be a plumber." -- Albert Einstein


In reply to Re: Extract variables from file (split? regex? backflip?) by davido
in thread Extract variables from file (split? regex? backflip?) by Lori713

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.