better range-checking on list slices, with test
authorHugo van der Sanden <hv@crypt.org>
Sun, 2 May 1999 17:02:53 +0000 (18:02 +0100)
committerGurusamy Sarathy <gsar@cpan.org>
Wed, 12 May 1999 11:26:03 +0000 (11:26 +0000)
Message-Id: <199905021602.RAA13905@crypt.compulink.co.uk>
Subject: Re: List slice of undefs returns 0 items

p4raw-id: //depot/perl@3406

pp.c
t/op/list.t

diff --git a/pp.c b/pp.c
index 431dc9a..e76266e 100644 (file)
--- a/pp.c
+++ b/pp.c
@@ -2827,20 +2827,17 @@ PP(pp_lslice)
 
     for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
        ix = SvIVx(*lelem);
-       if (ix < 0) {
+       if (ix < 0)
            ix += max;
-           if (ix < 0)
-               *lelem = &PL_sv_undef;
-           else if (!(*lelem = firstrelem[ix]))
-               *lelem = &PL_sv_undef;
-       }
-       else {
+       else 
            ix -= arybase;
-           if (ix >= max || !(*lelem = firstrelem[ix]))
+       if (ix < 0 || ix >= max)
+           *lelem = &PL_sv_undef;
+       else {
+           is_something_there = TRUE;
+           if (!(*lelem = firstrelem[ix]))
                *lelem = &PL_sv_undef;
        }
-       if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
-           is_something_there = TRUE;
     }
     if (is_something_there)
        SP = lastlelem;
index a4230b6..4d7a2d5 100755 (executable)
@@ -1,8 +1,6 @@
 #!./perl
 
-# $RCSfile: list.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:02 $
-
-print "1..27\n";
+print "1..28\n";
 
 @foo = (1, 2, 3, 4);
 if ($foo[0] == 1 && $foo[3] == 4) {print "ok 1\n";} else {print "not ok 1\n";}
@@ -81,3 +79,11 @@ for ($x = 0; $x < 3; $x++) {
     print $a,$b,$c;
 }
 
+# slices
+{
+    my @a = (0, undef, undef, 3);
+    my @b = @a[1,2];
+    my @c = (0, undef, undef, 3)[1, 2];
+    print "not " unless @b == @c and @c == 2;
+    print "ok 28\n";
+}