我在 中安装了 Perl 6 ~/.rakudo-star/rakudo-star-2018.04,使用 LoneStar。当zef安装了一个模块,它被安装到Rakudo Perl 6的目录的子目录。这里有一个名为 的目录perl6/site/resources,它似乎包含所有已安装的文件。如何使用 Perl 6 找出哪个模块包含在哪个文件中?
如果您想获取将被加载的命名空间的来源,您可以执行以下操作:
my $module-name = 'Test';
# Get a Distribution object which provides an IO interface to its contents
my $compunit = $*REPO.resolve(CompUnit::DependencySpecification.new(:short-name{$module-name}));
my $distribution = $compunit.distribution;
my $handle-from-name = $distribution.content($distribution.meta<provides>{$module-name}.keys[0]).open;
say $handle-from-name.slurp(:close);
# Or if we already know the name-path:
my $handle-from-path = $distribution.content("lib/Test.pm6").open;
say $handle-from-path.slurp(:close);
Run Code Online (Sandbox Code Playgroud)
请注意,$compunit.distribution只有当 resolve 从CompUnit::Repository::Installation存储库返回 CompUnit 时才有效。
rakudo@1812是进一步改进这一点的框架,允许查询单个存储库($*REPO.resolve迭代存储库的链接列表以给出结果)并在CompUnit::Repository::Installation和之间统一解析/候选者/等的行为CompUnit::Repository::FileSystem。