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:
Abigail
Date:
March 26, 2007 08:36
Subject:
Re: Differences between state variables and smart matches in 5.10 and 6
Message ID:
20070326153608.GA3970@abigail.nl
On Mon, Mar 26, 2007 at 12:30:40PM +0200, Rafael Garcia-Suarez wrote:
> On 26/03/07, Nicholas Clark <nick@ccl4.org> wrote:
> >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.
> 
> That's an interesting idea, although it needs a new op "state check"
> and heavy manipulation of the optree in the ck_ routines.
> 
> But looking at the ops at this way, I just found a new bug :
> 
> $ bleadperl -E 'sub f{say state$x=42;++$x;say $x}f;f'
> 42
> 43
> 42
> 44
> 
> The return value of a state assignment is the right hand side. (but
> the actual assignment happens only once, correctly)


It looks like the return value is the right hand side only if there's no
assignment done - the first time it returns an lvalue:

  $ curperl -E 'sub f {(state $x = 42)++; say $x} f; f;'
  43
  Modification of a read-only value attempted at -e line 1.


Abigail

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