Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by LanX (Saint)
on Apr 05, 2021 at 15:37 UTC ( [id://11130846]=note: print w/replies, xml ) Need Help??


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

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"

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

    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

        The term "lexical" is messed up tho.

        No. Maybe imprecise since there two kinds of lexically-scoped variables, but not messed up.

        I attempt to stay as close to perlglossary as possible.

        Don't do that by by reinventing what global means (or basing yourself on a passage that does that). *That* would be messed up.

        $_ and similar have been called superglobals.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-03-28 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found