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

I'm trying to launch a windows executable from a perl script.
$BaseDir = "C:\\Program Files\\myprog\\"; $cmd = $BaseDir."myprog"; `$cmd`;
The problem is that the backticks wait for the program to be exited before continuing. From my understanding the backticks are supposed to act like exec, not system.

If I do exec $cmd; then it gives me an error because of the space between Program and Files. If I try system is does the same thing the backticks do.

I even tried putting quotations around the file path before I passed it to the exec command but the perl script exits without errors after I close the executed program without finishing the rest of my script.

Replies are listed 'Best First'.
Re: Perl Exec with Spaces
by kyle (Abbot) on Mar 11, 2009 at 19:53 UTC
      Thank you, I didn't realize system would allow me to execute something without waiting for it to end.
Re: Perl Exec with Spaces
by xyzzy (Pilgrim) on Mar 11, 2009 at 19:42 UTC
    windows shells don't have escape sequences for spaces, so you have to put double quotes inside $cmd. try:
    $cmd = "\"$BaseDir$myprog\""


    Everything is true." "Even false things?" "Even false things are true" "How can that be?" "I dunno man, I didn't do it."
      I did, it executes the program, but exits the perl script without warning and most importantly without finishing the rest of my script.

        As it should.

        You can either use IPC::Open3 or launch your application via start.

Re: Perl Exec with Spaces
by VinsWorldcom (Prior) on Mar 11, 2009 at 20:03 UTC

    Something I've used before in a Perl script to launch a 'tail -f' in a CMD.EXE window so I could monitor along with file output as the Perl script runs:

    if (-e "c:\\usr\\bin\\tail.exe") { system ("start \"TITLE OF WINDOW - $LOG\" \"c:\\usr\\bin\\tail.exe +\" -f $LOG") }

    $LOG is a variable populated with the create log file that I'm tailing. 'start' is a built-in Windows command to launch a program. Note that you'll need to supply the first argument (in the above example: \"TITLE OF WINDOW - $LOG\") if you're going to use quotes or arguments to your program; otherwise, start will interpret the args wrong.

    For information, open CMD.EXE and do:

    {C} > start /?