2021年4月10日星期六

how do I access a module's symbol table dynamically at runtime in Raku?

I want to be able to pass my script the path to a .rakumod file, say <blah>/Mod.rakumod, and be able to access the symbol table as a hash as if I'd used the module instead:

The module:

$ cat Mod.rakumod  unit module Mod;  sub bag        is export             { ... }  
use lib <dir-containing-Mod>  use Mod;  say Mod::EXPORT::.keys  

works, as expected, returning (ALL DEFAULT).

On the other hand:

use lib <dir-containing-Mod>  require Mod;  say Mod::EXPORT::.keys  

fails with

Could not find symbol '&EXPORT' in 'Mod'    in block <unit> at <blah>  

This is despite the fact that even with require, say Mod::.keys does see EXPORT:

use lib <dir-containing-Mod>  require Mod;  say Mod::.keys  ---  (EXPORT Mod)  

I need to use require to make this dynamic, as I don't know which module I'll want.


Edit

Thought I'd add this: I can actually think of one thing to do, but it is absolutely disgusting:

  • save my module name into a variable $mod
  • have my script write another script using that module:
my $mod = <whatever>  my $cd = qq:to/END/;  use v6;  use lib qq\|\$\*CWD\|;  use $mod;    say {$mod}::EXPORT::ALL::.keys;  END    'aux.p6'.IO.spurt($cd);  
  • and then have the initial script call the auxiliary one:
shell("raku aux.p6")  
https://stackoverflow.com/questions/67040798/how-do-i-access-a-modules-symbol-table-dynamically-at-runtime-in-raku April 11, 2021 at 09:56AM

没有评论:

发表评论