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

Re: Differences between state variables and smart matches in 5.10 and 6

Thread Previous | Thread Next
From:
Nicholas Clark
Date:
March 26, 2007 02:57
Subject:
Re: Differences between state variables and smart matches in 5.10 and 6
Message ID:
20070326095744.GS5748@plum.flirble.org
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


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