简体   繁体   English

如何在任何操作系统上启动Perl中的默认Web浏览器?

[英]How do I launch the default web browser in Perl on any operating system?

I want to open a URL, such as http://www.example.com/ , at the end of a Perl script. 我想在Perl脚本的末尾打开一个URL,例如http://www.example.com/ I don't want to access it with WWW::Mechanize but actually show the web page to the user in a graphical web browser. 我不想使用WWW :: Mechanize访问它,但实际上是在图形Web浏览器中向用户显示该网页。

There are ways to do this in Mac ( open URL ) and Windows, but I want a solution that works on any operating system, not just one. 有办法在Mac( open URL )和Windows中执行此操作,但我想要一个适用于任何操作系统的解决方案,而不仅仅是一个。

The second hit on "open url" at search.cpan brings up Browser::Open : 在search.cpan上第二次打开“open url”会打开Browser :: Open

use Browser::Open qw( open_browser );

my $url = 'http://www.google.com/';
open_browser($url);

If your OS isn't supported, send a patch or a bug report. 如果您的操作系统不受支持,请发送补丁或错误报告。

You can use $^O variable to identify a platform and use different commands for each OS. 您可以使用$^O变量来标识平台,并为每个OS使用不同的命令。

For example: 例如:

sub open_default_browser {
  my $url = shift;
  my $platform = $^O;
  my $cmd;
  if    ($platform eq 'darwin')  { $cmd = "open \"$url\"";          } # Mac OS X
  elsif ($platform eq 'linux')   { $cmd = "x-www-browser \"$url\""; } # Linux
  elsif ($platform eq 'MSWin32') { $cmd = "start $url";             } # Win95..Win7
  if (defined $cmd) {
    system($cmd);
  } else {
    die "Can't locate default browser";
  }
}

open_default_browser("http://www.example.com/");

If installing CPAN module Browser::Open is not an option or not desired, Taras' answer provides a good alternative, but can be improved in the following ways: 如果安装CPAN模块Browser :: Open不是一个选项或不需要, Taras的答案提供了一个很好的选择,但可以通过以下方式进行改进:

  • make the function work robustly on Windows with URLs that contain shell metacharacters such as & and ^ . 使用包含shell元字符(如&^ URL在Windows上强大地运行该函数。
  • on Windows, add support for the MSYS, Git Bash, and Cygwin Unix-emulation environments 在Windows上,添加对MSYS,Git Bash和Cygwin Unix仿真环境的支持
  • add support for additional operating systems that also have the xdg-open utility, namely all OSs that are freedesktop.org -compatible, ie, use GUIs that are X Window-based, which includes non-Linux platforms such as PC-BSD (FreeBSD-based) and OpenSolaris. 添加对具有xdg-open实用程序的其他操作系统的支持,即所有与freedesktop.org兼容的操作系统,即使用基于X Window的GUI,其中包括非Linux平台,如PC-BSD(FreeBSD) (基于)和OpenSolaris。
# 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 caveat: Bizarrely, the only way to protect the URL passed to cmd.exe from interpretation of chars. Cygwin警告:奇怪的是,保护传递给cmd.exe的URL的唯一方法是解释字符。 such as & and ^ is to append a trailing space . 例如&^附加一个尾随空格 This works in all but one edge case, which, however, should be rare in the real world: if the URL contains something like %FOO% and an environment variable named FOO exists , %FOO% is inadvertently expanded. 这适用于除了一个边缘情况之外的所有情况,但是在现实世界中应该是罕见的:如果URL包含类似%FOO%并且存在名为FOO的环境变量,则无意中扩展了%FOO%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM