Comment 3 for bug 44019

Revision history for this message
Robin Smidsrød (robinsmidsrod) wrote :

Gary: I don't use the code "local %ENV", if that's what you're asking, but I DO fetch variables from %ENV to determine what kind of environment I'm in. I do not use the Sys::HostIP package.

I'm using the code below (I've cut out the lines that mentions ENV):

<code>
$pf->var('docroot',$ENV{'DOCUMENT_ROOT'} . $uri->path);
my $ip_addr=inet_ntoa(inet_aton($q->remote_host)) || $ENV{'REMOTE_ADDR'};
my $ip_addr=inet_ntoa(inet_aton($q->remote_host)) || $ENV{'REMOTE_ADDR'};
</code>

I also use the following snippet of code to identify if I'm running CGI or mod_perl mode:

<code>
    if ($ENV{'MOD_PERL'}) {
        eval "use Apache2::ServerUtil ();";

        if ($@) {
            die __"Apache2/ServerUtil.pm unavilable. Please verify your mod_perl 2.x setup.";
        }

        if (Apache2::ServerUtil->server->dir_config('PORTFOLIO_HOME')) {
            $homedir=Apache2::ServerUtil->server->dir_config('PORTFOLIO_HOME');
            $bindir=$homedir . '/bin';
        } else {
            die __"Please set PORTFOLIO_HOME when using mod_perl.";
        }
        $hosting_environment=$ENV{'GATEWAY_INTERFACE'} . "; " . $ENV{'MOD_PERL'};
    } elsif ( $ENV{'GATEWAY_INTERFACE'}=~/^CGI\/.*$/ and $ENV{'SCRIPT_FILENAME'}) {
        (my $dummy,$bindir,my $dummy2)=File::Spec->splitpath($ENV{'SCRIPT_FILENAME'}); # Split filename from directory
        if ( -d $bindir) {
            $bindir=~s/^(.*)$/$1/;
            $homedir=$bindir . '/../';
        }
        $hosting_environment=$ENV{'GATEWAY_INTERFACE'};
    } else {
        $bindir=$FindBin::Bin;
        # Untaint directory if it's valid
        if ( -d $bindir) {
            $bindir=~s/^(.*)$/$1/;
            $homedir=$bindir . '/../';
        }
        $hosting_environment='CLI';
    }
</code>

I'm also using this code in /etc/apache2/mods-enabled/perl.conf:

<code>
# Make sure files with .pl extension are run as mod_perl scripts

AddHandler perl-script .pl

# Make sure these point to the base folder of the application

PerlSetVar PORTFOLIO_HOME "/home/portfolio/portfolio"
PerlSwitches -w -I/home/portfolio/portfolio/lib

# Portfolio is a full ModPerl::Registry script.

PerlOptions +ParseHeaders
PerlOptions +SetupEnv
PerlResponseHandler ModPerl::Registry

# Automatic reload check of changed modules at each request.
# Useful in development, but shouldn't be used in production.

PerlInitHandler Apache2::Reload
</code>