develooper Front page | perl.perl5.porters | Postings from April 2003

Re: [perl #21765] $s = ""; $n = @a = split(/,/, $s); results in undef $n

Thread Previous | Thread Next
From:
sthoenna
Date:
April 1, 2003 17:14
Subject:
Re: [perl #21765] $s = ""; $n = @a = split(/,/, $s); results in undef $n
Message ID:
u+ii+gzkgqkV092yn@efn.org
On 31 Mar 2003 15:39:40 -0000, wab@agere.com wrote:
>The following code:
>   $s = "";
>   $n = @a = split(/,/, $s);
>   print "a=@a n=$n\n";
>
>produces:
>   a= n=
>
>But I expect n=0.
>
>I get the expected result if I change the code from
>   $n = @a = split(/,/, $s);
>to
>   @a = split(/,/, $s);
>   $n = @a;
>
>I also get the expected result if I predefine @a as shown below:
>   my @a;
>   $n = @a = split(/,/, $s);

This is triggered by a weird optimization to @global=split...
that performs the assignment into the global during the split instead
of as a separate operation.  Sometime later, I'd like to see this
optimization extended to work on lexicals also, since it seems of
little worth as is (given the sheer amount of extra code it requires
in pp_split, newASSIGNOP, and elsewhere).  This could possibly be done
by setting OPf_STACKED and passing split the av as a separate
parameter.

The code to return undef in this case looks pretty intentional, but
since that contradicts both the documentation and common sense, I
think it should be changed to return 0.

Patch is to maint.  Added eval{} to former last test since that seems
to be what it expected and I wanted to be able to run the tests to
completion using a 5.8.0 perl.

--- perl-5.8/t/op/split.t.orig	Tue Feb 18 07:57:52 2003
+++ perl-5.8/t/op/split.t	Tue Apr  1 16:24:34 2003
@@ -6,7 +6,7 @@
     require './test.pl';
 }
 
-plan tests => 50;
+plan tests => 52;
 
 $FS = ':';
 
@@ -280,6 +280,13 @@
 {
     $p="a,b";
     utf8::upgrade $p;
-    @a=split(/[, ]+/,$p);
+    eval { @a=split(/[, ]+/,$p) };
     is ("$@-@a-", '-a b-', '#20912 - split() to array with /[]+/ and utf8');
+}
+
+{
+    is (\@a, \@{"a"}, '@a must be global for following test');
+    $p="";
+    $n = @a = split /,/,$p;
+    is ($n, 0, '#21765 - pmreplroot hack used to return undef for 0 iters');
 }
--- perl-5.8/pp.c.orig	Wed Mar 19 08:52:36 2003
+++ perl-5.8/pp.c	Tue Apr  1 14:44:14 2003
@@ -4785,12 +4785,10 @@
 	if (gimme == G_ARRAY)
 	    RETURN;
     }
-    if (iters || !pm->op_pmreplroot) {
-	GETTARGET;
-	PUSHi(iters);
-	RETURN;
-    }
-    RETPUSHUNDEF;
+
+    GETTARGET;
+    PUSHi(iters);
+    RETURN;
 }
 
 #ifdef USE_5005THREADS
End of Patch.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About