> On Wednesday, 10 November 2021, 20:41:57 CET, Oodler 577 via perl5-porters <perl5-porters@perl.org> wrote: > 'state' can be easily replicated with lexical > closures: > > { > my $foo; > sub bar { > ++$foo; > #...do stuff > return $foo; > } > } > > What's the form for 'permanent'? Ironically, depending on the surrounding code, the example you give would be for "permanent" variables, not "state" variables. What we currently use as "state" variables get reset in a dynamic scope, as my original code shows. Your example seems to match the "state" documentation, but doesn't match the state behavioe. "state" could be replaced with lexical closures when done correctly. When done correctly, "permanent" could, also. "When done correctly" is the problem. The docs for "state" *seem* to imply the semantics I would like to see for "permanent", but the behavior is different. Static lexical scope and dynamic lexical scope are different beasts. There are a couple of times I've been bitten by thinking "state" would imply the semantics of what I describe as "permanent". This is usually when I am building code dynamically and I hate building scaffolding to remember that. So ... - "my" reinitializes every lexical scope - "state" reinitializes every dynamic lexical scope - "permanent" never reinitializes (which is what the docs for "state" imply to me) I suspect (but can't prove) that "permanent" probably matches what most developers think of as "state", and those developers who can appreciate "dynamic lexical scope" would hopefully understand the difference. #!/usr/bin/env perl sub foo { # static lexical scope ... return sub { # dynamic lexical scope ... } } Best,Ovid-- IT consulting, training, specializing in Perl, databases, and agile developmenthttp://www.allaroundtheworld.fr/. Buy my book! - http://bit.ly/beginning_perlThread Previous | Thread Next