The perl namespaces extension is designed as replacement of the standard means of declaring and importing symbols, such as the pragma use strict 'vars' and Exporter interface for modules. It makes the symbol lookup rules more similar to those in C++ :

- A variable or function defined in the package AA is also visible in all nested subpackages, e.g. AA::BB or AA::BB::CC. It can be also imported to other packages and then become visible in all their nested subpackages.
- Names of nested packages may be used without qualification in static method calls compiled in the package itself, its subpackages, or the enclosing package:
  package AA::BB; my $x=new BB(1); # instead of new AA::BB(1); package AA; my $y=BB->new(1); # or AA::BB->new(1);
  The names of the nested packages can be imported to other packages as well.
  Note: the namespace lookup rules apply only to subpackage names specified as constant unquoted ("bare") strings. If the package name to be found is variable, use the following function: namespace::lookup_class('package_name', 'class_name') , where package_name is the package to start the search from (e.g. __PACKAGE__).
- Each variable (scalar, array, or hash) must be expicitly declared:
  declare $x;
  initial value is undef (empty for arrays)
  declare $x=123;
  with given initial value
  The declaration must be executed prior to all references to the variable, regardless whether from the own package or from a nested package or other importing package. However, the declaration does not have to be the first in lexical sense, that is, it can be put at the end of the script or even in another module included sometime later.
  Every variable must be declared only once.
  Violation of these two rules leads to an exception (die) raised by the first access to the variable.
  The standard statement use vars '$x'; is also accepted as a declaration.
- Variables accessed via qualified names, such as $AA::x, are also required to be declared, unless the package they belong to was compiled without namespaces extension.

Lexical variables (my) are not affected by this extension. our declarations are not compatible with the namespaces and should be avoided.

The global namespace main:: is purposely left aside as it contains many special variables (such as $_ or %ENV) which are automatically visible from all packages, and it would be absolutely silly to require them be explicitly declared. Some other special package-specific variables, such as @INC, $AUTOLOAD, or $a and $b in a sort block, may also be used without declaration.

### Syntax

The namespace extension is implemented as a special compilation mode. It can be enabled and disabled for each lexical scope, just like use strict. Each package activated during the namespace compilation mode (with the package statement) is treated later as using namespaces, even if other parts of it are compiled in the standard, non-namespace mode. Mixing of both modes in one package is not a very good practice, however, as it could lead to unexpected exceptions caused by undeclared variables.

use namespaces; no namespaces;
Enables or disables the namespaces mode. The setting lasts up to the end of the current lexical scope. Note that the compilation of each source file starts with disabled namespace mode, even if the require or use statement which caused its inclusion lies in the namespace-enabled scope.
Enabling the namespace mode automatically implies no strict 'vars' and vice versa.
use namespaces 'AA', 'BB::CC', ...;
Enables the namespace mode and sets the import list for all packages defined later in the current lexical scope. This is the analogon of the C++ directive using namespace ... .
The importing relation between packages is transitive. For example, the lookup list for the packages compiled after the statement shown above will consist of AA, the packages imported by AA, BB::CC, the packages imported by BB::CC, BB, and the packages imported by BB. To avoid multiple searches, duplicates in the lookup list are removed immediately.
no namespaces 'AA', 'BB::CC', ...;
Removes the named packages from the current import list, but lets the namespace compilation mode active. Note that this change will affect only packages defined after this statement.

### Effects at the run time

Each reference to a variable, both reading and writing, is first checked whether it refers to a declared or imported variable. If not, and the name is unqualified, then a declared variable with this name is looked up in all packages comprising the lookup list, as explained above. If found, the variable is imported into the current package. Otherwise an exception is raised. A qualified variable name is only checked to be pre-declared, if not, an exception is raised as well.

The lookup operation is performed once per variable and package; the declaration check is performed once per variable and each expression (script line) using it which was ever executed. This way the performance penalty is constant and comparable to that of the traditional import mechanism.

Unqualified subroutine calls are resolved in the same manner, but in the case of a failure no special exception is raised, since it will be done by the perl interpreter itself. Taking a subroutine reference \&abc involves the lookup too. There is also a special function namespaces::lookup_sub('package_name', 'sub_name') returning the code reference or undef if the lookup fails.

eval '...' statements, as well as regular expressions with deferred evaluation (like s///e operators or $(?{ }) expressions), re-establish the namespace environment for the duration of the expression compilation.

### namespaces in `polymake`

All rulefiles and perl modules defined in the application-specific perllib subdirectories are compiled with namespace mode enabled, due to the following statement automatically prepended to each source file:

use namespaces 'Apps::APPLICATION_NAME';

If an application imports `polymake` modules, their packages 'Modules::MODULE_NAME' are also included in the lookup list.

Finally, all applications and modules import everything from the package Poly, which contains some useful little functions like min and max.