Based on the module-source-finding code I have from the designer of the CompUnit
system, what you have is basically as simple as it gets. The only difference I see is they used $*REPO.need
, which also throws if it can't find a module:
my @installed = installed-modules <Digest::MD5 Test NotThere PrettyDump>;
sub installed-modules {
gather for @_ -> $mod {
put $mod, '-' x 15;
(try $*REPO.need: CompUnit::DependencySpecification.new: :short-name($mod))
andthen "Found $mod".put and $mod.take
orelse "Didn't find $mod".put
}
}
require
returns the loaded module on successful load, so you can just assign it to a variable right there and then:
put "Already installed: @installed[]";
(my $digest = (try require ::(@installed.head)).new) === Nil
and die "Failed to load @installed.head"