如何确定脚本是从命令行调用还是作为 CGI 脚本调用?

发布于 2024-08-15 05:43:43 字数 284 浏览 4 评论 0原文

我编写了一个脚本,可以在命令行上使用,也可以作为 CGI 脚本使用,并且需要确定如何调用该脚本,以便我可以输出 Web 请求的内容类型标头(也许还有一些反缓存)标题也是如此)。我的第一个想法是检查 http 环境变量是否存在:

my $js = build_javascript();

if ( exists $ENV{HTTP_HOST} ) {
   print "Content-type: text/javascript\n\n";
}
print $js;

有更好的方法吗?

I have a script that I wrote that can either be used on the command line or as a CGI script, and need to determine how the script was called so I can output a content-type header for web requests (and maybe some anti-cache headers too). My first thought is to check for the existance of http environment variables:

my $js = build_javascript();

if ( exists $ENV{HTTP_HOST} ) {
   print "Content-type: text/javascript\n\n";
}
print $js;

Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎歌 2024-08-22 05:43:43

根据 RFC3875(第 4.1.4 节)中的 CGI 规范,GATEWAY_INTERFACE 环境变量将为权威的东西来检查你是否在CGI上下文中运行:

4.1.4。 GATEWAY_INTERFACE

GATEWAY_INTERFACE 变量必须设置为 CGI 的方言
服务器使用它来与脚本进行通信。

According to the CGI specification in RFC3875 (section 4.1.4.), the GATEWAY_INTERFACE environment variable would be the authoritative thing to check whether you are running in a CGI context:

4.1.4. GATEWAY_INTERFACE

The GATEWAY_INTERFACE variable MUST be set to the dialect of CGI
being used by the server to communicate with the script.

想你只要分分秒秒 2024-08-22 05:43:43

确实没有什么好方法来判断您的脚本是由 Web 服务器启动还是从命令行启动。在这两种情况下都可以设置任何环境变量。例如,我经常直接从命令行运行 CGI 程序来测试它们。

要知道,如果您想选择一个环境变量来使用,它只需是您在另一种情况下不会设置的环境变量,或者是您在两种情况下都设置但赋予不同值的环境变量。在这种情况下,选择您喜欢的任何环境变量。

如果你想变得更复杂,你可以使用类似 IO::Interactive 来确定如果您连接到终端。如果不是,则 is_interactive 返回的文件句柄是一个空文件句柄,并且输出无处可去:

 print { is_interactive() } $http_header;

如果您不喜欢 IO::Interactive 决定,您可以重新实现 is_interactive。这是一段非常短的代码,并且高层接口非常好。

There's really no way good way to tell if your script was started by a web server or from the command line. Any of the environment variables can be set in both situations. I often run CGI programs straight from the command line to test them, for instance.

Knowing that, if you want to pick one environment variable to use, it just has to be one that you won't set in the other situation, or one that you set in both but give different values to. In that case, choose any environment variable that you like.

If you want to get more sophisicated, you can use something like IO::Interactive to determine if you're connected to a terminal. If you aren't, the filehanandle that is_interactive returns is a null filehandle and the output goes nowhere:

 print { is_interactive() } $http_header;

If you don't like how IO::Interactive decides, you can reimplement is_interactive. It's a very short piece of code and the higher-level interface is very nice.

稚然 2024-08-22 05:43:43

我通常在模块的开头做一些小技巧:

exit run(@ARGV) unless caller();   # run directly if called from command line

sub run
{
    process_options(@_);
    ...
}

sub process_options {
    @ARGV = @_;
    my  %opts;
    GetOptions(\%opts,
    ...
}

模块不必命名为“run”。

I usually do a little trick at the beginning of my module:

exit run(@ARGV) unless caller();   # run directly if called from command line

sub run
{
    process_options(@_);
    ...
}

sub process_options {
    @ARGV = @_;
    my  %opts;
    GetOptions(\%opts,
    ...
}

The module does not have to be named "run".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文