Are our variables dynamic?
Seems like dynamic variables are looked up in the GLOBAL
name space. Thus the following works:
unit package ABC;
$GLOBAL::var="Duo";
sub sub1() {
say $*var;
}
sub1();
#output is 'Duo'
The reason your first example works is that (according to the documentation):
The user's program starts in the
GLOBAL
package, so "our" declarations in the mainline code go into that package by default.
Dynamic variable lookup conceptually happens in all dynamic scopes. Dynamic scopes are first PROCESS::
, then GLOBAL::
and then whatever dynamic scopes that the program has.
So when you look up a dynamic variable, it will first look in all dynamic scopes from the current down. When it doesn't find it there, it will then look in GLOBAL::
, and if not found, in PROCESS::
.
For example, if you want to print something on STDOUT, it will look up the $*OUT
dynamic variable. If you did not define one somewhere in your dynamic scopes, it will use the one from PROCESS::
:
dd PROCESS::<$OUT>;
# IO::Handle element = IO::Handle.new(path => IO::Special.new("<STDOUT>")...)