Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey all! i was wondering how to filter out variables from a multiline string.. example:
tttt $x=123 ttt @c=qw/1 2 3/; tt $xxx ttttt @something
so the output would be: $x @c $xxx. thanks guys

Replies are listed 'Best First'.
Re: regex help
by JavaFan (Canon) on Apr 21, 2012 at 20:47 UTC
    Questions:
    • What's the criteria that makes that @something isn't part of the output?
    • What ought to be the output when variables like $array[1], @hash{"foo", "bar"}, qq[foo${bar}baz], @{$ref}{1, 2}, ${__PACKAGE__ . '::foo'}, etc are being used?
      no criteria, just missed it out by accident. all i need to do is extract variable,array names. variables like $array1 should just be $array. i guess i will need to avoid ${__PACKAGE__
        Perhaps then all what you need is (untested):
        /(?(DEFINE) (?<sigil> [\$\@%&*]) (?<name_atom> (?: [_A-Za-z][_A-Za-z0-9]* | [0-9]+) ) (?<separator> (?: :: | ')) (?<name> (?: (?&separator)? (?&name_atom) (?:(?&separator) (?&name_atom))*)) (?<punct> \S)) (?&sigil) \s* (?:(?&name) | (?&punct)) /x
Re: regex help
by NetWallah (Canon) on Apr 21, 2012 at 21:19 UTC
    Make sure the code you are running throught he regex is not interpolated.
    perl -e 'print qq|$_\n| for q|tttt $x=123 ttt @c=qw/1 2 3/; tt $xxx tt +ttt @something|=~/([\$\@\%]\w+)/g' $x @c $xxx @something
    Update:This code is simplistic. JavaFan's regex (++), above, is much more rigorous.

                 All great truths begin as blasphemies.
                       ― George Bernard Shaw, writer, Nobel laureate (1856-1950)