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

$a++

Thread Previous | Thread Next
From:
hv
Date:
March 15, 2003 19:32
Subject:
$a++
Message ID:
200303160334.h2G3YRP09535@crypt.compulink.co.uk
This is strange:
  crypt% perl -le 'print $a++'
  0
  crypt% 

It happens because pp.c:pp_postinc has these lines:
    if (!SvOK(TARG))
        sv_setiv(TARG, 0);
which aren't duplicated in pp_postdec.

Anyone got a clue why those lines are there? They appear to have
been there forever - at least back to around 5.003, when pp.c was
first checked into perforce.

Removing those two lines causes me one test failure, in
lib/AutoSplit.t, in which a subroutine set up as:
  sub obsolete {my $a if 0; return $a++;}
is expected to return "0" the first time it is called.

I think it is correct for:
  sub { $a = undef; return $a++ }
to return undef. So I think those two lines should be removed, and
the AutoSplit test corrected, as in the patch below. Any demurral?

Hugo
--- pp.c.old	Sun Mar 16 03:33:09 2003
+++ pp.c	Sun Mar 16 03:33:19 2003
@@ -857,8 +857,6 @@
     else
 	sv_inc(TOPs);
     SvSETMAGIC(TOPs);
-    if (!SvOK(TARG))
-	sv_setiv(TARG, 0);
     SETs(TARG);
     return NORMAL;
 }
--- lib/AutoSplit.t.old	Sun Mar 16 03:32:50 2003
+++ lib/AutoSplit.t	Sun Mar 16 03:33:01 2003
@@ -299,7 +299,7 @@
 use AutoLoader 'AUTOLOAD';
 1;
 __END__
-sub obsolete {my $a if 0; return $a++;}
+sub obsolete {my $a if 0; return ++$a;}
 sub gonner {warn "This gonner function should never get called"}
 ## Get
 AutoSplitting *INC**PATHSEP**MOD*.pm (*DIR**PATHSEP**MOD**ENDPATHSEP*)
@@ -310,8 +310,8 @@
 *DIR*/*MOD*/gonner.al
 *DIR*/*MOD*/obsolete.al
 ## Tests
-is (&*MOD*::obsolete, 0);
 is (&*MOD*::obsolete, 1);
+is (&*MOD*::obsolete, 2);
 ## Sleep
 4
 ## SameAgain

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