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

state variables, first draft

Thread Next
From:
Rafael Garcia-Suarez
Date:
April 4, 2006 09:21
Subject:
state variables, first draft
Message ID:
20060404182101.3381f23f@grubert.mandriva.com
Here's a first draft of a patch implementing state variables.
That's supposed to replace the old "my $x if 0" trick, and
also allowing to set an initial value. Thus :

$ bleadperl -le 'sub f { state $x = 10; print $x++ } f; f; f'
10
11
12

The patch adds a new private flag OPpASSIGN_STATE for when
one initializes a state variable, a new OPpPAD_STATE for
PADSV ops that correspond to state variables, and a new
SVpad_STATE flag to mark pad entries that are "stateful".

notes :
* only works with scalars for now
* I abuse PADTMP to mark uninitialized state variables,
  so :
    $ bleadperl -e 'state $x'
    Attempt to free temp prematurely: SV 0x826a510.
  another flag would be welcome, but I don't know which.

However, I thought I'd rather send it now, so I can have
early feedback :) esp. from the pad gurus in the back.
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