Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

calling external program from CGI

by stan131 (Acolyte)
on May 07, 2009 at 08:40 UTC ( #762520=perlquestion: print w/ replies, xml ) Need Help??
stan131 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I am writing my first CGI program and having issues calling external programs from CGI program. I have captured Query in CGI program. Now I need to call external perl program say test_process.pl passing the Query parameter. test_process.pl takes Query as parameter does its processing and needs to return an array.
Here is what I have tried:
test_process.pl \"$myQuery\";
Since test_process.pl is a program, not sure if it can return array back. Do I need to call a subroutine within test_process.pl ?
My current approach is test_process.pl writes the array using Storable and I retrieve the array in CGI script. But as you can see I am not able to call test_process.pl in the first place.
Where am I going wrong?
Appreciate your help.
Thanks,
Stan

Comment on calling external program from CGI
Download Code
Re: calling external program from CGI
by Anonymous Monk on May 07, 2009 at 08:51 UTC
      Monk - Yes my program starts with #!/usr/bin/perl -w. I am also able to capture the Query. But I need inputs on calling external programs.
      Thanks
        #!/usr/bin/perl -T called taint mode is different from #!/usr/bin/perl -w called warnings.
Re: calling external program from CGI
by ikegami (Pope) on May 07, 2009 at 09:59 UTC

    Seriously? test_process.pl is not a Perl function, and \"$myQuery\" isn't even close to valid Perl either.

    my $output = `command`;
      Ikegami,
      I am newbie to CGI and still not able to what am I doing wrong. I have a perl program that takes parameter and generates an array. Now I need to call this program from my .cgi file and retrieve the array. Since program cannot return(only subroutines can), how do I invoke this from CGI and fetch the array.
        Processes communicate via file handles, and file handles can only transmit bytes (not Perl arrays). You need to serialize the array somehow. I thought you already addressed that issue using Storable?
Re: calling external program from CGI
by scorpio17 (Monsignor) on May 07, 2009 at 13:29 UTC

    When you point a browser at a CGI script, the script does not run as YOU, but as whatever user the web server is running as - usually 'nobody' or 'www-user' or 'apache'. This frequently causes permission problems, because the 'apache' user (for example) may not be able to read/write files in a particular directory that you can, etc.

    So, if you have a script that YOU can run, but that fails to run under CGI, it's probably due to permission problems.

    A better solution might be to rewrite your external program so that all the work is contained inside a subroutine, and put this inside a module. Then, your external program can 'use' this module, call the sub, and everything work the same as before, BUT, now the CGI can also 'use' the same module, and call the sub too (no external calls necessary). Since you want to "pass an array" this makes more sense (although if it's going to contain many elements, a REFERENCE to an array will be more efficient than the whole array).

    If you're new to perl, I suggest you do a little more reading first - learn about subroutines, modules, references, etc. CGI scripts are hard to get right. There are MANY security issues involved. Read all the FAQs and tutorials on this site, just for starters. Good luck.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://762520]
Approved by targetsmart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-12-26 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls