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

Hi Monks! I am writing a perl/tk script and I would like to have its help files (html) opened in the users browser. I thought of using my $output = `firefox myhelp.html` if the user has a different browser, try an other one e.t.c. Is there a simpler way of doing it?? Thank you!

Replies are listed 'Best First'.
Re: opening browser from script
by zentara (Cardinal) on May 16, 2007 at 12:44 UTC
    Here is a more "feature-filled" script that you can derive some ideas from (for linux). More ideas are here-> Perl Windows Open a Web Browser
    #!/usr/bin/perl -w use strict; use Cwd; my $pid; my @docs; if(@ARGV){ my $cwd = getcwd; foreach my $arg(@ARGV){ if( $arg =~ /^http:\/\/.*$/ ){ push @docs, $arg } else{ push @docs , "file://$cwd/$arg" } } } else{ #load defaults @docs = qw( file:///home/zentara/1down/1GET http://www.google.com ); } # Look for instance of mozilla. `mozilla -remote "ping()"`; if ( $? ) { # Error, mozilla is not running. if (!defined($pid = fork())) { # Undef branch of fork: die "Cannot fork: $!"; } elsif ($pid == 0) { # Child branch of fork: `mozilla`; # Start new instance.... } else { # Parent branch of fork: do { `mozilla -remote "ping()"`; #sleep 1; select(undef,undef,undef,.5); } while ( $? ); #sleep 1; #give mozilla some time select(undef,undef,undef,.5); load_docs(); $pid=waitpid($pid, 0); # Wait for the pseudo-process # containing the new mozilla instance # to end. } } else { load_docs(); } ############################################################### sub load_docs { my $element; foreach (0..$#docs){ if ( $docs[$_] =~ /~file:\/\// ) { `mozilla -remote "openFile($docs[$_], new-tab)"` ; #`mozilla -remote "openFile($docs[$_], new-window)"` ; } else { `mozilla -remote "openURL($docs[$_], new-tab)"` ; # `mozilla -remote "openURL($docs[$_], new-window)"` ; } } }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Thank you shmem & zentara. I couldn't find htmlview in my ubuntu 6.10, so I finally used
      if ($output = `firefox`) {print "no browser found\n"}
        Are you sure that works like you think? The way I interpret it, you are setting $output to the backticks returned string with the =. Maybe you need == or eq ? Or I am possibly just oblivious to the magic invoved. :-)

        I'm not really a human, but I play one on earth. Cogito ergo sum a bum
        That
        if ($output = `firefox`) {print "no browser found\n"}

        would mean that you invoke firefox, wait for firefox to exit and stuff its output into $output. Meanwhile (while firefox is running) your application is blocked. Is that the intended behaviour?

        What happens if the invocation fails? Most probably the shell will send an error message to its STDERR, which you don't collect in $output. Bummer, no error message "no browser found"...

        Collecting the output of a program that doesn't exist is no good to test its existence. A better approach would be

        BROWSER: { for my $dir (split /:/, $ENV{PATH}) { for my $browser (qw(firefox mozilla opera konqueror)) { if (-x "$dir/$browser") { system "$dir/$browser" # or "$dir/$browser &" to bac +kground and die "running $browser returned $?\n"; last BROWSER; } } } print "no browser found.\n"; }

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: opening browser from script
by shmem (Chancellor) on May 16, 2007 at 11:21 UTC
    That depends on what platform you are on. If I remember right, on Windows you can do
    system 'start', $htmlfile and die "could not invoke browser: $!\n";

    and it will open the default browser. On some linux distributions, there is htmlview which will open whatever browser is installed / configured in the preferences:

    system "htmlview '$htmlfile' &" and die "starting htmlview failed: $!\ +n";

    Note - system ... and die - 'and' is necessary because system returns nonzero on failure.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}