CPANモジュールBrowser::Openをインストールするオプションまたは望ましくないでない場合は、Taras' answerが良好な代替手段を提供するが、次の方法で改善することができる:
# SYNOPSIS
# openurl <url>
# DESCRIPTION
# Opens the specified URL in the system's default browser.
# COMPATIBILITY
# OSX, Windows (including MSYS, Git Bash, and Cygwin), as well as Freedesktop-compliant
# OSs, which includes many Linux distros (e.g., Ubuntu), PC-BSD, OpenSolaris...
sub openurl {
my $url = shift;
my $platform = $^O;
my $cmd;
if ($platform eq 'darwin') { $cmd = "open \"$url\""; } # OS X
elsif ($platform eq 'MSWin32' or $platform eq 'msys') { $cmd = "start \"\" \"$url\""; } # Windows native or MSYS/Git Bash
elsif ($platform eq 'cygwin') { $cmd = "cmd.exe /c start \"\" \"$url \""; } # Cygwin; !! Note the required trailing space.
else { $cmd = "xdg-open \"$url\""; } # assume a Freedesktop-compliant OS, which includes many Linux distros, PC-BSD, OpenSolaris, ...
if (system($cmd) != 0) {
die "Cannot locate or failed to open default browser; please open '$url' manually.";
}
}
Cygwinの注意点:奇妙な話、文字の解釈からcmd.exe
に渡されたURLを保護する唯一の方法。 &
および^
のようなものは、に後ろのスペースを追加します。しかし、URLには%FOO%
などの環境変数があり、FOO
という環境変数がある場合は、が不注意に展開されます。
ブラウザ:: OpenはCygwinでは機能しません。それは 'cygstart'ではなく 'start'だけを使用します。 – Chloe
@Chloe、はい、それは[Browser :: Openのバグです](https://rt.cpan.org/Public/Bug/Display.html?id=85972)です。うまくいけば、すぐに修正されるでしょう。 – cjm