in reply to Unusual variable declaration
our $var; # Declare a variable in the current package. *var = \$main::var; # Create an alias in the current package to packag +e main's $var;
Observe the following:
package Foo; our $var = 42; print __PACKAGE__, ": $var\n"; print \$var, "\n"; package main; *var = \$Foo::var; print __PACKAGE__, ": $var\n"; print \$var, "\n";
The output:
Foo: 42 SCALAR(0x559ec7e7b6a0) main: 42 SCALAR(0x559ec7e7b6a0)
This is aliasing; you're creating in main a symbol that refers to the same variable that is known in Foo as $var. Notice they share the same memory address. (It will be a different address on your system).
In your example code you were creating $var in the current package and aliasing it to $main::var (the $var that belongs to main::). The example I gave is going in the opposite direction because it's easier to demonstrate. But the concept is the same; you're setting up a symbol that refers to the same thing that is known in another package namespace simultaneously.
This is what Exporter does.
Dave
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Unusual variable declaration
by Jenda (Abbot) on Nov 04, 2019 at 12:37 UTC | |
by LanX (Saint) on Nov 04, 2019 at 14:44 UTC | |
by davido (Cardinal) on Nov 04, 2019 at 19:28 UTC | |
by LanX (Saint) on Nov 04, 2019 at 22:25 UTC | |
by Jenda (Abbot) on Nov 05, 2019 at 11:19 UTC | |
by LanX (Saint) on Nov 05, 2019 at 12:27 UTC | |
|
In Section
Seekers of Perl Wisdom