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

前端 未结 3 461
再見小時候
再見小時候 2021-02-07 21:25

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

3条回答
  •  误落风尘
    2021-02-07 22:16

    You can use $^O variable to identify a platform and use different commands for each 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/");
    

提交回复
热议问题