in reply to Re: About packages and scopes
in thread About packages and scopes

Some mistakes and corrections.

First of all with strict on you can declare global variables with vars or (in 5.6) with our. (I prefer vars.) Then you don't have to use fully qualified names. In fact doing this is both customary and preferred, because it extends strict's typo checking to global variables, and makes it easier to change what package code is written for. (I would, in fact, assume that anyone routinely using fully qualified names instead of declaring them is simply unaware of how to declare them. Furthermore I would strongly suggest rewriting code which uses lots of fully qualified names without very specific cause.)

Secondly and more importantly, your comment about "implicit namespace" on the $::foo construct is likely to mislead, and may indicate confusion on your part. Writing $::foo does not declare a global in your current package. Instead it declares a global in package main.

How does this work? Well it is simple. Your root namespace is main. In main there is a package main:: which is a reference to the root namespace again. Therefore $::foo is the same as $main::foo is the same as $main::main::foo, etc. (Likewise $foo::bar is the same as $main::foo::bar.) So $::foo is a fully qualified name of a global variable in package main, not your current package.

Therefore there is nothing really implicit about $::foo. And nobody should let the word "implicit" fool you into thinking that it is in the current package. It is in package main.

UPDATE
Added a closing bracket. danger caught the typo.

  • Comment on Re (tilly) 2: About packages and scopes