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

Hello Dear Monks,

I am trying to make a system call from perl:

#! C:\perl\bin\perl.exe my $file1 = "test1.txt"; system ( "check.bat > C:\\$file1 2>&1" );

The above system call does not seem to execute. But, when I type out the same command on dos prompt, it executes as expected. What might be wrong with the code above?

Thank you in advance.

Replies are listed 'Best First'.
Re: system calls
by crashtest (Curate) on Apr 22, 2005 at 22:09 UTC
    I surmise from the top line of your code that you are running your script through Apache and its emulation of the UNIX shebang line for Windows. If so, I'd try the following two things:
    1. Try running this script from the command line instead of through Apache. That is, run perl yourscript.pl and see if that works. If it does, that should be a clue that the problem stems from the Apache environment. You could then Google / Super Search specifically for this problem as it relates to Apache.
    2. I have seen in other nodes recommendations of executing system commands by explicitly invoking cmd.exe. So try something like:
      system("cmd.exe /c check.bat > C:\\$file1 2>&1");
      or even
      # Check this is the correct path to cmd.exe on your system system("C:\\WINDOWS\\system32\\cmd.exe /c check.bat > C:\\$file1 2>&1" +);
    I hope this helps. I had no problems executing a simple batch file using your syntax from my Windows XP SP2 command line.
Re: system calls
by phaylon (Curate) on Apr 22, 2005 at 20:49 UTC
    Hi there. First, there's no reason not to use
    use warnings; use strict;
    For your problem: You might want to read up the perldoc of system, where the following is statet "Return value of -1 indicates a failure to start the program (inspect $! for the reason)."

    Ordinary morality is for ordinary people. -- Aleister Crowley
      Thanks for the suggestion. When I turned on warnings, it gives me the following error:

      Can't spawn "cmd.exe": No such file or directory

      Does that mean system calls are not accepted?

        I would try crashtest's Hint for specifying the absolute path to cmd.exe. I'm sorry but this is where my windows knowledge ends.

        Ordinary morality is for ordinary people. -- Aleister Crowley

        It's been many, many years since I've had to actively support windows, but from what I remember, the two different Window codebases (the NT derivitives, vs. those that derived from DOS) used different calls to get to a shell. One of them used cmd while the other used command. (I have no idea what the file extensions were ... they might've been exe, com, or any of the other executable extensions in windows ... I also can't remember which was which)

        So, when one doesn't work -- try the other one. (if you had mentioned what version of Windows you were using, someone might be able to elliminate this as having been the problem).

Re: system calls
by bofh_of_oz (Hermit) on Apr 22, 2005 at 20:48 UTC
    I might be wrong, but I found that system calls under DOS/Win do not work well when the string to execute in in the brackets of sys. Here is my workaround:

    #! C:\perl\bin\perl.exe my $file1 = "test1.txt"; my $cmd = "check.bat > C://$file1 2>&1"; system ( $cmd );
    Hope this helps.

    Update: Also, even in Windows, use // instead of \\

    ------------------------------------------
    An idea is not responsible for the people who believe in it...
      Good to know. I thought about that first, but wasn't sure if windows can redirect it's error output like the bash.

      Ordinary morality is for ordinary people. -- Aleister Crowley
      Hello,

      Thanks for the suggestion. I went ahead and tried that. It did not seem to help.