In perl.git, the branch blead has been updated <http://perl5.git.perl.org/perl.git/commitdiff/87f718f15c8a57e274b2507ba40e4bd0857529ea?hp=90d1f214e03568148fd6495efac0f5614cfc0323>
- Log ----------------------------------------------------------------- commit 87f718f15c8a57e274b2507ba40e4bd0857529ea Author: Rafael Garcia-Suarez <r...@consttype.org> Date: Tue Sep 7 16:18:14 2010 +0200 More tests for when(slice) M t/op/switch.t commit 329a333e7a4ed898282bec7f485751efbea92e8f Author: David Leadbeater <d...@dgl.cx> Date: Sat Aug 28 22:39:58 2010 +0100 Fix RT #77468: Smart matching on slices 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. M op.c M t/op/smartmatch.t ----------------------------------------------------------------------- Summary of changes: op.c | 12 ++++++++++++ t/op/smartmatch.t | 26 +++++++++++++++++++++++++- t/op/switch.t | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/op.c b/op.c index 5ca1823..f49ce82 100644 --- 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; } diff --git a/t/op/smartmatch.t b/t/op/smartmatch.t index f8a073b..f14e91c 100644 --- a/t/op/smartmatch.t +++ b/t/op/smartmatch.t @@ -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 diff --git a/t/op/switch.t b/t/op/switch.t index 1452b78..fa05918 100644 --- a/t/op/switch.t +++ b/t/op/switch.t @@ -9,7 +9,7 @@ BEGIN { use strict; use warnings; -plan tests => 160; +plan tests => 164; # The behaviour of the feature pragma should be tested by lib/switch.t # using the tests in t/lib/switch/*. This file tests the behaviour of @@ -1162,6 +1162,44 @@ unreified_check(undef,""); @list = $smart_hash->(999); is("@list", '', "rvalue given - list context propagation [999]"); } +{ + # Array slices + my @list = 10 .. 15; + my @in_list; + my @in_slice; + for (5, 10, 15) { + given ($_) { + when (@list) { + push @in_list, $_; + continue; + } + when (@list[0..2]) { + push @in_slice, $_; + } + } + } + is("@in_list", "10 15", "when(array)"); + is("@in_slice", "10", "when(array slice)"); +} +{ + # Hash slices + my %list = map { $_ => $_ } "a" .. "f"; + my @in_list; + my @in_slice; + for ("a", "e", "i") { + given ($_) { + when (%list) { + push @in_list, $_; + continue; + } + when (@list{"a".."c"}) { + push @in_slice, $_; + } + } + } + is("@in_list", "a e", "when(hash)"); + is("@in_slice", "a", "when(hash slice)"); +} # Okay, that'll do for now. The intricacies of the smartmatch # semantics are tested in t/op/smartmatch.t -- Perl5 Master Repository