3

Background: There are a number of modules in the local lib eg Eg-1.rakumod Eg-2.rakumod Eg-3.rakumod, each Eg-\d is referenced in META6.json file, and each module has the code

sub SITE is export { ... }

Clarification: SITE is written in each example so the SITE.routes generates a CRO route set.

When another program eg., show-example.raku, is written with

use Eg-1;
my $routes = SITE.routes;

show-example.raku compiles and runs. SITE is in the namespace and yields a structure to be stored in $routes.

However, if we have in show-example2.raku

my @egs = <Eg-1 Eg-2 Eg-3>;
my $option = 1; # pointing to Eg-2
require ::(@egs[1]);
my $routes = SITE.routes

running raku -I. show-example2.raku fails to compile. Naturally because at compile time SITE is not known to be the name of a sub, although it will be known at runtime.

The question is how to refer to a sub that is known at runtime?

However, this does not seem to work:
my $routes = &::(@egs[1])::SITE.routes

error: No such symbol Eg-2::&SITE

3
  • 1
    require ::(@egs[1]) <&SITE>;? also your last attempt would work if &SITE is declared our (it's my by default) Commented Aug 2 at 12:34
  • Raku's standard default export mechanisms (which is export is a part of) store any exported symbols (subs or any other) in a Stash (Symbol table hash) named DEFAULT (or other :tag if is export(:tag) is used) plus another called ALL. Symbols corresponding to these Stashes are in turn stored in another Stash named EXPORT. And a symbol corresponding to that is stored in the Stash corresponding to the package whose symbols are being exported (in your code, the one ::(@egs[1]) refers to). Your presumption that it was otherwise is wrong but understandable. Commented Aug 4 at 12:19
  • So, for a solution along the lines you were thinking about when you wrote &::(@egs[1])::SITE you would need to instead write &::(@egs[1])::EXPORT::DEFAULT::SITE. I'm currently planning to finish and post an answer that expands on the many topics I think worth covering. Commented Aug 4 at 15:11

1 Answer 1

0

One suggestion that works is to avoid require and use use in a restricted sense.

my @egs = <Eg-1 Eg-2 Eg-3>;
my $option = 1; # pointing to Eg-2
my $routes = "use @egs[$option]; SITE.routes".EVAL;
Sign up to request clarification or add additional context in comments.

1 Comment

I'd say that is using the mother (or should I say MONKEY?) of all hacks! 🤣 I am preparing an answer that will provide the clean solution that the relevant mechanisms provide and that Rakoons are supposed to use, and a detailed explanation of those mechanisms, and how I would characterize the initial mistakes you made, and why I think others didn't figure out the simple answer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.