This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix RT #77468: Smart matching on slices
authorDavid Leadbeater <dgl@dgl.cx>
Sat, 28 Aug 2010 21:39:58 +0000 (22:39 +0100)
committerRafael Garcia-Suarez <rgs@consttype.org>
Tue, 7 Sep 2010 14:02:59 +0000 (16:02 +0200)
ref_array_or_hash did not take aslice or hslice OPs into account; wrap
them in an anonlist so that smart matching has a reference as it
expects.

op.c
t/op/smartmatch.t

diff --git a/op.c b/op.c
index 5ca1823..f49ce82 100644 (file)
--- a/op.c
+++ b/op.c
@@ -5542,6 +5542,18 @@ S_ref_array_or_hash(pTHX_ OP *cond)
        return newUNOP(OP_REFGEN,
            0, mod(cond, OP_REFGEN));
 
+    else if(cond
+    && (cond->op_type == OP_ASLICE
+    ||  cond->op_type == OP_HSLICE)) {
+
+       /* anonlist now needs a list from this op, was previously used in
+        * scalar context */
+       cond->op_flags |= ~(OPf_WANT_SCALAR | OPf_REF);
+       cond->op_flags |= OPf_WANT_LIST;
+
+       return newANONLIST(mod(cond, OP_ANONLIST));
+    }
+
     else
        return cond;
 }
index f8a073b..f14e91c 100644 (file)
@@ -73,7 +73,7 @@ my %keyandmore = map { $_ => 0 } @keyandmore;
 my %fooormore = map { $_ => 0 } @fooormore;
 
 # Load and run the tests
-plan tests => 335;
+plan tests => 351;
 
 while (<DATA>) {
   SKIP: {
@@ -484,6 +484,30 @@ __DATA__
        @nums           {  1, '', 12, '' }
 !      @nums           { 11, '', 12, '' }
 
+# array slices
+       @nums[0..-1]    []
+       @nums[0..0]     [1]
+!      @nums[0..1]     [0..2]
+       @nums[0..4]     [1..5]
+
+!      undef           @nums[0..-1]
+       1               @nums[0..0]
+       2               @nums[0..1]
+!      @nums[0..1]     2
+
+       @nums[0..1]     @nums[0..1]
+
+# hash slices
+       @keyandmore{qw(not)}            [undef]
+       @keyandmore{qw(key)}            [0]
+
+       undef                           @keyandmore{qw(not)}
+       0                               @keyandmore{qw(key and more)}
+!      2                               @keyandmore{qw(key and)}
+
+       @fooormore{qw(foo)}             @keyandmore{qw(key)}
+       @fooormore{qw(foo or more)}     @keyandmore{qw(key and more)}
+
 # UNDEF
 !      3               undef
 !      1               undef