blib/lib/String/InterpolatedVariables.pm

filter: Statementstmt 100% 18 / 18 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 100% 25 / 25 CC 2 SCAR 3.4
CC
2
Cov
100%
extract
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.4
↑ Summary
1 package String::InterpolatedVariables;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '2.000000';
7  
8 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 2 Readonly::Scalar my $VARIABLES_REGEX => qr/
12 # Ignore escaped sigils, since those wouldn't get interpreted as variables to interpolate.
13 (?<!\\)
14 # Allow literal, non-escapy backslashes.
15 (?:\\\\)*
16 (
17 # The variable needs to start with a sigil.
18 [\$\@]
19 # Account for the dereferencing, such as "$$" or "@$".
20 \$?
21 # Variable name.
22 (?:
23 # Note: include '::' to support package variables here.
24 \{(?:\w+|::)\} # Explicit {variable} name.
25 |
26 (?:\w|::)+ # Variable name.
27 )
28 # Catch nested data structures.
29 (?:
30 # Allow for a dereferencing ->.
31 (?:->)?
32 # Can be followed by either a hash or an array.
33 (?:
34 \{(?:\w+|'[^']+'|"[^"]+")\} # Hash element.
35 |
36 \[['"]?\d+['"]?\] # Array element.
37 )
38 )*
39 )
40 /x;
41  
42  
43 9 ▶sub extract {
Subroutine extract: called CC=2 SCAR=6.9
Pod documented
44 9 my ($string) = @_;
45  
46 9 my $variables = [];
47 9 ▶ while ( my ($variable) = $string =~ $VARIABLES_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($variable) = $string =~ /$VARIABLES_REGEX/) 11 9 20
48 11 push( @$variables, $variable );
49 11 $string =~ s/\Q$variable\E//g;
50 }
51  
52 9 return $variables;
53 }
54  
55  
56 2 1;
57  
58 =pod
59  
60 =encoding UTF-8
61  
62 =head1 NAME
63  
64 String::InterpolatedVariables - Extract variable names from interpolated strings.
65  
66 =head1 VERSION
67  
68 version 2.000000
69  
70 =head1 SYNOPSIS
71  
72 use String::InterpolatedVariables;
73  
74 my $variables = String::InterpolatedVariables::extract(
75 'A $test->{string} from a PPI::Token::Quote::Double $object.'
76 );
77  
78 # $variables now contains:
79 # [
80 # '$test->{string}',
81 # '$object',
82 # ]
83  
84 =head1 DESCRIPTION
85  
86 String::InterpolatedVariables offers a way to extract the name of the variables
87 that are present in interpolated strings.
88  
89 This is particularly useful if you are using L<PPI> to parse Perl documents,
90 and you want to know what variables would be interpolated inside the
91 L<PPI::Token::Quote::Double> and L<PPI::Token::Quote::Interpolate> objects you
92 find there. A practical example of this use can be found in
93 L<Perl::Critic::Policy::ValuesAndExpressions::PreventSQLInjection>.
94  
95 =head1 FUNCTIONS
96  
97 =head2 extract()
98  
99 Extract variables from interpolated strings.
100  
101 my $variables = String::InterpolatedVariables::extract(
102 'A $test->{string} from a PPI::Token::Quote::Double $object.'
103 );
104  
105 # $variables now contains:
106 # [
107 # '$test->{string}',
108 # '$object',
109 # ]
110  
111 Note that you need to pass the text of the string, even if the string itself is
112 destined to be interpolated. In other words, passing C<"Test $test"> would not
113 find any variables, as C<$test> would get interpolated by Perl before the
114 string is passed to the C<extract()> function. This function is thus more
115 useful if you are using using a tool such as L<PPI> to read Perl code, since
116 PPI will give you access to the text of the string itself for strings that
117 would otherwise be interpolated during execution.
118  
119 =head1 BUGS
120  
121 Please report any bugs or feature requests through the web interface at
122 L<https://github.com/guillaumeaubert/String-InterpolatedVariables/issues>.
123 I will be notified, and then you'll automatically be notified of progress on
124 your bug as I make changes.
125  
126 =head1 SUPPORT
127  
128 You can find documentation for this module with the perldoc command.
129  
130 perldoc String::InterpolatedVariables
131  
132 You can also look for information at:
133  
134 =over 4
135  
136 =item * GitHub (report bugs there)
137  
138 L<https://github.com/oalders/String-InterpolatedVariables/issues>
139  
140 =item * MetaCPAN
141  
142 L<https://metacpan.org/release/String-InterpolatedVariables>
143  
144 =back
145  
146 =head1 AUTHOR
147  
148 Guillaume Aubert <aubertg@cpan.org>
149  
150 =head1 COPYRIGHT AND LICENSE
151  
152 This software is copyright (c) 2014 by Guillaume Aubert.
153  
154 This is free software; you can redistribute it and/or modify it under
155 the same terms as the Perl 5 programming language system itself.
156  
157 =cut
158  
159 __END__
↑ Summary