9

Assuming there is a Module that contains the sub MAIN's which is supposed to improve the startup speed. Unfortunately I am unable to use the named-anywhere feature that way. Is my export broken or what am I supposed to do?

use v6.c;

unit module My::Main;
our %*SUB-MAIN-OPTS is export = ( 'named-anywhere' => True);

multi sub MAIN() is export {
    say 1;
}

multi sub MAIN('a', :$pa) is export {
    say $pa;
}
nxadm
  • 2,079
  • 12
  • 17
Martin Barth
  • 796
  • 4
  • 13
  • This feels like a combination of NYI and a bug to me. The dynamic variable *is* exported, because visible in the importing code. But it has lost its value. So I think this warrants a Rakudo issue to make sure we either fix this or determine this is undefined / unwanted / illegal. – Elizabeth Mattijsen Nov 30 '18 at 11:22

2 Answers2

6

You cannot currently export dynamic variables that way, and maybe we never will.

In the meantime, since this is usually in the context of Command Line scripts, there is a way around this:

# in your module:
PROCESS::<%SUB-MAIN-OPTS><named-anywhere> = True;

# in your script
dd %*SUB-MAIN-OPTS'
# Hash element = ${:named-anywhere}

What you're doing there is that your setting the named-anywhere key in the %SUB-MAIN-OPTS hash that lives in the PROCESS:: namespace. That is the outer namespace in which dynamic variables are looked up if they cannot be found anywhere else in the stack. Note that the assignment to the key named-anywhere will actually vivify the hash if it doesn't exist yet. So this will not interfere with any other future additions to the %SUB-MAIN-OPTS hash.

Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105
4

While you can probably export MAIN that way, you have to consider the scope of the %*SUB-MAIN-OPTS variable. It's not clear to me if you are setting the value in the module that imports or in the exporting module. In any case, just print the value within the MAIN subs to check it. I would say that, as a dynamic variable, you will have to set it in the importing module.

Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105
jjmerelo
  • 22,578
  • 8
  • 40
  • 86