in reply to Extract variables from file (split? regex? backflip?)
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Extract variables from file (split? regex? backflip?)
by Not_a_Number (Prior) on Sep 03, 2003 at 19:29 UTC |