Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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 passing the Query parameter. takes Query as parameter does its processing and needs to return an array.
Here is what I have tried: \"$myQuery\";
Since is a program, not sure if it can return array back. Do I need to call a subroutine within ?
My current approach is writes the array using Storable and I retrieve the array in CGI script. But as you can see I am not able to call in the first place.
Where am I going wrong?
Appreciate your help.

Replies are listed 'Best First'.
Re: calling external program from CGI
by ikegami (Pope) on May 07, 2009 at 09:59 UTC

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

    my $output = `command`;
      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 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.
        #!/usr/bin/perl -T called taint mode is different from #!/usr/bin/perl -w called warnings.
Re: calling external program from CGI
by scorpio17 (Abbot) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://762520]
Approved by targetsmart
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2018-06-24 04:20 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.