String::InterpolatedVariables - Extract variable names from interpolated strings.
version 2.000000
use String::InterpolatedVariables; my $variables = String::InterpolatedVariables::extract( 'A $test->{string} from a PPI::Token::Quote::Double $object.' ); # $variables now contains: # [ # '$test->{string}', # '$object', # ]
String::InterpolatedVariables offers a way to extract the name of the variables that are present in interpolated strings.
This is particularly useful if you are using PPI to parse Perl documents, and you want to know what variables would be interpolated inside the PPI::Token::Quote::Double and PPI::Token::Quote::Interpolate objects you find there. A practical example of this use can be found in Perl::Critic::Policy::ValuesAndExpressions::PreventSQLInjection.
Extract variables from interpolated strings.
my $variables = String::InterpolatedVariables::extract( 'A $test->{string} from a PPI::Token::Quote::Double $object.' ); # $variables now contains: # [ # '$test->{string}', # '$object', # ]
Note that you need to pass the text of the string, even if the string itself is destined to be interpolated. In other words, passing "Test $test" would not find any variables, as $test would get interpolated by Perl before the string is passed to the extract() function. This function is thus more useful if you are using using a tool such as PPI to read Perl code, since PPI will give you access to the text of the string itself for strings that would otherwise be interpolated during execution.
"Test $test"
$test
extract()
Please report any bugs or feature requests through the web interface at https://github.com/guillaumeaubert/String-InterpolatedVariables/issues. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc String::InterpolatedVariables
You can also look for information at:
GitHub (report bugs there)
https://github.com/oalders/String-InterpolatedVariables/issues
MetaCPAN
https://metacpan.org/release/String-InterpolatedVariables
Guillaume Aubert <aubertg@cpan.org>
This software is copyright (c) 2014 by Guillaume Aubert.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install String::InterpolatedVariables, copy and paste the appropriate command in to your terminal.
cpanm
cpanm String::InterpolatedVariables
CPAN shell
perl -MCPAN -e shell install String::InterpolatedVariables
For more information on module installation, please visit the detailed CPAN module installation guide.