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

state variables, first draft

Thread Next
Rafael Garcia-Suarez
April 4, 2006 09:21
state variables, first draft
Message ID:
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'

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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About