如何区分CLI和Perl中的CGI模式

问题描述:

我应该编写一个Perl脚本,该脚本既可以在命令行中运行,也可以作为CGI脚本运行。我无法确定如何区分这两种模式。

I am supposed to write a Perl script which can be run both on the command line and as a CGI script. I haven't been able to determine how I should distinguish between the two modes.

那么您能不能让我知道如何实现逻辑?

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

您可以检查是否存在任意数量的 CGI环境变量,例如:

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";
}