title: How to pass environment variable to an AutoLoaded mod_perl handler, to be used at module load time?
body: <p>I have a HTTP Request Handler for mod_perl which needs to read an environment variable, from <code>%ENV</code>, at module load time. The environment variable is passed from the Apache config into mod_perl using the <a href="http://perl.apache.org/docs/2.0/user/config/config.html#C_PerlSetEnv_" rel="nofollow"><code>PerlSetEnv</code></a> directive.</p>
  
  <p>This worked fine, until we changed the Apache configuration to <a href="http://perl.apache.org/docs/2.0/user/config/config.html#C_AutoLoad_" rel="nofollow">AutoLoad</a> the handler at startup time, for performance reasons. When the module is AutoLoaded like this, the<code>PerlSetEnv</code> does not take effect at module load time, and the variable we need is only available from <code>%ENV</code> at request time inside the handler method.</p>
  
  <p><strong>Is there a way to continue using AutoLoad, but still set an environment variable in the Apache config which is available in Perl's <code>%ENV</code> at module load time?</strong></p>
  
  <h2>Minimal example:</h2>
  
  <p>Here's a stripped down test-case to illustrate the problem.</p>
  
  <p>The Apache config <strong>without</strong> autoload enabled:</p>
  
  
  
  <pre><code>PerlSwitches -I/home/day/modperl
  &lt;Location /perl&gt;
      SetHandler modperl
      PerlSetEnv TEST_PERLSETENV 'Does it work?'
      PerlResponseHandler ModPerl::Test
      Allow from all
  &lt;/Location&gt;
  </code></pre>
  
  <p>Contents of /home/day/modperl/ModPerl/Test.pm:</p>
  
  
  
  <pre><code>package ModPerl::Test;
  
  use strict;
  use warnings;
  
  use Apache2::RequestRec ();
  use Apache2::RequestIO ();
  use Apache2::Const qw(OK);
  
  my %ENV_AT_MODULE_LOAD = %ENV; # Take a copy
  
  sub handler {
      my $r = shift;
      $r-&gt;content_type('text/plain');
  
      $r-&gt;print("ENV:\n");
      foreach my $key (sort keys %ENV) {
          $r-&gt;print("  $key: $ENV{$key}\n");
      }   
  
      $r-&gt;print("ENV_AT_MODULE_LOAD:\n");
      foreach my $key (sort keys %ENV_AT_MODULE_LOAD) {
          $r-&gt;print("  $key: $ENV_AT_MODULE_LOAD{$key}\n");
      }   
  
      return OK;
  }   
  1;
  </code></pre>
  
  <p>When localhost/perl is viewed in the browser, I see this:</p>
  
  
  
  <pre><code>ENV:
    MOD_PERL: mod_perl/2.0.5
    MOD_PERL_API_VERSION: 2
    PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    TEST_PERLSETENV: Does it work?
  ENV_AT_MODULE_LOAD:
    MOD_PERL: mod_perl/2.0.5
    MOD_PERL_API_VERSION: 2
    PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    TEST_PERLSETENV: Does it work?
  </code></pre>
  
  <p><em>Hooray!</em> <code>TEST_PERLSETENV</code> is available at module load time, as we want.</p>
  
  <p><strong>But</strong> when we change the Apache config to enable Autoload (by using + in the PerlResponseHandler like so):</p>
  
  
  
  <pre><code>    PerlResponseHandler +ModPerl::Test
  </code></pre>
  
  <p>I get the following output instead:   </p>
  
  
  
  <pre><code>ENV:
    MOD_PERL: mod_perl/2.0.5
    MOD_PERL_API_VERSION: 2
    PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    TEST_PERLSETENV: Does it work?
  ENV_AT_MODULE_LOAD:
    MOD_PERL: mod_perl/2.0.5
    MOD_PERL_API_VERSION: 2
    PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  </code></pre>
  
  <p><em>Boo!</em> <code>TEST_PERLSETENV</code> is no longer available at module load time :( How can I get it back while keeping the AutoLoad behaviour?</p>