String::InterpolatedVariables - Extract variable names from interpolated strings.
Version 1.1.0
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/guillaumeaubert/String-InterpolatedVariables/issues
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/String-InterpolatedVariables
CPAN Ratings
http://cpanratings.perl.org/d/String-InterpolatedVariables
MetaCPAN
https://metacpan.org/release/String-InterpolatedVariables
Guillaume Aubert, <aubertg at cpan.org>.
<aubertg at cpan.org>
Copyright 2014-2017 Guillaume Aubert.
This code is free software; you can redistribute it and/or modify it under the same terms as Perl 5 itself.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LICENSE file for more details.
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.