Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re^2: PadWalker's closed_over - but for our variables? (update: "declaration") by LanX
in thread PadWalker's closed_over - but for our variables? by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-24 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found