http://www.perlmonks.org?node_id=659459

mikasue has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to launch the user's default browser to go to a website in Linux. I know there is a system command in Windows. Is there something similar for Linux?

I looked into HTML::Display but can't find the package under Linux apt-get command.

Any help with launching a browser on Linux is much appreciated.

Replies are listed 'Best First'.
Re: Is there a system command in Linux
by tcf03 (Deacon) on Dec 29, 2007 at 09:44 UTC
    #!/usr/bin/perl use strict; use warnings; $ENV{PATH}="/usr/bin:/usr/local/bin:/bin"; system("firefox http://www.perlmonks.org");

    If you want a default browser, and its under your control you could have users set an environmental var like export BROWSER='/usr/bin/firefox' Other than that, certain distros may have some facility for getting at that, but it would most likely be distro specific. Your mileage may vary.

    Untested, but you could also try something such as
    #!/usr/bin/perl # use strict; use warnings; $ENV{PATH}="/usr/bin:/usr/local/bin:/bin"; my @browsers = qq|firefox lynx|; USEBROWSER: { for (@browsers) { my $status = (system("$_ http://www.perlmonks.org") == 0 ) ? 0 + : 1; last USEBROWSER if $status == 0; } print "No suitable browser was found...\n"; }
    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson
Re: Is there a system command in Linux
by eserte (Deacon) on Dec 29, 2007 at 10:01 UTC
    Every Linux distribution seems to have its own idea about the concept of a default browser. There's htmlview, which I found in old RedHat distributions (Is this still available? I don't have any RedHat systems to test anymore), Debian with their /etc/alternatives concept and so on.

    Some wisdom about starting a web browser in a portable way is collected in this module.

      htmlview is still available on Fedora 7, but it's only a generic wrapper to find the installed web browser.

      The alternatives system is available in RH systems, too.
Re: Is there a system command in Linux
by ww (Archbishop) on Dec 29, 2007 at 14:43 UTC

    This may mean I'm out of my mind, but I'm reading your question as reflecting a misunderstanding of what system is and does. My apologies if I'm wrong, but since most other answers above direct themselves to browser launching (the last line of the OP), allow me to vent on the observation and question in the first paragraph.

    system is a native perl function; not limited to Windows... nor any other OS to which Perl has been ported. The OS specific elements related to system are the commands which you can call. For example,

    C:\perl -e "system(dir);"

    is specific to those OSen which provide a dir program or function. So that won't work for a nix'ish box (unless, of course, someone there are created an executable shell script named "dir") but the comparable *n*x command, ls, will work:

    ww@GIG:~$ perl -e 'system(ls)';

    Note that the quoting has changed; that's an OS-specific issue but system(command) is portable.

    However, for more info (and important 'gotchas'), see

    perldoc -f system system LIST system PROGRAM LIST Does exactly the same thing as "exec LIST", except...

    cf: `, qx//, exec

      Quoting is a shell-specific issue.
      Using bash, perl -e "system(ls);" works fine
      (although you get into problems with more useful one-liners because it does interpolation on $foo)
Re: Is there a system command in Linux
by spx2 (Deacon) on Dec 29, 2007 at 12:32 UTC
    system does work on windows as well as on linux.
    if you want to make your script portable and some portions
    of it are not using portable functions and you need to
    write them specifically for windows and then for linux
    I suggest you make some wrapper functions wich implement
    those portions like
    sub f_windows { } sub f_linux { } sub f_mac { } sub specific_f { my $dispatch_os = { "MSWin32" => \&f_windows, "linux" => \&f_linux, "MacOS" => \&f_mac }; $dispatch_os->{$^O}->(@_); }
    and then call specific_f whenever you need specific
    behaviour for your program this should make your function portable accross
    some os's.
    $^O is an implicit variable in perl wich tells you
    on wich os you're running the script.
    you can read its documentation here

    also here you'll find some code wich is
    made portable accross macos,windows,linux

    I don't understand what do you need HTML::Display for ?

Re: Is there a system command in Linux
by tcf03 (Deacon) on Dec 29, 2007 at 12:59 UTC
    I looked into HTML::Display but can't find the package under Linux apt-get command.

    You may want to use CPAN for installing a module

    perl -MCPAN -e'install HTML::Display
    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson
      a search on google reveals this location in wich the package desired is located
      wether or not it is good to install it I don't know(I know of some problems
      about not having packages from some incompatible repositories,like not having etch and testing at the same time)
Re: Is there a system command in Linux
by Anonymous Monk on Dec 29, 2007 at 12:18 UTC