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

Monks,

I wrote a script on everyone's favorite OS (sarcasm anyone?), Win XP, and I can get it to execute an external program, but it just hangs from there. If I exit the program that I'm trying to run, the script continues. Also, if I run the command simply from dos, it launches fine and brings me right back to the command prompt.

I tried using "exec" instead of "system" an although it executes the command, it quits my perl script.

Is there a way to spawn a new process to run this command or does Windows have an equivalent to Unix's '&' option to run a command in the background? Here is the portion of code that executes the program:
my $bi = 'C:\Program Files\program.exe'; if ($ans =~ /\bn\b/i){ print "\n### Now starting program.exe\n\n"; system ($bi) and print "Could not execute $bi: $!\n"; stop_services(); }

Replies are listed 'Best First'.
Re: MS Forking Blues
by BUU (Prior) on Sep 26, 2005 at 20:38 UTC
    Try "start".
      Thank you, that did it!
Re: MS Forking Blues
by chromatic (Archbishop) on Sep 26, 2005 at 20:40 UTC

    system and exec are doing what they ought to do. system runs an external program and returns when it exits. exec replaces the current process with another process.

    Your title suggests fork but you don't actually use it in this code. On Unix, the idiom would be to fork the program and, in the child, exec the other program you want to run. When you do this, the parent continues as it is and the child immediately replaces itself with the other process.

    You can get more complex if you need to do so -- and you may, on Windows, with something like the Win32 module and CreateProcess -- but I think you may have misunderstood that there are three different ideas here and just what the differences are between them.

Re: MS Forking Blues
by tye (Sage) on Sep 27, 2005 at 00:53 UTC
    system( 1, $bi )

    Too bad a portable way to "spawn" isn't provided for Perl scripts despite one being available in Perl's C code. The above trick only works in "DOSish" operating systems.

    - tye