line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package String::InterpolatedVariables; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
65147
|
use strict; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
53
|
|
4
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
68
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '2.000000'; |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
996
|
use Readonly; |
|
2
|
|
|
|
|
6866
|
|
|
2
|
|
|
|
|
455
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub extract { |
44
|
9
|
|
|
9
|
1
|
4625
|
my ($string) = @_; |
45
|
|
|
|
|
|
|
|
46
|
9
|
|
|
|
|
16
|
my $variables = []; |
47
|
9
|
|
|
|
|
109
|
while ( my ($variable) = $string =~ $VARIABLES_REGEX ) { |
48
|
11
|
|
|
|
|
22
|
push( @$variables, $variable ); |
49
|
11
|
|
|
|
|
144
|
$string =~ s/\Q$variable\E//g; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
9
|
|
|
|
|
22
|
return $variables; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
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 to parse Perl documents, |
90
|
|
|
|
|
|
|
and you want to know what variables would be interpolated inside the |
91
|
|
|
|
|
|
|
L and L objects you |
92
|
|
|
|
|
|
|
find there. A practical example of this use can be found in |
93
|
|
|
|
|
|
|
L. |
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 function. This function is thus more |
115
|
|
|
|
|
|
|
useful if you are using using a tool such as L 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. |
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 |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item * MetaCPAN |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
L |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=back |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 AUTHOR |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Guillaume Aubert |
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__ |