Organizing and referencing namespaced program elements
Packages are nested namespaces of named program elements. Modules, classes, grammars, and others are types of packages. Like files in a directory, you can generally refer to named elements with their short-name if they are local, or with the longer name that includes the namespace to disambiguate.
A name is anything that is a legal part of a variable name (not counting the sigil). This includes:
my ; # simple identifierssay Foo::Bar.baz; # Calling a method; OUTPUT: «Þor is mighty␤»say Foo::Bar::zape; # compound identifiers separated by ::; OUTPUT: «zipi␤»my = 'Bar';say ::()::quux;# compound identifiers with interpolations; OUTPUT: «42␤»$42; # numeric names$!; # certain punctuation variables
Ordinary package-qualified names look like this:
$Foo::Bar::quux, which would be the
$quux variable in package
Foo::Bar::zape would represent the
&zape variable in the same package.
Sometimes it's clearer to keep the sigil with the variable name, so an alternate way to write this is:
(This does not work with the
&zape variable) The name is resolved at compile time because the variable name is a constant.
If the name part before
:: is null, it means the package is unspecified and must be searched for. Generally this means that an initial
:: following the main sigil is a no-op on names that are known at compile time, though
::() can also be used to introduce an interpolation. Also, in the absence of another sigil,
:: can serve as its own sigil indicating intentional use of a not-yet-declared package name.
|MY||Symbols in the current lexical scope (aka $?SCOPE)|
|OUR||Symbols in the current package (aka $?PACKAGE)|
|CORE||Outermost lexical scope, definition of standard Perl|
|GLOBAL||Interpreter-wide package symbols, really UNIT::GLOBAL|
|PROCESS||Process-related globals (superglobals). The last place dynamic variable lookup will look.|
|COMPILING||Lexical symbols in the scope being compiled|
|CALLER||Contextual symbols in the immediate caller's lexical scope|
|CALLERS||Contextual symbols in any caller's lexical scope|
|DYNAMIC||Contextual symbols in my or any caller's lexical scope|
|OUTER||Symbols in the next outer lexical scope|
|OUTERS||Symbols in any outer lexical scope|
|LEXICAL||Contextual symbols in my or any outer's lexical scope|
|UNIT||Symbols in the outermost lexical scope of compilation unit|
|SETTING||Lexical symbols in the unit's DSL (usually CORE)|
|PARENT||Symbols in this package's parent package (or lexical scope)|
|CLIENT||The nearest CALLER that comes from a different package|
The file's scope is known as
UNIT, but there are one or more lexical scopes outside of that corresponding to the linguistic setting (often known as the prelude in other cultures). Hence, the
SETTING scope is equivalent to
UNIT::OUTERS. For a standard Perl 6 program
SETTING is the same as
CORE, but various startup options (such as
-p) can put you into a domain specific language, in which case
CORE remains the scope of the standard language, while
SETTING represents the scope defining the DSL that functions as the setting of the current file. When used as a search term in the middle of a name,
SETTING includes all its outer scopes up to
CORE. To get only the setting's outermost scope, use
You may interpolate a string into a package or variable name using
::($expr) where you'd ordinarily put a package or variable name. The string is allowed to contain additional instances of
::, which will be interpreted as package nesting. You may only interpolate entire names, since the construct starts with
::, and either ends immediately or is continued with another
:: outside the parentheses. Most symbolic references are done with this notation:
= "Bar";= "Foo::Bar";$::() # lexically-scoped $Bar$::("MY::$foo") # lexically-scoped $Bar$::("OUR::$foo") # package-scoped $Bar$::("GLOBAL::$foo") # global $Bar$::("PROCESS::$foo")# process $Bar$::("PARENT::$foo") # current package's parent's $Bar$::() # $Foo::Bar$::()::baz # $Foo::Bar::baz$::()::Bar::baz # $Bar::Bar::baz$::()baz # ILLEGAL at compile time (no operator baz)
:: doesn't imply global. Here as part of the interpolation syntax it doesn't even imply package. After the interpolation of the
::() component, the indirect name is looked up exactly as if it had been there in the original source code, with priority given first to leading pseudo-package names, then to names in the lexical scope (searching scopes outwards, ending at
CORE). The current package is searched last.
MY pseudopackage to limit the lookup to the current lexical scope, and
OUR to limit the scopes to the current package scope.
In the same vein, class and method names can be interpolated too.
does with-methoddoes with-method ;my ='a-class';say ::().a-method;= 'b-class';say ::().a-method;my ='a-method';say a-class."$what-method"();='another-method';say a-class."$what-method"();
To do direct lookup in a package's symbol table without scanning, treat the package name as a hash:
Foo::Bar:: # same as &Foo::Bar::bazPROCESS::<$IN> # Same as $*INFoo::<::Bar><::Baz> # same as Foo::Bar::Baz
::() symbolic references, this does not parse the argument for
::, nor does it initiate a namespace scan from that initial point. In addition, for constant subscripts, it is guaranteed to resolve the symbol at compile time.
The null pseudo-package is the same search list as an ordinary name search. That is, the following are all identical in meaning:
Each of them scans lexical scopes outward, and then the current package scope (though the package scope is then disallowed when "strict" is in effect).
As a result of these rules, you can write any arbitrary variable name as either of:
You can also use the
::<> form as long as there are no spaces in the name.
Subscript the package object itself as a hash object, the key of which is the variable name, including any sigil. The package object can be derived from a type name by use of the
Methods—including auto-generated methods, such as public attributes' accessors—are stored in the class meta object and can be looked up through by the lookup method.
Interpreter globals live in the
GLOBAL package. The user's program starts in the
GLOBAL package, so "our" declarations in the mainline code go into that package by default. Process-wide variables live in the
PROCESS package. Most predefined globals such as
$*PID are actually process globals.
Any package may have a Version object attached to it. This object can be accessed via
$?PACKAGE.^ver or from outside the package