Fix match parsing with perl 5.8.8
Andrew Rodland [Tue, 3 Apr 2012 17:12:26 +0000 (13:12 -0400)]
Runtime-loading the ParamParser loads Encode, which blows away $_ and
pos. Carefully save these values around the module load so that things
don't blow up.

Changes
lib/Web/Dispatch/Predicates.pm

diff --git a/Changes b/Changes
index 86f7dbc..9341251 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,3 +1,6 @@
+  - Fix a weird interaction between match-spec parsing and module loading
+    that was causing brokenness and test failures with perl 5.8.8
+
 0.012 - 2012-01-30
   - Added match predicates match_true and match_false
   - Added '~' to match an empty path
index 6a489a5..452a5e2 100644 (file)
@@ -125,7 +125,16 @@ sub match_uploads {
 
 sub _param_matcher {
   my ($type, $spec) = @_;
-  require Web::Dispatch::ParamParser;
+  # We're probably parsing a match spec while building the parser, and
+  # on 5.8.8, loading ParamParser loads Encode which blows away $_ and pos.
+  # Furthermore, localizing $_ doesn't restore pos afterwards. So do this
+  # stupid thing instead to work on 5.8.8
+  my $saved_pos = pos;
+  {
+    local $_;
+    require Web::Dispatch::ParamParser;
+  }
+  pos = $saved_pos;
   my $unpack = Web::Dispatch::ParamParser->can("get_unpacked_${type}_from");
   sub {
     _extract_params($unpack->($_[0]), $spec)