blib/lib/Regexp/Result.pm

filter: Statementstmt 85.7% 84 / 98 Branchbran 12.5% 1 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.1% 27 / 31 Podpod 66.6% 4 / 6 totaltotal 81.1% 116 / 143 CC 6 SCAR 6.9
CC
6
Cov
80%
last_numbered_match_start
6.0
last_named_paren_match
6.0
named_paren_matches
6.0
CRAP 2.0
↑ Summary
1 package Regexp::Result;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use 5.010; # we require ${^MATCH} etc
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 our $VERSION = '0.004';
7 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our @EXPORT_OK = qw(rr);
9 2 ▶use Sub::Name 'subname';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Regexp::Result - store information about a regexp match for later retrieval
14  
15 =head1 SYNOPSIS
16  
17 $foo =~ /(a|an|the) (\w+)/;
18 my $result = Regexp::Result->new();
19
20 # or, equivalently
21 my $result = rr;
22  
23 # ...
24 # some other code which potentially executes a regular expression
25  
26 my $determiner = $result->c(1);
27 # i.e. $1 at the time when the object was created
28  
29 Have you ever wanted to retain information about a regular expression
30 match, without having to go through the palaver of pulling things out
31 of C<$1>, C<pos>, etc. and assigning them each to temporary variables
32 until you've decided what to use them as?
33  
34 Regexp::Result objects, when created, contain as much information about
35 a match as perl can tell you. This means that you just need to create
36 one variable and keep it.
37  
38 Hopefully, your code will be more comprehensible when it looks like
39 C<< $result->last_numbered_match_start->[-1] >>,
40 instead of C<$-[-1]>. The documentation for the punctuation
41 variables, by the way, is hidden away in C<perldoc perlvar>
42 along with scary things like C<^H>. I've copied most of it and/or
43 rewritten it below.
44  
45 =head1 FUNCTIONS
46  
47 =head3 rr
48  
49 use Regexp::Result qw(rr);
50
51 $foo =~ /(a|an|the) (\w+)/;
52 my $result = rr;
53  
54 Equivalent to calling C<< Regexp::Result->new() >>.
55  
56 =cut
57  
58 1 ▶sub rr {
Subroutine rr: called CC=1 SCAR=0.0
Pod documented
59 1 __PACKAGE__->new
60 }
61  
62 =head1 METHODS
63  
64 =head3 new
65  
66 Creates a new Regexp::Result object. The object will gather data from
67 the last match (if successful) and store it for later retrieval.
68  
69 Note that almost all of the contents are read-only.
70  
71 =cut
72  
73 =head3 numbered_captures
74  
75 This accesses C<$1>, C<$2>, etc as C<< $rr->numbered_captures->[0] >>
76 etc. Note the numbering difference!
77  
78 =cut
79  
80 has numbered_captures=>
81 is => 'ro',
82 7 ▶ default => sub{
Subroutine __ANON__: called CC=2 SCAR=6.9
83 7 my $captures = [];
84 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
85 7 for my $i (1..$#-) { #~ i.e until the end of LAST_MATCH_START
86 9 push @$captures, ${$i};
87 }
88 2 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
89 7 $captures;
90 2 };
91  
92 =head3 c
93  
94 This accesses the contents of C<numbered_captures>, but uses numbers from 1
95 for comparability with C<$1>, C<$2>, C<$3>, etc.
96  
97 =cut
98  
99 5 ▶sub c {
Subroutine c: called CC=2 SCAR=7.6
Pod documented
100 5 my ($self, $number) = @_;
101 5 ▶ if ($number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number) 5 0 5
102 #:todo: consider allowing more than one number
103 5 return $self->numbered_captures->[$number - 1];
104 }
105 0 return undef;
106 }
107  
108 14 ▶sub _has_scalar {
Subroutine _has_scalar: called CC=1 SCAR=0.0
109 14 my ($name, $creator) = @_;
110 14 has $name =>
111 is => 'ro',
112 default => $creator
113 }
114  
115 #~ _has_array
116 #~
117 #~ _has_array primes => sub { [2,3,5,7,11] };
118 #~ $object->primes->[0]; # 2
119 #~ $object->primes(0); # also 2
120  
121 4 ▶sub _has_array {
Subroutine _has_array: called CC=1 SCAR=0.4
122 4 my ($name, $creator) = @_;
123 4 my $realName = '_'.$name;
124 4 has $realName =>
125 is => 'ro',
126 default => $creator;
127 0 ▶ my $accessor = sub {
Subroutine last_numbered_match_start: not called CC=2 SCAR=17.9
Pod documented
128 0 my $self = shift;
129 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
130 #~ ideally check if @_ contains only numbers
131 #~ Should foo(1,3) return something different?
132 0 return $self->$realName->[@_];
133 }
134 else {
135 0 return $self->$realName;
136 }
137 4 };
138 {
139 4 my $package = __PACKAGE__;
140 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
141 4 my $fullName = $package . '::' . $name;
142 4 *$fullName = subname( $name, $accessor );
143 }
144 }
145  
146 4 ▶sub _has_hash {
Subroutine _has_hash: called CC=1 SCAR=1.3
147 4 my ($name, $creator) = @_;
148 4 my $realName = '_'.$name;
149 4 has $realName =>
150 is => 'ro',
151 default => $creator;
152 0 ▶ my $accessor = sub {
Subroutine named_paren_matches: not called CC=2 SCAR=17.9
Subroutine last_named_paren_match: not called CC=2 SCAR=17.9
Pod undocumented
Pod undocumented
153 0 my $self = shift;
154 0 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 0 0
if (@_) { } 0 0 0
155 0 return $self->$realName->{@_};
156 }
157 else {
158 0 return $self->$realName;
159 }
160 4 };
161 {
162 4 my $package = __PACKAGE__;
163 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
164 4 my $fullName = $package . '::' . $name;
165 4 *$fullName = subname( $name, $accessor );
166 }
167 }
168  
169 =head3 match, prematch, postmatch
170  
171 'The quick brown fox' =~ /q[\w]+/p;
172 my $rr = Regexp::Result->new();
173 print $rr->match; # prints 'quick'
174 print $rr->prematch; # prints 'The '
175 print $rr->postmatch; # prints ' brown fox'
176  
177 When a regexp is executed with the C</p> flag, the variables
178 C<${^MATCH}>, C<${^PREMATCH}>, and C<${^POSTMATCH}> are set.
179 These correspond to the entire text matched by the regular expression,
180 the text in the string which preceded the matched text, and the text in
181 the string which followed it.
182  
183 The C<match> method provides access to the data in C<${^MATCH}>.
184  
185 The C<prematch> method provides access to the data in C<${^PREMATCH}>.
186  
187 The C<postmatch> method provides access to the data in C<${^POSTMATCH}>.
188  
189 Note: no accessor is provided for C<$&>, C<$`>, and C<$'>, because:
190  
191 a) The author feels they are unnecessary since perl 5.10 introduced
192 C<${^MATCH}> etc.
193  
194 b) Implementing accessors for them would force a performance penalty
195 on everyone who uses this module, even if they don't have any need of
196 C<$&>.
197  
198 =cut
199  
200 7 ▶_has_scalar match => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
201 7 ${^MATCH}
202 2 };
203  
204 7 ▶_has_scalar prematch => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
205 7 ${^PREMATCH}
206 2 };
207  
208 7 ▶_has_scalar postmatch => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
209 7 ${^POSTMATCH}
210 2 };
211 =head3 last_paren_match
212  
213 Equivalent to C<$+>.
214  
215 The text matched by the last capturing parentheses of the match.
216 This is useful if you don't know which one of a set of
217 alternative patterns matched. For example, in:
218  
219 /Version: (.*)|Revision: (.*)/
220  
221 C<last_paren_match> stores either the version or revision (whichever
222 exists); perl would number these C<$1> and C<$2>.
223  
224 =cut
225  
226 7 ▶_has_scalar last_paren_match => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
227 7 $+;
228 2 };
229  
230 =head3 last_submatch_result
231  
232 Equivalent to C<$^N>.
233  
234 =cut
235  
236 7 ▶_has_scalar last_submatch_result => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
237 7 $^N;
238 2 };
239  
240 =head3 last_numbered_match_end
241  
242 Equivalent to C<@+>.
243  
244 This array holds the offsets of the ends of the last successful
245 submatches in the currently active dynamic scope. C<$+[0]> is the
246 offset into the string of the end of the entire match. This is the
247 same value as what the C<pos> function returns when called on the
248 variable that was matched against. The nth element of this array
249 holds the offset of the nth submatch, so C<$+[1]> is the offset past
250 where C<$1> ends, C<$+[2]> the offset past where C<$2> ends, and so
251 on.
252  
253 =cut
254  
255 7 ▶_has_array last_numbered_match_end => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
256 7 [@+]
257 2 };
258  
259 =head3 last_numbered_match_start
260  
261 Equivalent to C<@->.
262  
263 This array holds the offsets of the starts of the last successful
264 submatches in the currently active dynamic scope. C<$-[0]> is the
265 offset into the string of the start of the entire match. The nth
266 element of this array holds the offset of the nth submatch, so
267 C<$-[1]> is the offset where C<$1> starts, C<$-[2]> the offset
268 where C<$2> starts, and so on.
269  
270 =cut
271  
272 7 ▶_has_array last_numbered_match_start => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
273 7 [@-]
274 2 };
275 =head3 named_paren_matches
276  
277 'wxyz' =~ /(?<ODD>w)(?<EVEN>x)(?<ODD>y)(?<EVEN>z)/
278  
279 # named_paren_matches is now:
280 #
281 # {
282 # EVEN => [ 'x', 'z' ],
283 # ODD => [ 'w', 'y' ]
284 # }
285  
286 Equivalent to C<%->.
287  
288 This variable allows access to the named capture
289 groups in the last successful match in the currently active
290 dynamic scope. To each capture group name found in the regular
291 expression, it associates a reference to an array containing the
292 list of values captured by all buffers with that name (should
293 there be several of them), in the order where they appear.
294  
295 =cut
296  
297 7 ▶_has_hash named_paren_matches => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
298 7 {%-}
299 2 };
300  
301 =head3 last_named_paren_matches
302  
303 'wxyz' =~ /(?<ODD>w)(?<EVEN>x)(?<ODD>y)(?<EVEN>z)/
304  
305 # last_named_paren_matches is now:
306 #
307 # {
308 # EVEN => 'x',
309 # ODD => 'w',
310 # }
311  
312 The "%+" hash allows access to the named capture
313 buffers, should they exist, in the last successful match in the
314 currently active dynamic scope.
315  
316 The keys of the "%+" hash list only the names of buffers that have
317 captured (and that are thus associated to defined values).
318  
319 Note: C<%-> and C<%+> are tied views into a common internal hash
320 associated with the last successful regular expression. Therefore
321 mixing iterative access to them via C<each> may have unpredictable
322 results. Likewise, if the last successful match changes, then the
323 results may be surprising.
324  
325 Author's note: I have no idea why this is a useful thing to use.
326 But perl provides it, and it is occasionally used according to
327 L<http://grep.cpan.me/> (461 distros, of which some the string
328 C<\%\+|\$\+\{> is in a binary stream).
329  
330 =cut
331  
332 7 ▶_has_hash last_named_paren_match => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
333 7 {%+}
334 2 };
335  
336 =head3 last_regexp_code_result
337  
338 The result of evaluation of the last successful C<(?{ code })>
339 regular expression assertion (see L<perlre>).
340  
341 =cut
342  
343 7 ▶_has_scalar last_regexp_code_result => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
344 7 $^R;
345 2 };
346  
347 =head3 re_debug_flags
348  
349 The current value of the regex debugging flags. Set to 0 for no
350 debug output even when the C<re 'debug'> module is loaded. See
351 L<re> for details.
352  
353 =cut
354  
355 7 ▶_has_scalar re_debug_flags => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
356 7 ${^RE_DEBUG_FLAGS}
357 2 };
358  
359 =head3 pos
360  
361 Returns the end of the match. Equivalent to C<$+[0]>.
362  
363 =cut
364  
365 0 ▶sub pos {
Subroutine pos: not called CC=1 SCAR=6.9
Pod documented
366 0 return shift->last_match_end->[0];
367 }
368  
369 =head1 BUGS
370  
371 Please report any bugs or feature requests to the github issues tracker at L<https://github.com/pdl/Regexp-Result/issues>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
372  
373 =head1 AUTHORS
374  
375 Daniel Perrett
376  
377 =head1 LICENSE AND COPYRIGHT
378  
379 Copyright 2012-2013 Daniel Perrett.
380  
381 This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
382  
383 See L<http://dev.perl.org/licenses/> for more information.
384  
385  
386 =cut
387  
388 2 1;
389  
↑ Summary