I want to thank everyone who answered or commented on this question.
I benchmarked my proposed solution and the two ones given in the comments to my question:
my $pre = now;
for ^10000 {
$*REPO.repo-chain.map(*.?candidates('Available::Module').Slip).grep(*.defined);
$*REPO.repo-chain.map(*.?candidates('Unavailable::Module').Slip).grep(*.defined);
}
say now - $pre; # 13.223087
$pre = now;
for ^10000 {
$*REPO.resolve(CompUnit::DependencySpecification.new(:short-name("Available::Module")));
$*REPO.resolve(CompUnit::DependencySpecification.new(:short-name("Unavailable::Module")));
}
say now - $pre; # 3.105257
$pre = now;
for ^10000 {
(try require Available::Module) !=== Nil;
(try require Unavailable::Module) !=== Nil;
}
say now - $pre; # 4.963793
Change the module names to match one that you have available on your system and one that you don't.
The comments show the results in seconds on my computer.
if $*REPO.candidates('Optional::Module')
or similar, though I couldn't seem to get it to work on my sister. but honestly, unless you're doing it many many times, I think(try require Foo) !=== Nil
is about as simple and unfussy as you can get..candidates
currently only works on CURI and CURFS, so using it would be more likesay so $*REPO.repo-chain.map(*.?candidates(...).Slip).grep(*.defined)
. One could use.resolve
directly on$*REPO
instead, but its probably not any shorter:say so $*REPO.resolve(CompUnit::DependencySpecification.new(:short-name("Optional::Module")))
try require Foo
. Perhaps with macros someoe could make anavailable Foo
macro that rewrites into thetry require
bit