File Coverage

blib/lib/Regex/Object.pm
Criterion Covered Total %
statement 58 59 98.3
branch 6 6 100.0
condition n/a
subroutine 14 14 100.0
pod 3 3 100.0
total 81 82 98.7


line stmt bran cond sub pod time code
1             package Regex::Object;
2              
3 4     4   219035 use 5.20.0;
  4         36  
4              
5 4     4   18 use utf8;
  4         5  
  4         23  
6 4     4   1873 use English;
  4         12819  
  4         21  
7 4     4   1553 use feature qw(signatures);
  4         7  
  4         565  
8              
9 4     4   1574 use Regex::Object::Match;
  4         12  
  4         252  
10 4     4   1881 use Regex::Object::Matches;
  4         33  
  4         143  
11 4     4   25 use Moo;
  4         6  
  4         39  
12              
13 4     4   1316 no warnings qw(experimental::signatures);
  4         9  
  4         174  
14 4     4   21 use namespace::clean;
  4         5  
  4         17  
15              
16             our $VERSION = '1.25';
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 12     12 1 10876 sub match($self, $string) {
  12         21  
  12         16  
  12         15  
26 12         74 my $success = $string =~ $self->regex;
27              
28 12         31 return $self->collect($success);
29             }
30              
31 5     5 1 1456 sub match_all($self, $string) {
  5         8  
  5         6  
  5         6  
32 5         11 my $regex = $self->regex;
33 5         6 my $success;
34             my @matches;
35              
36 5         30 while($success = $string =~ /$regex/g) {
37 11         207 push @matches, $self->collect($success);
38             }
39              
40 5         179 return Regex::Object::Matches->new(
41             collection => \@matches,
42             );
43             }
44              
45 26     26 1 358 sub collect($, $success = undef) {
  26         35  
  26         25  
46 26         29 my $match;
47              
48 26 100       47 if (defined $success) {
49             # Because of quirk with built-in $MATCH - in has last success match.
50 23 100       51 $match = $success ? $MATCH : undef;
51             } else {
52             # Okay, you have not passed success, then we have no choice and have to rely on $MATCH.
53 3         7 $success = defined $MATCH;
54 3         4 $match = $MATCH;
55             }
56              
57 26         53 return Regex::Object::Match->new(
58             success => $success,
59             prematch => $PREMATCH,
60             match => $match,
61             postmatch => $POSTMATCH,
62             last_paren_match => $LAST_PAREN_MATCH,
63             captures => _collect_captures(),
64             named_captures => { %nc },
65             named_captures_all => { %nca },
66             );
67             }
68              
69             sub _collect_captures {
70 26     26   38 my @captures;
71              
72             # Trick to get @captures the most appropriate to language version way.
73             eval {
74 26         97 @captures = @{^CAPTURE}; # This was added in 5.25.7.
75             }
76 26 100       34 or do {
77 4     4   2485 no strict 'refs';
  4         8  
  4         542  
78 9         39 @captures = map { "$$_" } 1 .. $#-;
  0         0  
79             };
80              
81 26         532 return \@captures;
82             }
83              
84             1;
85              
86             __END__