in reply to Launch an exe using system

It returned me an error. Please suggest.

Read the error, understand the error, fix your code (you may need to read system) :)

Use Proc::Background

Or any equivalent you're comfortable with, where the answer was also Proc::Background

Replies are listed 'Best First'.
Re^2: Launch an exe using system
by priyaviswam (Sexton) on Nov 14, 2011 at 08:43 UTC

    Thanks for your suggestion. The error is "Windows cannot find C:/program". The space in the program file is not read. So I was not able to launch the exe.

      Well, that is an error message from the shell you're using, the shell which has the start command

      You can read about it if you use the help command ( help start)

      You can learn more about shells if you read Behind the GUI lives the Shell and perlrun

      If you use Proc::Background, you don't have to concern yourself with the shell start command

Re^2: Launch an exe using system
by priyaviswam (Sexton) on Nov 14, 2011 at 08:45 UTC

    The error is "Windows cannot find C:/program. Make sure you type the name correctly, and then try again". The space in the program file is not read. So I was not able to launch the exe.

      Well, there is the clue right there: the path to the "program" you want got truncated at the space. Just as you do on the command line you need to quote parameters that include spaces and similar interesting characters. One way to do that is:

      system(qq{start "C:\\Program Files (x86)\\Microsoft LifeCam\\LifeCam.e +xe"});

      See Quote and Quote like Operators

      True laziness is hard work

      You will need to supply proper quotes to the start command:

      my $cmd = qq{start "C:\\Program Files (x86)\\Microsoft LifeCam\\LifeCa +m.exe"}; system($cmd) == 0 or die "Couldn't launch [$cmd]: $! / $?";

      Alternatively, you can use the form of system(1, ...) as documented in perlport:

      system( 1, $cmd ) ...

        Thanks of the response. I tried the following code :

        my $LifeCamLoc = "C:\\Program Files (x86)\\Microsoft LifeCam"; system('start "$LifeCamLoc"'); system('start LifeCam.exe');

      How would you handle that if you were typing start ..... on the command line? There are some Windows utilities that don't play nice with spaces in the path names, and for those you usually have to use quoting.

      That being the case, you're not looking at a Perl problem, you're looking at a "I don't know how to use my operating system." problem. That's alright. But you should at least be aware where the issue is.

      Try this (untested, but ...at least try it... ;)

      system( qq/start "C:\\Program Files (x86)\\Microsoft LifeCam\\LifeCam.exe"/ );

      Dave

        It's not so much the Windows utilities that can't handle the spaces (although that may be true), but the command line parsing that is performed by Window's equivalent of the "shell". The quoting is needed to have a command line argument that includes spaces be treated as a single argument rather than being broken into multiple arguments at the spaces. Exactly the same situation exists for *nix shells, although the quoting options are different. Oh, and various *nixen have their share of utilities that don't play well with paths containing spaces too. ;)

        True laziness is hard work