Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: PadWalker's closed_over - but for our variables?

by haukex (Archbishop)
on Apr 03, 2021 at 16:42 UTC ( [id://11130779]=note: print w/replies, xml ) Need Help??


in reply to Re^2: 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)

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.

Replies are listed 'Best First'.
Re^4: PadWalker's closed_over - but for our variables?
by ikegami (Patriarch) on Apr 04, 2021 at 05:38 UTC

    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"

        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

Re^4: PadWalker's closed_over - but for our variables?
by LanX (Saint) on Apr 03, 2021 at 16:55 UTC
    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).

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

        OK Wolowitz, but we can agree that wording matters in communication? ;-)

        > B::Xref seems like a decent starting point

        unfortunately it's buggy, I'd rather try to reverse "engineer" B::Deparse.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130779]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-03-28 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found