in reply to Re^3: PadWalker's closed_over - but for our variables?
in thread PadWalker's closed_over - but for our variables?

LanX: our variables are lexical package variables

haukex: I'm not sure I agree. At least in my mind, package variables are quite different from lexical variables

You are both correct. our vars are lexical vars. Package vars aren't.

You seem to be equating our vars and package vars, but that is incorrect. our vars are lexically scoped aliases to package vars.

Seeking work! You can reach me at ikegami@adaelis.com

Replies are listed 'Best First'.
Re^5: PadWalker's closed_over - but for our variables?
by LanX (Cardinal) on Apr 05, 2021 at 15:37 UTC
    not that easy

    > our vars are lexically scoped aliases to package vars.

    the op-tree shows our vars as GVSV with explicit link to a STASH entry *TST::xxx

    D:\tmp\pm>perl -MO=Concise,TST::foo -e"package TST; our $xxx=666; sub +foo { print $xxx}" TST::foo: 5 <1> leavesub[1 ref] K/REFC,1 ->(end) - <@> lineseq KP ->5 1 <;> nextstate(TST 3 -e:1) v ->2 4 <@> print sK ->5 2 <0> pushmark s ->3 - <1> ex-rv2sv sK/1 ->4 3 <#> gvsv[*TST::xxx] s ->4 -e syntax OK

    while illguts° states

    Lexicals (my and our variables) have SVs_PADMY / SVs_PADOUR

    My experiments show that deleting the stash entry prior to running the code doesn't effect the correct execution. Hence I think that the refs are hardcoded (bound) into the op-tree and STASHs and PADs are primarly for lookup at compilation time. (plus ref-counting)

    My experiments also showed that deleting the alias of an 'our'-var from the STASH forced a destruction of that var at the end of the scope, much like with 'my'-vars.

    So I'm inclined to think that Concise is just showing an interpretation of the refadress in the optree.

    Anyway all this confusion stems from fuzzy wording.

    We should stop referring to private my-vars as "lexicals".

    A distinction between "private" and "public" would be far better.

    "lexical" should be reserved for the scope only and contrasted with "dynamic" ˛

    Consequence: an our-var is a "lexical and public" variable.

    NB: I'm also contrasting "public" from "global", because only special vars like $_, $| or $a are really global, they always belong to main::

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    °) "Illustrated Perl-Guts" ... is it only me, or does "illguts" make you think of diarrhea?

    update

    ˛) in hindsight, lets avoid "lexical scope" and rather use the synonymous "static scope"

      because only special vars like $_, $| or $a are really global

      Nah, all package variables are globally scoped (visible globally).

      The variable you listed simply have a different default namespace if you use them unqualified. That doesn't change their scope.

      Seeking work! You can reach me at ikegami@adaelis.com

        I attempt to stay as close to perlglossary as possible.

        The term "lexical" is messed up tho.

        edit

        https://perldoc.perl.org/perlglossary#global

        In Perl, only certain special variables are truly global—most variables (and all subroutines) exist only in the current package.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery