Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: PadWalker's closed_over - but for our variables? (update: "declaration")

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


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

Our does an implicit declaration initialization of the package variable if it is new.

If you disagree, you should define what you mean with declaration in Perl.

update

"Declaration" is the act of defining type and attributes of a symbol for the compiler of a language.

That's especially important for languages where all symbols are barewords but Perl has sigils and variables are dynamically typed.

A "fully qualified name" in Perl like $pck::scl doesn't need to be declared for the compiler, because everything is known at compile-time. It's only initialized at first encounter into the STASH (symbol table) with a value (= undef by default)

So talking about the "declaration of a package var" is highly confusing.

BUT if you write our $var you are declaring for the compiler how to treat the symbol $var

To cite the docs:

An our declaration declares an alias for a package variable that will be visible across its entire lexical scope

Like with my this declaration is recorded in a hash like structure called a Pad which is an attribute of the scope (= surrounding "block" resp. function).

The big difference to my is that the our var is NOT destroyed (the ref is freed and the data erased from memory) at the end of the scope, because it is still referenced in the STASH of it's namespace.

I.e. next time you'll encounter an our $var in the same package it'll continue pointing to the same ref.

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? (update: "declaration")
by xiaoyafeng (Deacon) on Apr 05, 2021 at 13:17 UTC

    I totally agree what you point out on the update. ;) what I want to empasize is the declaration of our acatually means declaring a alias to a a pkg variable, not declare a package variable itself.





    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

      > declare a package variable itself.

      please explain what that is supposed to mean.

      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://11130835]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 13:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found