develooper Front page | perl.perl5.porters | Postings from July 2012

Re: [perl #113974] package NAMESPACE manpage comments

Thread Previous | Thread Next
From:
demerphq
Date:
July 6, 2012 07:06
Subject:
Re: [perl #113974] package NAMESPACE manpage comments
Message ID:
CANgJU+VWw9PnpvQkx8W-Faf33vUMgd43-fbeNc-dZkxfJA5_tw@mail.gmail.com
On 6 July 2012 15:20, David Golden <xdaveg@gmail.com> wrote:
> On Fri, Jul 6, 2012 at 7:34 AM, demerphq <demerphq@gmail.com> wrote:
>> A big chunk of the confusion about our and my comes from the fact that
>> our is not a declaration like my is. It doesnt create or reserve
>> storage. It is a compile hint. my on the other hand is a true
>> declaration. It actually creates stuff.
>
> FWIW, I think that's just as confusing.  'our' declares an identifier,
> just not one that allocates storage, because package variables never
> have storage allocated by declaration, only on demand.

Ok, well, I just checked some things and I concede that I am misusing
the term "declaration". I should have said "definition".

For instance see:

http://en.wikipedia.org/wiki/External_variable#Definition.2C_declaration_and_the_extern_keyword

<quote>
To understand how external variables relate to the extern keyword, it
is necessary to know the difference between defining and declaring a
variable. When a variable is defined, the compiler allocates memory
for that variable and possibly also initializes its contents to some
value. When a variable is declared, the compiler requires that the
variable be defined elsewhere. The declaration informs the compiler
that a variable by that name and type exists, but the compiler need
not allocate memory for it since it is allocated elsewhere.
</quote>

So to rephrase using that documents definition of "declaration" and
"definition":

<quote>
A big chunk of the confusion about our and my comes from the fact that
our is not a variable definition like my is. It doesn't create or reserve
storage. It is actually a declaration that acts as a compiler hint. my
on the other
hand is a true variable definition. It actually creates stuff.
</quote>

The point here is that dynamic variables are in fact hash/stash
lookups with a special notation. They do not create anything, they
merely access the stash. Which autovivifies on use like any other
hash. (And in fact scalar's are "special" as they always exist for any
identifier in that namespace whether or not the scalar is ever used).

A my variable is not accessable from the stash and will not be
autovivified into existence and thus requires storage allocation at
compile time.

cheers,
Yves



-- 
perl -Mre=debug -e "/just|another|perl|hacker/"

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About