如何将环境从 Python Web 应用程序传递到 Perl 程序?

发布于 2024-07-25 15:42:41 字数 568 浏览 4 评论 0原文

如何设置 Perl 的 %ENV 以将 Perl 脚本引入到我的 Web 应用程序的上下文中?

我有一个网站,用不同于 Perl 的语言 (Python) 编写。 但是我需要使用 Perl 应用程序,它由 .pl 文件组成:

    #!/usr/bin/env perl
    "$ENV{DOCUMENT_ROOT}/foo/bar.pm" =~ /^(.+)$/;
    require $1;
    my $BAR = new BAR(
        user    => 'foo',
    );
    print $bar->get_content;

... 和模块 bar.pm,它依赖于 "$ENV{HTTP_HOST}", "$ ENV{REQUEST_URI}", “$ENV{REMOTE_ADDR}”“$ENV{DOCUMENT_ROOT}”

我应该如何设置这个哈希值? 这是我第一次使用 Perl,所以我可能会错过一些非常明显的东西:)

How do I set Perl's %ENV to introduce a Perl script into the context of my web application?

I have a website, written in a language different from Perl (Python). However I need to use a Perl application, which consists of a .pl file:

    #!/usr/bin/env perl
    "$ENV{DOCUMENT_ROOT}/foo/bar.pm" =~ /^(.+)$/;
    require $1;
    my $BAR = new BAR(
        user    => 'foo',
    );
    print $bar->get_content;

... and a module bar.pm, which relies on "$ENV{HTTP_HOST}", "$ENV{REQUEST_URI}",
"$ENV{REMOTE_ADDR}" and "$ENV{DOCUMENT_ROOT}".

How should I set this hash? This is my very first experience with Perl, so I may be missing something really obvious here :)

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

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

发布评论

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

评论(2

半葬歌 2024-08-01 15:42:41

如果您从 Python 代码生成 Perl 进程(而不是“直接从 Web 服务器”),则有多种方法可以从 Python 父进程环境设置子进程环境,具体取决于您使用的子进程环境。 “产卵”。

例如,如果您使用的是 subprocess.Popen,则可以将 env= 参数集传递给您想要的字典,如 文档 解释:

如果 env 不是 None,则它必须是
定义环境的映射
新流程的变量; 这些
使用而不是继承
当前进程的环境,即
默认行为。

If you're spawning that Perl process from your Python code (as opposed to "directly from the webserver"), there are several ways to set the child process environment from the Python parent process environment, depending on what you're using for the "spawning".

For example, if you're using subprocess.Popen, you can pass an env= argument set to the dictionary you desire, as the docs explain:

If env is not None, it must be a
mapping that defines the environment
variables for the new process; these
are used instead of inheriting the
current process’ environment, which is
the default behavior.

世界和平 2024-08-01 15:42:41

Perl 的特殊 %ENV 哈希是环境的接口。 (在幕后,它根据需要调用 getenvputenv。)

例如:

$ cat run.sh 
#! /bin/bash

export REMOTE_ADDR=127.0.0.1

perl -le 'print $ENV{REMOTE_ADDR}'

$ ./run.sh 
127.0.0.1

您的 Web 服务器应该设置这些环境变量。

Perl's special %ENV hash is the interface to the environment. (Under the hood, it calls getenv and putenv as appropriate.)

For example:

$ cat run.sh 
#! /bin/bash

export REMOTE_ADDR=127.0.0.1

perl -le 'print $ENV{REMOTE_ADDR}'

$ ./run.sh 
127.0.0.1

Your web server ought to be setting these environment variables.

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