tags:

views:

52

answers:

2

Hi,

I am suppose to write perl-CGI script, which supports CLI & CGI mode. And I am unable to find the way to distinguish between two mode. I am looking for the way to check for mode whether it is CLI or CGI ?

So could you please let me know how to implement the logic?

Thanks in advance.

+6  A: 

You can check for the presence of any number of CGI environment variables, e.g.:

if ($ENV{GATEWAY_INTERFACE})
{
      print "Content-type: text/plain\n\nLooks like I'm a CGI\n";
}
else
{
      print "I'm just a plain command line program\n";
}
Ether
Thank you very much...
Bhavik
This is a bad resource, it does not list the mandatory [`GATEWAY_INTERFACE`](http://www.faqs.org/rfcs/rfc3875.html#4.1.4.).
daxim
@daxim: thanks; I've replaced the link with a better resource.
Ether
+2  A: 

At a guess, $ENV{'GATEWAY_INTERFACE'} will be NULL when run from the command line, and contain something (e.g. 1.1) when run as a CGI.

You'll need to try it out.

Pete
Err, why have I been voted down? My suggestion doesn't sound too ridiculous. Could have least have left a comment you coward...
Pete
Upvote for you, you have the better answer in terms of robustness and expressing the intention to the maintenance programmer.
daxim
@daxim Thanks, was beginning to doubt my albeit very rusty Perl knoweldge.
Pete