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

Greetings monks,

I am a very inexperienced perl coder.

I am using perl, v5.6.1 built for MSWin32-x86-multi-thread (ActiveState build 633) on Windows XP.

Out of pure laziness in not wanting to have to click several bookmarks each time I open my browser, I am trying write a short script that open a new Mozilla window, and then pass the -remote command line option to it a few times to open tabs of the URLs that I want to view.

It opens mozilla just fine, but it never seems to get to the last three lines. Is this a place where my complete noncomprehension of the fork command is hindering me?

I tried reading about exec, system, and fork on perl.com, but I got confused when it started talking about zombies. I know that this is probably very simple, but I'm getting a bad case of RTFM burnout.

Here's what I have so far:

#!/usr/bin/perl -w use strict; local $ENV{"PATH"} = "c:/progra~1/mozilla.org/Mozilla"; system ("mozilla") || die "can't open mozilla"; system ("mozilla -remote openURL (www.perlmonks.org, new-tab)"); system ("mozilla -remote openURL (www.metafilter.org, new-tab)"); system ("mozilla -remote openURL (www.slashdot.org, new-tab)");

Edit: $title =~ s/Confusued/Confused/, per request. larsen

Replies are listed 'Best First'.
Re: Confusued about Fork and System
by Coruscate (Sexton) on Mar 20, 2003 at 19:50 UTC

    system() waits until the running program has completed its execution before running the next line. So you are opening mozilla and then perl waits for you to close mozilla before it tries running the next one.

    I was going to introduce a way of forking each URL into the browser, but I thought of using backticks, which do not block until execution is complete. So you end up with this:

    my @urls = ( 'www.perlmonks.org', 'www.metafilter.org', 'www.slashdot.org' ); `mozilla`; `mozilla -remote "openURL ($_, new-tab)"` for @urls;


    Update: Just to point out one little thing, the system ("mozilla -remote openURL (www.perlmonks.org, new-tab)"); isn't quite correct. The command to remote has to be contained within quotes, so something along the lines of system ('mozilla -remote "openURL (www.perlmonks.org, new-tab)"'); is necessary. :)


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

Re: Confusued about Fork and System
by robartes (Priest) on Mar 20, 2003 at 19:51 UTC
    system only returns when the command that is launched returns. In your case, the first system only returns when mozilla is finished, so the script only continues after mozilla has exited. One way to deal with this is to fork off a child to run mozilla. Something like:
    #!/usr/local/bin/perl -w use strict; die "Something fishy: could not fork.\n" unless (defined(my $pid=fork( +))); if ($pid) { # $pid has a non zero value, so this is the parent sleep 5; # give mozilla some time to start up system ("mozilla -remote openURL (www.perlmonks.org, new-tab)"); system ("mozilla -remote openURL (www.metafilter.org, new-tab)"); system ("mozilla -remote openURL (www.slashdot.org, new-tab)"); waitpid($pid,0); # wait until mozilla is finished } else { # $pid is zero, so this is the child system ("mozilla"); }
    This code is untested.

    If Windows supports backgrounding a process from the shell, another solution is to run mozilla in the background in your first system(), but I have no idea whether that's possible or not.

    CU
    Robartes-

Re: Confusued about Fork and System
by Nkuvu (Priest) on Mar 20, 2003 at 19:52 UTC
    Perl code aside, why not just use Mozilla's "Bookmark this group of tabs" feature?