File Coverage

blib/lib/Regex/Object.pm
Criterion Covered Total %
statement 50 51 98.0
branch 2 2 100.0
condition n/a
subroutine 14 14 100.0
pod 3 3 100.0
total 69 70 98.5


line stmt bran cond sub pod time code
1             package Regex::Object;
2              
3 4     4   288410 use 5.20.0;
  4         45  
4              
5 4     4   24 use utf8;
  4         7  
  4         30  
6 4     4   2229 use English;
  4         15886  
  4         24  
7 4     4   1727 use feature qw(signatures);
  4         9  
  4         638  
8              
9 4     4   1916 use Regex::Object::Match;
  4         14  
  4         181  
10 4     4   1994 use Regex::Object::Matches;
  4         50  
  4         154  
11 4     4   29 use Moo;
  4         8  
  4         17  
12              
13 4     4   1534 no warnings qw(experimental::signatures);
  4         9  
  4         179  
14 4     4   24 use namespace::clean;
  4         7  
  4         30  
15              
16             our $VERSION = '1.23';
17              
18             tie my %nc, "Tie::Hash::NamedCapture";
19             tie my %nca, "Tie::Hash::NamedCapture", all => 1;
20              
21             has regex => (
22             is => 'ro',
23             );
24              
25 11     11 1 6797 sub match($self, $string) {
  11         21  
  11         19  
  11         14  
26 11         77 $string =~ $self->regex;
27 11         29 return $self->collect;
28             }
29              
30 5     5 1 1732 sub match_all($self, $string) {
  5         10  
  5         7  
  5         8  
31 5         12 my $regex = $self->regex;
32 5         7 my @matches;
33              
34 5         47 while($string =~ /$regex/g) {
35 11         30 push @matches, $self->collect;
36             }
37              
38 5         81 return Regex::Object::Matches->new(
39             collection => \@matches,
40             );
41             }
42              
43             sub collect {
44 25     25 1 363 return Regex::Object::Match->new(
45             prematch => $PREMATCH,
46             match => $MATCH,
47             postmatch => $POSTMATCH,
48             last_paren_match => $LAST_PAREN_MATCH,
49             captures => _collect_captures(),
50             named_captures => { %nc },
51             named_captures_all => { %nca },
52             );
53             }
54              
55             sub _collect_captures {
56 25     25   35 my @captures;
57              
58             # Trick to get @captures the most appropriate to language version way.
59             eval {
60 25         123 @captures = @{^CAPTURE}; # This was added in 5.25.7.
61             }
62 25 100       40 or do {
63 4     4   2748 no strict 'refs';
  4         8  
  4         555  
64 8         24 @captures = map { "$$_" } 1 .. $#-;
  0         0  
65             };
66              
67 25         629 return \@captures;
68             }
69              
70             1;
71              
72             __END__