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

I'm new to CGI programming but have been using Perl for many years. For some reason, I can't seem to get my Perl program to call a Windows executable file and retrieve data from it.
I've tried the following:
@junk = `test.exe`;
@junk = `c:\\test.exe`;

@junk is blank with an array length of -1 (empty).

The system command just gives me a return code of 0.

Any ideas... Thanks!
Jim
  • Comment on Calling Windows executable from a CGI Perl

Replies are listed 'Best First'.
Re: Calling Windows executable from a CGI Perl
by oko1 (Deacon) on Aug 30, 2008 at 17:10 UTC

    Couple of possibilities besides ones that have already been noted by BrowserUk and Corion:

    • You may be Suffering from Buffering - i.e., you may well be executing the program, but trying to process its output before it actually gets there.
    • Is the executable actually where you think it is? I.e., is it actually in 'C:/'?
    • Is the output of 'test.exe' going to STDOUT (which would be captured in your array) or STDERR (which would not)?

    You can cut through most of this by making definitive tests. E.g., create a CGI executable that tests a known working command (untested but should work):

    #!/usr/bin/perl -w use strict; # Enable all the warnings use CGI::Carp qw/fatalsToBrowser warningsToBrowser/; use CGI qw/:standard/; # Turn off buffering (possibly unnecessary depending on Perl version, # but can't hurt) $|++; # Get the date my $date = qx{date /t}; print header, start_html, p($date), end_html;

    Run this from the command line; then, try it via your server and browser. Based on the output, you should be able to easily decide in which direction the problem lies.

    
    -- 
    Human history becomes more and more a race between education and catastrophe. -- HG Wells
    
Re: Calling Windows executable from a CGI Perl
by Corion (Patriarch) on Aug 30, 2008 at 14:04 UTC

    Most likely, the user your webserver runs programs as does not have the right permissions. Have you looked in your webserver error logs or the Event Log? Does your program work from the command line? Does test.exe output to STDOUT or to STDERR?

    system tells you when it returns 0, which has little to do with CGI, so possibly, your program can be run, but it puts its output where you can't catch it?.

    The length of an array is usually determined by using scalar @array, and I think short of using a tied array, you can't get the length to be negative. Maybe you used $#array which is commonly referred to as "the index of the last element in @array", which is always one less than the length of the array?

Re: Calling Windows executable from a CGI Perl
by BrowserUk (Patriarch) on Aug 30, 2008 at 13:59 UTC
Re: Calling Windows executable from a CGI Perl
by jimcadd (Novice) on Aug 30, 2008 at 18:45 UTC
    U da Monks... Thanks for the replies....
    I was really stuck and you've given me some good things to check out...

    I just had a plumbing nightmare happen at the house and won't be able to try these until tomorrow.
    I'll keep you posted
    Thanks again!!!
    Jim