I want to open a URL, such as http://www.example.com/
, at the end of a Perl script. I don't want to access it with WWW::Mechanize but actually show the web page to the user in a graphical web browser.
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.
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:
&
and^
.xdg-open
utility, namely all OSs that are freedesktop.org-compatible, i.e., use GUIs that are X Window-based, which includes non-Linux platforms such as PC-BSD (FreeBSD-based) and OpenSolaris.Cygwin caveat: Bizarrely, the only way to protect the URL passed to
cmd.exe
from interpretation of chars. 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 namedFOO
exists,%FOO%
is inadvertently expanded.The second hit on "open url" at search.cpan brings up Browser::Open:
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.For example: