Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

raku - How to export %*SUB-MAIN-OPTS

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...