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

Monks... I have been tryng to use the system() command to call an external process and pass parameters to it as well.
the line of code looks something like this :-
... system("C:\\perlscripts\\foo.exe -c $color -f $fromNode -t $textmsg");

this does not seem to work. I can run the exe outside of the script and it works.
Any tips on calling programs in perl on windows ?
Cheers
Danny
p.s. I have read the perldoc for system and done searching as well.

Replies are listed 'Best First'.
Re: call exe on windows via script
by Tanalis (Curate) on Jul 08, 2005 at 13:09 UTC
Re: call exe on windows via script
by radiantmatrix (Parson) on Jul 08, 2005 at 13:19 UTC

    Try separating your command and parameters, (see the docs for the system() call), like this:

    system('C:\\perlscripts\\foo.exe', '-c', $color, '-f', $fromNode, '-t' +, $textmsg);
    This helps because Perl then takes care of any quoting and shell-escaping needed for your system's shell.

    If that fails, you'll want to check the special variables $! and $? (their use for this is explained in the docs for system()). For further debugging, you'll want to post the results, as well as any warnings generated during your run (you are using strict and warnings, right?).

    Larry Wall is Yoda: there is no try{}
    The Code that can be seen is not the true Code
      I tried your method but it didn't work.
      I am now getting an exit code of 256 from the system call.

      I am not using strict or warnings. Can I just use warnings and not have to use strict yet ?

      Thanks

        The reason using strict and warnings is useful is that it helps to ensure that the problem you're having is not actually a problem somewhere else in your script. Making your scripts work with both strict and warnings turned on means that if you are still having the same problem, you've isolated it properly. Otherwise you're shooting in the dark a bit.

        Also, it seems as though you didn't read the perlfunc secion on system() as I suggested, or you would have pasted output from a block like this:

        if ($? == -1) { print "failed to execute: $!\n"; } elsif ($? & 127) { printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; } else { printf "child exited with value %d\n", $? >> 8; }

        That is a very good and reliable way to find the "real" exit code. It may be that your application is being called correctly, but when called from Perl doesn't have access to the same environment, or a myriad other possibilities. When foo.exe exits, you can read it's docs to find the meaning of that exit code and thereby find the problem.

        However, until your code works with strict and warnings, you are probably just wasting debugging time.

        Larry Wall is Yoda: there is no try{}
        The Code that can be seen is not the true Code