Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ktat
Created October 11, 2012 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktat/3871366 to your computer and use it in GitHub Desktop.
Save ktat/3871366 to your computer and use it in GitHub Desktop.
diff --git a/lib/Router/Simple/Route.pm b/lib/Router/Simple/Route.pm
index 6c5ba4a..2e664f1 100644
--- a/lib/Router/Simple/Route.pm
+++ b/lib/Router/Simple/Route.pm
@@ -33,7 +33,7 @@ sub new {
}
$row->{pattern} = $pattern;
-
+ my $pattern_re_capture = $pattern;
# compile pattern
my @capture;
$row->{pattern_re} = do {
@@ -64,6 +64,28 @@ sub new {
qr{^$pattern$};
}
};
+ $row->{pattern_re_capture} = do {
+ if (ref $pattern_re_capture) {
+ $row->{_regexp_capture} = 1;
+ $pattern_re_capture;
+ } else {
+ $pattern_re_capture =~ s!
+ \{((?:\{[0-9,]+\}|[^{}]+)+)\} | # /blog/{year:\d{4}}
+ :([A-Za-z0-9_]+) | # /blog/:year
+ (\*) | # /blog/*/*
+ ([^{:*]+) # normal string
+ !
+ if ($1 or $2) {
+ '([^/]+)';
+ } elsif ($3) {
+ "(.+)";
+ } else {
+ quotemeta($4);
+ }
+ !gex;
+ qr{^$pattern_re_capture$};
+ }
+ };
$row->{capture} = \@capture;
$row->{dest} ||= +{};
@@ -83,7 +105,8 @@ sub match {
return undef;
}
}
- if (my @captured = ($env->{PATH_INFO} =~ $self->{pattern_re})) {
+ if ($env->{PATH_INFO} =~ $self->{pattern_re}) {
+ my @captured = ($env->{PATH_INFO} =~ $self->{pattern_re_capture});
my %args;
my @splat;
if ($self->{_regexp_capture}) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment