8

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).

1 Answer 1

11

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)

3
  • What is the relationship between our and is export ? Jan 31, 2017 at 21:21
  • @BradClawsie, probably little, as is export code stuffs all the symbols into the importing package, whereas our makes things package scoped. What exactly that means when used in a mainline of a separate file... I'm not 100% sure. I tried pinging the two people who would know, but they were AFK :) The answer might also be slightly wrong. When I dump GLOBAL, I don't see the constant there, but instead in MY. And modules don't see it unless I use the file with the constant in them as well.
    – user2410502
    Feb 1, 2017 at 19:59
  • @BradClawsie, ah, nm, I got the explanation for the missing parts: it's not visible in all the modules because of precompilation as well as lexical module loading (which is why the symbol ends up in MY:: instead of GLOBAL).
    – user2410502
    Feb 1, 2017 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.