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

Re: A 'permanent' keyword

Thread Previous | Thread Next
From:
Ovid via perl5-porters
Date:
November 10, 2021 21:12
Subject:
Re: A 'permanent' keyword
Message ID:
1474032450.626378.1636578723152@mail.yahoo.com
Resending this as plain text because I now see it's getting mangled in the archive. I'll be a better citizen in the future.

I would love to see a permanent keyword for Perl. It initializes a variable once and only once. It would look like this:

    #!/usr/bin/env perl

    use 5.38.0;
    use warnings;

    sub variables {
        my $name = shift;
        return sub {
            my $my = 1;
            state $state = 1;
            permanent $permanent = 1;
            say "$name: my is $my. state is $state. permanent is $permanent";
            $_++ for $my, $state, $permanent;
        }
    }

    my $first  = variables('first');
    my $second = variables('name');

    say "First";
    $first->();
    $first->();
    $first->();

    say "Second";
    $second->();
    $second->();
    $second->();

And the output would be:

    First
    first: my is 1. state is 1. permanent is 1
    first: my is 1. state is 2. permanent is 2
    first: my is 1. state is 3. permanent is 3
    Second
    name: my is 1. state is 1. permanent is 4
    name: my is 1. state is 2. permanent is 5
    name: my is 1. state is 3. permanent is 6


Currently, state's documentation says this:

    "state" declares a lexically scoped variable,
    just like "my".  However, those variables will 
    never be reinitialized, contrary to lexical
    variables that are reinitialized each time their
    enclosing block is entered.  See "Persistent
    Private Variables" in perlsub for details.

But that's not quite correct. It's never reinitialized unless the scope is dynamic, in which case it is.

Permanent variables would never be reinitialized. Not only will this be more clear to existing Perl developers, it might help new Perl developers who misuse state variables. It might also clear up future issues with Corinna, but I won't go there now :)

Best,
Ovid
-- 
IT consulting, training, specializing in Perl, databases, and agile development
http://www.allaroundtheworld.fr/. 

Buy my book! - http://bit.ly/beginning_perl






On Wednesday, 10 November 2021, 22:08:10 CET, Ovid via perl5-porters <perl5-porters@perl.org> wrote: 





> On Wednesday, 10 November 2021, 21:57:56 CET, Dan Book <grinnz@gmail.com> wrote:


> Thanks for explaining, this is new terminology for me - unrelated to the concept of dynamic scope used in the > documentation (runtime scoping).
>
> Perhaps an addition to the documentation could be made, but this seems like expected behavior to me.

Hmm, I didn't know the term "runtime scoping" was used. I just pulled the latest Perl from github and did a `git grep -i 'runtime scoping' and it returned no results.

I made up the term "dynamic scoping" because I didn't know there was a term for it. I don't see anything in in the pod/ directory that matches this, either.

I definitely think there's room for improvement in the docs, but I'm loathe to submit a patch to further confuse the documentation if there's already something there that I missed.

Best,
Ovid




Thread Previous | 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