Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell programmatically if a Perl 6 module is installed?

Tags:

module

raku

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";
            }
        }
    }
like image 951
brian d foy Avatar asked Feb 21 '18 06:02

brian d foy


People also ask

What Perl packages are installed?

You need to use instmodsh (interactive inventory for installed Perl modules) command to find out what modules already installed on my system. instmodsh command provides an interactive shell type interface to query details of locally installed Perl modules.

Where are Perl modules stored?

Perl packages should install module files into /usr/lib/perl5/$version/vendor_perl/ (use perl -V:vendorarch in scripts), or /usr/share/perl5/vendor_perl/ .


1 Answers

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

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

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

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

like image 122
ugexe Avatar answered Oct 10 '22 01:10

ugexe