I don't get it. What's wrong with state?
Using normal closures, I can do:
{ my $closure; sub x1{ $closure = shift if @_; $closure } sub x2{ $closure = shift if @_; $closure } };; print x1( 123 ); print x2(); print x2(456); print x1();; 123 123 456 456
Or I can do:
{ local our $closure; sub x1{ $closure = shift if @_; $closure } sub x2{ $closure = shift if @_; $closure } };; print x1( 123 ); print x2(); print x2(456); print x1();; 123 123 456 456
Or:
sub x1{ our $closure = shift if @_; $closure } sub x2{ our $closure = +shift if @_; $closure };; print x1( 123 ); print x2(); print x2(456); print x1();; 123 123 456 456
Try that with state:
sub x1{ state $closure = shift if @_; $closure } sub x2{ state $closure = shift if @_; $closure };; print x1( 123 ); print x2(); print x2(456); print x1();; 123 Use of uninitialized value in print at (eval 22) line 1, <STDIN> line +14. 456 123
Or:
sub x1{ state our $closure = shift if @_; $closure } sub x2{ state our + $closure = shift if @_; $closure };; No such class our at (eval 23) line 1, near "{ state our" No such class our at (eval 23) line 1, near "{ state our"
So, three different ways to do something that works; and a new mechanism specifically designed to replace them that doesn't.
In reply to Re^3: Experimental features: autoderef vs postfix deref
by BrowserUk
in thread Experimental features: autoderef vs postfix deref
by stevieb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |