line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Find::Scalar; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
54191
|
use 5.006001; |
|
1
|
|
|
|
|
11
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
16
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
534
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.07'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new{ |
10
|
1
|
|
|
1
|
1
|
843
|
my ($class) = @_; |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
|
|
3
|
my $self = {}; |
13
|
1
|
|
|
|
|
1
|
bless $self,$class; |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
|
|
3
|
$self->_Counter(0); |
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
|
|
3
|
return $self; |
18
|
|
|
|
|
|
|
}# new |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub find{ |
21
|
1
|
|
|
1
|
1
|
241
|
my ($self,$text) = @_; |
22
|
1
|
|
|
|
|
2
|
my @array = (); |
23
|
1
|
|
|
|
|
3
|
$self->_Counter(0); |
24
|
|
|
|
|
|
|
|
25
|
1
|
50
|
|
|
|
4
|
return if !defined $text; |
26
|
|
|
|
|
|
|
|
27
|
1
|
|
|
|
|
9
|
$text =~ s,<<'(.*?)'.*?\n\1,,sg; |
28
|
1
|
|
|
|
|
6
|
$text =~ s,'.*?',,sg; |
29
|
1
|
|
|
|
|
12
|
$text =~ s,q~.*?~,,sg; |
30
|
1
|
|
|
|
|
38
|
@array = $text =~ m/(?:(\$\w+(?:->)?(?:\[\$?\w+\]|\{\$?\w+\}))|(\$\{\w+\})|(\$\w+))/sg; |
31
|
1
|
|
|
|
|
3
|
@array = grep{defined}@array; |
|
39
|
|
|
|
|
50
|
|
32
|
|
|
|
|
|
|
|
33
|
1
|
|
|
|
|
4
|
$self->_Elements(@array); |
34
|
1
|
50
|
|
|
|
3
|
return wantarray ? @{$self->_Elements()} : $self->_Elements(); |
|
0
|
|
|
|
|
0
|
|
35
|
|
|
|
|
|
|
}# find |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub unique{ |
38
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
39
|
0
|
|
|
|
|
0
|
my %seen; |
40
|
0
|
|
|
|
|
0
|
my @unique = grep{!$seen{$_++}}@{$self->_Elements()}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
41
|
0
|
|
|
|
|
0
|
return \@unique; |
42
|
|
|
|
|
|
|
}# unique |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub count{ |
45
|
0
|
|
|
0
|
1
|
0
|
my ($self,$name) = @_; |
46
|
0
|
|
|
|
|
0
|
my %counter; |
47
|
0
|
|
|
|
|
0
|
$counter{$_}++ for(@{$self->_Elements()}); |
|
0
|
|
|
|
|
0
|
|
48
|
0
|
|
|
|
|
0
|
return $counter{$name}; |
49
|
|
|
|
|
|
|
}# count |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub hasNext{ |
52
|
14
|
|
|
14
|
1
|
710
|
my ($self) = @_; |
53
|
14
|
|
|
|
|
17
|
my $count = $self->_Counter(); |
54
|
14
|
100
|
|
|
|
15
|
if($count > scalar(@{$self->_Elements()}) - 1){ |
|
14
|
|
|
|
|
34
|
|
55
|
1
|
|
|
|
|
2
|
return 0; |
56
|
|
|
|
|
|
|
} |
57
|
13
|
|
|
|
|
18
|
return 1; |
58
|
|
|
|
|
|
|
}# hasNext |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub nextElement{ |
61
|
13
|
|
|
13
|
1
|
33
|
my ($self) = @_; |
62
|
13
|
|
|
|
|
14
|
my $count = $self->_Counter(); |
63
|
13
|
|
|
|
|
15
|
my $all = $self->_Elements(); |
64
|
13
|
|
|
|
|
15
|
my $element = undef; |
65
|
13
|
50
|
|
|
|
17
|
if($count < scalar(@$all)){ |
66
|
13
|
|
|
|
|
12
|
$element = ${$all}[$count]; |
|
13
|
|
|
|
|
18
|
|
67
|
|
|
|
|
|
|
} |
68
|
13
|
|
|
|
|
22
|
$self->_Counter(++$count); |
69
|
13
|
|
|
|
|
18
|
return $element; |
70
|
|
|
|
|
|
|
}# nextElement |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _Counter{ |
73
|
42
|
|
|
42
|
|
47
|
my ($self,$count) = @_; |
74
|
42
|
100
|
|
|
|
61
|
$self->{Counter} = $count if(defined $count); |
75
|
42
|
|
|
|
|
49
|
return $self->{Counter}; |
76
|
|
|
|
|
|
|
}# _Counter |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _Elements{ |
79
|
29
|
|
|
29
|
|
36
|
my ($self,@elements) = @_; |
80
|
29
|
100
|
|
|
|
44
|
$self->{Elements} = [@elements] if(scalar(@elements) > 0); |
81
|
29
|
|
|
|
|
43
|
return $self->{Elements}; |
82
|
|
|
|
|
|
|
}# _Elements |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=pod |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=encoding UTF-8 |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 NAME |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Text::Find::Scalar |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 VERSION |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
version 0.07 |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 SYNOPSIS |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
use Text::Find::Variable; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $finder = Text::Find::Variable->new(); |
103
|
|
|
|
|
|
|
my $arrayref = $finder->find($string); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# or |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
$finder->find($string); |
108
|
|
|
|
|
|
|
while($finder->hasNext()){ |
109
|
|
|
|
|
|
|
print $finder->nextElement(); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 DESCRIPTION |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
This Class helps to find all Scalar variables in a text. It is recommended to |
115
|
|
|
|
|
|
|
use L to parse Perl programs. This module should help to find SCALAR names |
116
|
|
|
|
|
|
|
e.g. in Error messages. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Scalars that should be found: |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=over 10 |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item * double quoted |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
"$foo" |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item * references |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
$foo->{bar} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item * elements of arrays |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
$array[0] |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=back |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Scalars that are not covered |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=over 10 |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item * single quoted |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
'$foo' |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=back |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 NAME |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Text::Find::Scalar - Find scalar names in a text. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 EXAMPLE |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
#!/usr/bin/perl |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
use strict; |
157
|
|
|
|
|
|
|
use warnings; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
use Text::Find::Scalar; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
my $string = q~This is a $variable |
162
|
|
|
|
|
|
|
another $variable and another "$eine", but '$no' is not found. |
163
|
|
|
|
|
|
|
A $reference->{$key} is found. An array element $array[0] |
164
|
|
|
|
|
|
|
is also found~; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
my $finder = Text::Find::Scalar->new(); |
167
|
|
|
|
|
|
|
my @scalars = $finder->find($string); |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
print $_,"\n" for(@scalars); |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
prints |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
/homes/reneeb/community>find_scalar.pl |
174
|
|
|
|
|
|
|
$variable |
175
|
|
|
|
|
|
|
$variable |
176
|
|
|
|
|
|
|
$eine |
177
|
|
|
|
|
|
|
$reference->{$key} |
178
|
|
|
|
|
|
|
$array[0] |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 METHODS |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 new |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
my $finder = Text::Find::Scalar->new(); |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
creates a new Text::Find::Scalar object. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 find |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
my $string = q~Test $test $foo '$bar'~; |
191
|
|
|
|
|
|
|
my $arrayref = $finder->find($string); |
192
|
|
|
|
|
|
|
my @found = $finder->find($string); |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
parses the text and returns an arrayref that contains all matches. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head2 hasNext |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
while($finder->hasNext()){ |
199
|
|
|
|
|
|
|
print $finder->nextElement(); |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
returns 1 unless the user walked through all matches. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head2 nextElement |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
print $finder->nextElement(); |
207
|
|
|
|
|
|
|
print $finder->nextElement(); |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
returns the next element in list. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head2 unique |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
my $uniquenames = $finder->unique(); |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
returns an arrayref with a list of all scalars, but each match appears just once. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head2 count |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
my $counter = $finder->count('$foo'); |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
returns the number of appearances of one scalar. |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head2 "private" methods |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head3 _Elements |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
returns an arrayref of all scalars found in the text |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head3 _Counter |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head1 AUTHOR |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Renee Baecker, Emodule@renee-baecker.deE |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
Copyright (C) 2006 by Renee Baecker |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
240
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.6 or, |
241
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=head1 AUTHOR |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
Renee Baecker |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
This software is copyright (c) 2016 by Renee Baecker. |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
252
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=cut |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
__END__ |