summaryrefslogtreecommitdiff
path: root/t/op
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2007-09-06 09:18:41 +0000
committerNicholas Clark <nick@ccl4.org>2007-09-06 09:18:41 +0000
commitc5917253cfa0ec36b4c868a1582baaaab99eb0d0 (patch)
tree617731b471122ffb901814d1a2cec921fd95e1a9 /t/op
parent5f0a87760bb9130fbd7579236f81db0729b54496 (diff)
downloadperl-c5917253cfa0ec36b4c868a1582baaaab99eb0d0.tar.gz
Make state $zok = slosh(); behave as the Perl 6 design with an implicit
START block. First time through, call slosh() and assign to $zok. Subsequently neither call slosh() nor assign to $zok. Adds a new op ONCE to control the conditonal call and assign. No change to list context, so state ($zok) = slosh() and (state $zok) = ... etc will still repeatedly evaluate and assign. [Can't fix that before 5.10] Use as an RVALUE is as Larry's design - my $boff = state $zok = ...; will evaluate, assign and return first time, and subsequently act as if it were written my $boff = $zok; FIXME - state $zok = ...; won't deparse - I believe op->op_last isn't being correctly set on the sassign, but I don't know how to fix this. This change may be backed out before 5.10. p4raw-id: //depot/perl@31798
Diffstat (limited to 't/op')
-rw-r--r--t/op/state.t15
1 files changed, 8 insertions, 7 deletions
diff --git a/t/op/state.t b/t/op/state.t
index fb2880d852..f7db80401c 100644
--- a/t/op/state.t
+++ b/t/op/state.t
@@ -10,7 +10,7 @@ BEGIN {
use strict;
use feature "state";
-plan tests => 37;
+plan tests => 38;
ok( ! defined state $uninit, q(state vars are undef by default) );
@@ -18,7 +18,7 @@ ok( ! defined state $uninit, q(state vars are undef by default) );
sub stateful {
state $x;
- state $y //= 1;
+ state $y = 1;
my $z = 2;
state ($t) //= 3;
return ($x++, $y++, $z++, $t++);
@@ -45,9 +45,9 @@ is( $t, 5, 'incremented state var, list syntax' );
# in a nested block
sub nesting {
- state $foo //= 10;
+ state $foo = 10;
my $t;
- { state $bar //= 12; $t = ++$bar }
+ { state $bar = 12; $t = ++$bar }
++$foo;
return ($foo, $t);
}
@@ -83,7 +83,7 @@ is( $f2->(), 2, 'generator 2 once more' );
sub TIESCALAR {bless {}};
sub FETCH { ++$fetchcount; 18 };
tie my $y, "countfetches";
- sub foo { state $x //= $y; $x++ }
+ sub foo { state $x = $y; $x++ }
::is( foo(), 18, "initialisation with tied variable" );
::is( foo(), 19, "increments correctly" );
::is( foo(), 20, "increments correctly, twice" );
@@ -94,7 +94,7 @@ is( $f2->(), 2, 'generator 2 once more' );
sub gen_cashier {
my $amount = shift;
- state $cash_in_store;
+ state $cash_in_store = 0;
return {
add => sub { $cash_in_store += $amount },
del => sub { $cash_in_store -= $amount },
@@ -113,7 +113,7 @@ sub stateless {
++$reinitme;
}
is( stateless(), 43, 'stateless function, first time' );
-is( stateless(), 43, 'stateless function, second time' );
+is( stateless(), 44, 'stateless function, second time' );
# array state vars
@@ -157,3 +157,4 @@ noseworth(2);
sub pugnax { my $x = state $y = 42; $y++; $x; }
is( pugnax(), 42, 'scalar state assignment return value' );
+is( pugnax(), 43, 'scalar state assignment return value' );