I find the whole 'state $x' effecting the rhs is very tricky.
And having this magic in sassign seems wrong. It means that
'(state $x) = "foo"' assigns "foo" to $x every time.
And also 'state $x ||= "foo"' works every time.
If the magic remains I think it should be done by
'state $x' returning an LVALUE with "get" and "set" magic.
But I think it is much simpler that 'state $x' always returns the
state variable $x, that is just a declaration.
If you want to initialize it only once you can do 'state $x //= "foo"'
It is immediatly clear that $x will keeps it old value, and "foo" is
only evaluated if $x is uninitialized.
And '=' does what I expect it to do, assign the rhs to the lhs, no
exceptions.
Gerard Goossen
On Mon, Mar 26, 2007 at 10:57:44AM +0100, Nicholas Clark wrote:
> On Mon, Mar 26, 2007 at 10:16:41AM +0200, Rafael Garcia-Suarez wrote:
>
> > Yes, that's how Perl 6 specifies it. However I think that would be
> > much more difficult to implement in Perl 5.
> > Here's the opcode for Perl 5:
> >
> > $ bleadperl -MO=Concise -E 'state $x = foo()'
> > 8 <@> leave[1 ref] vKP/REFC ->(end)
> > 1 <0> enter ->2
> > 2 <;> nextstate(main 40 -e:1) v:%,{ ->3
> > 7 <2> sassign vKS/STATE,2 ->8
> > 5 <1> entersub[t2] sKS/TARG,1 ->6
> > - <1> ex-list sK ->5
> > 3 <0> pushmark s ->4
> > - <1> ex-rv2cv sK ->-
> > 4 <$> gv(*foo) s/EARLYCV ->5
> > 6 <0> padsv[$x:40,41] sRM*/LVINTRO,STATE ->7
> >
> > state() is implemented by a combination of flags, one on the
> > assignment op that indicates that this is an assignment to a state
> > variable, and one on the pad variable that indicates that it has been
> > initialized. If you want to discard the whole right side of the
> > assignment, effectively implementing Perl 6's "once" blocks, you'd
> > have to modify the optree at runtime. And that's something we can't
> > do, since optrees are shared between threads.
>
> Assuming the same opcode layout.
>
> Currently the execution order for state $a = $b is
>
> $b
> $a
> =
>
> isn't it?
>
> If, instead, assignments where the entire LHS is state variables compiled to
>
> state check on $a:
> $b
> $a
> =
>
> then the RHS would only be evaluated if $a needed assigning to, and also the
> assign op wouldn't need the flags any more.
>
>
> Nicholas Clark
Thread Previous
|
Thread Next