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

hey guys! i'm just wondrin how to execute a C program through the CGI. I already used system then it executed but i want to get its output and print it to another webpage.

Replies are listed 'Best First'.
Re: execute a C program through PerlCGI
by thinker (Parson) on Aug 26, 2002 at 07:50 UTC
    Hi,

    If your C program is called c_program, just use the following.
    my $output=qx/c_program/;
    system() returns the exit status from a command, not it's output. See perldoc -f system.

    cheers

    thinker
Re: execute a C program through PerlCGI
by spurperl (Priest) on Aug 26, 2002 at 10:58 UTC
    Unless you want to communicate via files (not adviced), usually pipes are used for this purpose. The less painful method, however, is to use the backticks (`) on a command, that transparently create an anonymous pipe and return the output to your script.

    my $output = `path/program_name`;

    qx// (Quoted eXecution) is an alternative to the backtics

Re: (nrd) execute a C program through PerlCGI
by newrisedesigns (Curate) on Aug 27, 2002 at 02:46 UTC

    Considering you might not have access to modify that program's source code or might not know exactly what the code in the program will do, I suggest not only using the qx method, but also untainting any data you might also pass to the program.

    CGI is always tricky. Users will find every vulnerability, eventually. Don't use backticks, unless everything in between them is in no way modifiable. Same with qx. Be wary of user-submitted input. Hope this helps.

    John J Reiser
    newrisedesigns.com