in reply to Re: Re: what's $p'a=$a mean?
in thread what's $p'a=$a mean?

> So, I can use $p::var as some arbitrary global variable even under "use strict" and even when "p" is not referring to any package at all?

Well, first, $p::var isn't a global variable; it's a package variable. And p:: does refer to a package; it specifically refers to the package p. So I would reformat your question as follows:

So, I can use $p::var as some arbitrary package variable even under "use strict" and even when "p" doesn't refer to a package that I've specifically declared (i.e., with the "package" keyword)?

And the answer is, yes. I suppose you could consider that a deficiency of "use strict".

> Is it an advisable thing to do?

Well, personally I would say not. It's bound to confuse someone somewhere. And technically it's allowing you to autovivify a variable, which is what you're trying to avoid by use'ing strict. So I'd avoid it.

Replies are listed 'Best First'.
Re: Re: Re: Re: what's $p'a=$a mean?
by demerphq (Chancellor) on Jun 14, 2003 at 22:41 UTC

    Well, first, $p::var isn't a global variable; it's a package variable.

    Since all package variables are global it is not uncommon to refer to them as global variables.

    So, I can use $p::var as some arbitrary package variable even under "use strict" and even when "p" doesn't refer to a package that I've specifically declared (i.e., with the "package" keyword)?

    And the answer is, yes. I suppose you could consider that a deficiency of "use strict".

    The problem here is that the package keyword is not declarative at all (nor does it define the beginning or ending of a lexical scope). It is more "informative" than anything else. All it says is that within the lexical scope involved unqualified identifiers are to be considered to be in a particular package namespace. In fact there is no means of declaring a package or global variable in perl (before you disagree consider that our and use vars are no more declarative than a package statement). Interestingly there are a few variables that can _only_ be reached through a fully qualified package name.

    Also, I dont really think that you can consider this a deficiency of "use strict", if anything its a feature. After all, there are occasions where you may want to reach into a package that doesnt really exist yet... Hopefully they dont occur often, but when you need to do something like this it sure is nice that you can. IMO the benefits outweigh the hassle of tracking down typos in fully qualified names.

    Well, personally I would say not. It's bound to confuse someone somewhere. And technically it's allowing you to autovivify a variable, which is what you're trying to avoid by use'ing strict. So I'd avoid it.

    This is an interesting point of view. 'use strict' isnt to prevent the autovivification of variables (although it does that as a side effect), its to prevent perl from thinking you meant some arbitrary package variable when you made a typo. Consider that the variable can already be "vivified" (in your terms ;-) but strict will still complain:

    use strict; $foo::bar=1; package foo; $bar=1;

    While I suppose there is some sense in thinking of globals/dynamics/package vars (pick your term) as being created, but I personally don't. To me all packages exists simultaneously, as do all package vars. The fact that they havent been thought of or used yet is irelevent. :-)


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...