File Coverage

blib/lib/Path/Dispatcher/Rule/Regex.pm
Criterion Covered Total %
statement 23 23 100.0
branch 7 8 87.5
condition n/a
subroutine 5 5 100.0
pod n/a
total 35 36 97.2


line stmt bran cond sub pod time code
1             package Path::Dispatcher::Rule::Regex;
2             # ABSTRACT: predicate is a regular expression
3              
4             our $VERSION = '1.07';
5              
6 31     31   228 use Moo;
  31         70  
  31         429  
7 31     31   9724 use Types::Standard qw(RegexpRef);
  31         74  
  31         219  
8              
9             extends 'Path::Dispatcher::Rule';
10              
11             has regex => (
12             is => 'ro',
13             isa => RegexpRef,
14             required => 1,
15             );
16              
17 31     31   16211 my $named_captures = $] > 5.010 ? eval 'sub { %+ }' : sub { };
  31         14497  
  31         1030  
18              
19             sub _match {
20 58     58   113 my $self = shift;
21 58         106 my $path = shift;
22              
23             # davem++ http://www.nntp.perl.org/group/perl.perl5.porters/2013/03/msg200156.html
24 58 100       209 if ($self->prefix) {
25 5         289 eval q{$'};
26             }
27              
28 58 100       555 return unless my @positional = $path->path =~ $self->regex;
29              
30 48         1192 my %named = $named_captures->();
31              
32 48         110 my %extra;
33              
34             # only provide leftover if we need it. $' is slow, and it may be undef
35 48 100       167 if ($self->prefix) {
36 4         161 $extra{leftover} = eval q{$'};
37 4 50       21 delete $extra{leftover} if !defined($extra{leftover});
38             }
39              
40             return {
41 48         282 positional_captures => \@positional,
42             named_captures => \%named,
43             %extra,
44             }
45             }
46              
47             __PACKAGE__->meta->make_immutable;
48 31     31   21363 no Moo;
  31         76  
  31         152  
49              
50             1;
51              
52             __END__