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

our variables are lexical package variables.

"Lexical" describes the scope not how they are stored (private vs package)

The docs are confused here.

> can't be accessed from the caller,

one still needs to know the package of an our-variable to access it

> Or do you just want to get a list of all the variables that a coderef accesses?

I need to introspect a coderef for the package vars used inside, you are right that it might not matter if they were declared outside with our.

As already mentioned: PadWalker offers peek_our but only usable from the inside.

edit

so I effectively need peek_sub for package vars

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

Replies are listed 'Best First'.
Re^3: PadWalker's closed_over - but for our variables?
by haukex (Archbishop) on Apr 03, 2021 at 16:42 UTC
    our variables are lexical package variables. "Lexical" describes the scope not how they are stored (private vs package)

    I'm not sure I agree. At least in my mind, package variables are quite different from lexical variables: package variables are stored in the symbol table (and they can be inspected and manipulated there) and lexical variables in the pads (which aren't so easy to inspect, hence PadWalker). All that our does is create an alias to a package variable, that that alias happens to be lexical in scope is the only "lexical" thing about it, I think. Anyway:

    I need to introspect a coderef for the package vars used inside, you are right that it might not matter if they were declared outside with our.

    At the moment I'm not aware of an easy solution. Any general-purpose solution would fail in the presence of no strict, since I could write no strict; ${"${x}::${y}"} = "hello"; or even ${"a".int(rand(999))} and you'd never know which package variable I just accesed. Perhaps something PPI-based...? I don't have any other good ideas at the moment, perhaps inspiration will strike later.

      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

        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"

      Hi thanks,

      I don't care about 'no strict'.

      Solutions are available by parsing the optree like demonstrated in B::Xref B::Concise and B::Deparse

      But patching them means reinventing the wheel, which I tried to avoid.

      > I'm not sure I agree

      I had the discussion here not long ago. The perldocs are contradictory since our was introduced.

      Lexical is a term borrowed from lisp for variables which have no dynamic scope, but only in the readable block.(lexical ~ like written)

      You know I'm picky about terminology, calling my "private" and package vars "public" would have been better.

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

      See also Re^3: Unusual variable declaration ff

        You know I'm picky about terminology, calling my "private" and package vars "public" would have been better.

        I'm not an expert on the theory and terminology, I'm an engineer and I care more about how things work ;-)

        Solutions are available by parsing the optree like demonstrated in B::Xref B::Concise and B::Deparse

        Yes, I was thinking about these too, it's too bad none of them provide exactly what you want already. B::Xref seems like a decent starting point, unless of course someone has done this already (I don't know yet).