如何以编程方式判断是否安装了 Perl 6 模块? [英] How can I tell programmatically if a Perl 6 module is installed?

查看:42
本文介绍了如何以编程方式判断是否安装了 Perl 6 模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩一个插件,它可以加载可用的东西.$*REPO 上的文档并不完整,所以我猜了一下.这似乎有效,但我觉得我错过了一些更简单的东西(除了其他部分的常规打高尔夫球):

I was playing around with a plugin thingy that would load things that were available. The docs on the $*REPO is not quite there so I guessed a bit. This seems to work but I have the feeling I'm missing something simpler (besides the regular golfing on the other bits):

my @modules = <Digest::MD5 NotThere PrettyDump>;
my @installed = gather installed-modules( @modules );

put "Already installed: @installed[]";
try require ::( @installed[0] );

# is there a better way to do this without eval
my $digest = ::( @installed[0] ).new;

sub installed-modules ( *@candidates ) {
    for @candidates -> $module {
        put $module, '-' x 15;
        my $ds = CompUnit::DependencySpecification.new:
            :short-name($module);
        if $*REPO.resolve: $ds {
            put "Found $module";
            take $module;
            }
        else {
            put "Didn't find $module";
            }
        }
    }

推荐答案

$*REPO.resolve(CompUnit::DependencySpecification.new(:short-name<Test>))

请注意,这仅在一定程度上有用,因为这只会告诉您是否可以解析模块.我的意思是,它还会检测到诸如 -I lib 之类的目录提供的未安装模块,而您将不知道 哪个 CompUnit::它来自的存储库.您还可以 grep 类似 $*REPO.repo-chain.grep(* ~~ CompUnit::Repository::Installable).map(*.installed).flat

Note that this is only useful to a certain degree as this only tells you if a module can be resolved. What I mean by this is it would also detect a non-installed module being provided by a directory such as -I lib, and you won't know which CompUnit::Repository it came from. You could also grep the results of something like $*REPO.repo-chain.grep(* ~~ CompUnit::Repository::Installable).map(*.installed).flat

此外,已安装"模块的含义并不那么简单——CompUnit::Repository::Installable 存储库可能是隐含的,但考虑第三方 CompUnit::Repository(例如如 https://github.com/ugexe/Perl6-CompUnit--Repository--Tar ) -- 本质上仍然安装了这些模块,但 repo 本身不是 CompUnit::Repository::Installable.::Installable 在 rakudo 中的真正含义是 rakudo 知道如何安装它——它与 rakudo 知道如何查找和加载

Additionally the meaning of an "installed" module is not so simple -- CompUnit::Repository::Installable repositories are what is likely implied, but consider a third party CompUnit::Repository ( such as https://github.com/ugexe/Perl6-CompUnit--Repository--Tar ) -- with this modules are essentially still installed, but the repo itself is not CompUnit::Repository::Installable. All ::Installable really means in rakudo is that rakudo knows how to install it -- it has nothing to do with what rakudo knows how to find and load

一些 PR(已关闭,但我最终会重新访问)通过 methodCandidate { ... } 帮助解决其中一些问题:

Some PRs ( closed, but I will revisit eventually ) that help address some of these problems via a method candidates { ... }:

https://github.com/rakudo/rakudo/pull/1125

https://github.com/rakudo/rakudo/pull/1132

这篇关于如何以编程方式判断是否安装了 Perl 6 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆