Reputation: 11168
Given the module file test.pm6
:
constant $AUTHOR='me';
And the script test_script.p6
:
use lib '.';
use test;
my $AUTHOR = 'someone';
I get the following warning when I compile check test_script.p6
:
perl6 -c test_script.p6
Potential difficulties:
Redeclaration of symbol '$AUTHOR'
at test_script.p6:5
------> my $AUTHOR⏏ = 'someone';
Syntax OK
However, simply changing test.pm6
to one of the following makes this warning go away:
my $AUTHOR='me';
or
my constant $AUTHOR='me';
So, the question is whether constants should be imported automatically or is this a bug?
This is using Rakudo Star 2017.01 Release Candidate 0 installed on CentOS Linux release 7.3.1611 (Core).
Upvotes: 9
Views: 212
Reputation:
This is not a bug. Constants are our
scoped by default and your constant in test.pm6
is still in the mainline and so gets placed into the GLOBAL
namespace and since it's an our
, it's visible in your main script.
One way to avoid it is what you found: to use a my
on constants/classes (as they default to our
). Or conversely, to use an our
on subroutines/variables that you want to make visible (as subs default to my
).
Another way is to use, say, unit module BlahBlah;
at the top of your module file, and then these symbols will be in BlahBlah
namespace and not in the GLOBAL
and so will not be visible in the main script directly (our
symbols can still be accessed as BlahBlah::whatever
)
P.S.: those on 2016 Rakudos won't observe this behaviour due to lexical module loading bug, which got fixed only in 2017.01 compiler release (and was merged to master a couple of days after 2016.12 compiler release)
Upvotes: 12