views:

88

answers:

2

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 :)

+2  A: 

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.

Greg Bacon
+3  A: 

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.

Alex Martelli