File Coverage

blib/lib/Path/Dispatcher/Rule/Regex.pm
Criterion Covered Total %
statement 26 26 100.0
branch 7 8 87.5
condition n/a
subroutine 6 6 100.0
pod n/a
total 39 40 97.5


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.08';
5              
6 31     31   226 use Moo;
  31         72  
  31         198  
7 31     31   11023 use MooX::TypeTiny;
  31         79  
  31         176  
8 31     31   19649 use Types::Standard qw(RegexpRef);
  31         79  
  31         248  
9              
10             extends 'Path::Dispatcher::Rule';
11              
12             has regex => (
13             is => 'ro',
14             isa => RegexpRef,
15             required => 1,
16             );
17              
18 31     31   16585 my $named_captures = $] > 5.010 ? eval 'sub { %+ }' : sub { };
  31         14873  
  31         1114  
19              
20             sub _match {
21 58     58   510 my $self = shift;
22 58         109 my $path = shift;
23              
24             # davem++ http://www.nntp.perl.org/group/perl.perl5.porters/2013/03/msg200156.html
25 58 100       513 if ($self->prefix) {
26 5         250 eval q{$'};
27             }
28              
29 58 100       532 return unless my @positional = $path->path =~ $self->regex;
30              
31 48         1170 my %named = $named_captures->();
32              
33 48         100 my %extra;
34              
35             # only provide leftover if we need it. $' is slow, and it may be undef
36 48 100       169 if ($self->prefix) {
37 4         160 $extra{leftover} = eval q{$'};
38 4 50       22 delete $extra{leftover} if !defined($extra{leftover});
39             }
40              
41             return {
42 48         291 positional_captures => \@positional,
43             named_captures => \%named,
44             %extra,
45             }
46             }
47              
48             __PACKAGE__->meta->make_immutable;
49 31     31   22329 no Moo;
  31         77  
  31         144  
50              
51             1;
52              
53             __END__
54              
55             =pod
56              
57             =encoding UTF-8
58              
59             =head1 NAME
60              
61             Path::Dispatcher::Rule::Regex - predicate is a regular expression
62              
63             =head1 VERSION
64              
65             version 1.08
66              
67             =head1 SYNOPSIS
68              
69             my $rule = Path::Dispatcher::Rule::Regex->new(
70             regex => qr{^/comment(s?)/(\d+)$},
71             block => sub { display_comment(shift->pos(2)) },
72             );
73              
74             =head1 DESCRIPTION
75              
76             Rules of this class use a regular expression to match against the path.
77              
78             =head1 ATTRIBUTES
79              
80             =head2 regex
81              
82             The regular expression to match against the path. It works just as you'd expect!
83              
84             The capture variables (C<$1>, C<$2>, etc) will be available in the match
85             object as C<< ->pos(1) >> etc. C<$`>, C<$&>, and C<$'> are not restored.
86              
87             =head1 SUPPORT
88              
89             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Path-Dispatcher>
90             (or L<bug-Path-Dispatcher@rt.cpan.org|mailto:bug-Path-Dispatcher@rt.cpan.org>).
91              
92             =head1 AUTHOR
93              
94             Shawn M Moore, C<< <sartak at bestpractical.com> >>
95              
96             =head1 COPYRIGHT AND LICENSE
97              
98             This software is copyright (c) 2020 by Shawn M Moore.
99              
100             This is free software; you can redistribute it and/or modify it under
101             the same terms as the Perl 5 programming language system itself.
102              
103             =cut