如何检查外部程序是否可以通过 Raku 运行?在 shell 中,type将使用命令,例如:
if type trash-put
then trash-put delete-me
else rm delete-me
fi
Run Code Online (Sandbox Code Playgroud)
你不能run 'type', 'trash-put'在 Raku 中,因为它type是一个内置的 shell。
你可以,run 'sh', '-c', 'type trash-put'或者shell 'type trash-put',所以 Raku 等价物是:
if ! run( 'sh', '-c', 'type trash-put', :!out ).exitcode {
# if ! shell( 'type trash-put', :!out ).exitcode { # shell alternative to run
run 'trash-put', 'delete-me';
} else {
unlink 'delete-me'.IO;
}
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更好的方法。
问题不限于删除文件,其他用例也需要答案:curl比浏览器 2wget或浏览器更喜欢over或 browser1 或$VISUALover$EDITOR等。
这是一个使用Inline::Perl5和can_run()来自 Perl 5 模块IPC::Cmd的子例程的示例:
use v6;
use IPC::Cmd:from<Perl5> <can_run>;
my $cmd = 'wget';
if (my $exec = can_run($cmd)) {
say "Path to {$cmd} : $exec";
}
else {
say "{$cmd} was not found in PATH";
}
Run Code Online (Sandbox Code Playgroud)
我机器上的输出:
Path to wget : /bin/wget
Run Code Online (Sandbox Code Playgroud)