develooper Front page | perl.perl5.porters | Postings from November 2021

Re: A 'permanent' keyword

Thread Previous | Thread Next
Ovid via perl5-porters
November 10, 2021 20:46
Re: A 'permanent' keyword
Message ID:
> On Wednesday, 10 November 2021, 20:41:57 CET, Oodler 577 via perl5-porters <> wrote:

 > 'state' can be easily replicated with lexical
> closures:
> {
>  my $foo;
>  sub bar {
>   ++$foo;
> 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 development 
Buy my book! -
Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About