[Bps-public-commit] Path-Dispatcher branch, pass-match, updated. 4a660e060ea38509440c6b6cdb55d71e6dd35f74

Shawn Moore sartak at bestpractical.com
Sat Aug 21 21:30:09 EDT 2010


The branch, pass-match has been updated
       via  4a660e060ea38509440c6b6cdb55d71e6dd35f74 (commit)
       via  b6b83b8bcd6473019da61598aea79a15cd9a0dd5 (commit)
      from  78eb561817c71070f283f7b2295f2ab33b35f951 (commit)

Summary of changes:
 lib/Path/Dispatcher/Match.pm      |    6 ++++++
 lib/Path/Dispatcher/Rule/Regex.pm |    9 +++++----
 t/026-named-captures.t            |   23 +++++++++++++++++++++++
 3 files changed, 34 insertions(+), 4 deletions(-)
 create mode 100644 t/026-named-captures.t

- Log -----------------------------------------------------------------
commit b6b83b8bcd6473019da61598aea79a15cd9a0dd5
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Sat Aug 21 21:32:22 2010 -0400

    Add named capture support

diff --git a/lib/Path/Dispatcher/Match.pm b/lib/Path/Dispatcher/Match.pm
index fea532d..49df4cd 100644
--- a/lib/Path/Dispatcher/Match.pm
+++ b/lib/Path/Dispatcher/Match.pm
@@ -27,6 +27,12 @@ has positional_captures => (
     default => sub { [] },
 );
 
+has named_captures => (
+    is      => 'rw',
+    isa     => 'HashRef[Str|Undef]',
+    default => sub { {} },
+);
+
 sub run {
     my $self = shift;
 
diff --git a/lib/Path/Dispatcher/Rule/Regex.pm b/lib/Path/Dispatcher/Rule/Regex.pm
index cc7770c..141df64 100644
--- a/lib/Path/Dispatcher/Rule/Regex.pm
+++ b/lib/Path/Dispatcher/Rule/Regex.pm
@@ -12,12 +12,13 @@ sub _match {
     my $self = shift;
     my $path = shift;
 
-    return unless my @matches = $path->path =~ $self->regex;
+    return unless my @positional = $path->path =~ $self->regex;
+
+    my %named = $] > 5.010 ? eval q{%+} : ();
 
-    # if $' is in the program at all, then it slows down every single regex
-    # we only want to include it if we have to
     return {
-        positional_captures => \@matches,
+        positional_captures => \@positional,
+        named_captures      => \%named,
         ($self->prefix ? (leftover => eval q{$'}) : ()),
     }
 }

commit 4a660e060ea38509440c6b6cdb55d71e6dd35f74
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Sat Aug 21 21:32:29 2010 -0400

    Tests for named captures

diff --git a/t/026-named-captures.t b/t/026-named-captures.t
new file mode 100644
index 0000000..e39b5ed
--- /dev/null
+++ b/t/026-named-captures.t
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 4;
+use Path::Dispatcher;
+
+my $dispatcher = Path::Dispatcher->new(
+    rules => [
+        Path::Dispatcher::Rule::Regex->new(
+            regex => qr/^(\w+) (?<second>\w+) (?<third>\w+)?$/,
+            block => sub { shift },
+        ),
+    ],
+);
+
+my $match = $dispatcher->run("positional named ");
+is_deeply($match->positional_captures, ["positional", "named", undef]);
+is_deeply($match->named_captures, { second => "named" });
+
+$match = $dispatcher->run("positional firstnamed secondnamed");
+is_deeply($match->positional_captures, ["positional", "firstnamed", "secondnamed"]);
+is_deeply($match->named_captures, { second => "firstnamed", third => "secondnamed" });
+

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list