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

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

Thread Previous | Thread Next
From:
Gerard Goossen
Date:
April 2, 2007 04:46
Subject:
Re: Differences between state variables and smart matches in 5.10 and 6
Message ID:
20070402115013.GJ20231@ostwald
On Mon, Apr 02, 2007 at 11:39:35AM +0200, Rafael Garcia-Suarez wrote:
> On 02/04/07, Gerard Goossen <gerard@tty.nl> wrote:
> >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.
> 
> That's by design. Or else, what should (state $x, my $y) = f() do ?
> 
> >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.
> 
> That would be much simpler, yes. Less surprising, too. I might back
> out all changes to sassign before 5.10. Still wondering.

I would like that.

> >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.
> 
> But that's a different semantics, since you can then undef $x and have
> the assignment occur again.
> 
> What we would like to have here is ONCE blocks, actually. But I don't
> see how to do this without a modifiable optree (even with
> indirections).

Of course, I just ment that using 'state $x //= "foo"' will probably do
what you want, it isn't exactly a ONCE block, but if you don't undef $x
it will behave like one.


Gerard Goossen


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