http://www.perlmonks.org?node_id=655678

olus has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm trying to read a variable, that was set on the apache2 conf with PerSetVar, on a PerlPostReadRequestHandler, but so far all my tries failed.
I do see the variable when accessing it from the requested page.

A snipet of the post read request handler code:
... sub handler { my $r = shift; my $var = $r->dir_config('var'); $ENV{'VAR'} = $var; $ENV{'AM_I_SET'} = 'Yes'; return Apache2::Const::OK; }
When printing the %ENV I see AM_I_SET as 'Yes' but VAR is undef.

On the requested page I have:
my $r = shift; # template initialization not shown my $var = $r->dir_config('var'); $vars{'message'} = "Var is: $var"; $vars{'message'} .= '<br />'.Dumper(\%ENV); $template->process("hello.html", \%vars) || die $template->error();
The resulting web page will correctly display the value of 'var' and undef on %ENV.

I've been browsing the mod_perl2 docs, the Practical mod_perl book and searching the monastery and the web, but still haven't found the solution.

So, is it possible to access variables set with PerlSetVar from within the early handlers (those other than the final script)? How? What am I not doing?

--
olus

Replies are listed 'Best First'.
Re: PerlSetVar and PerlPostReadRequestHandler
by perrin (Chancellor) on Dec 07, 2007 at 21:33 UTC
    That phase is too early. At that point, the request has not been resolved to a directory, so dir_config can't return anything. Make your code a PerlFixupHandler instead.
Re: PerlSetVar and PerlPostReadRequestHandler
by redhotpenguin (Deacon) on Dec 07, 2007 at 19:34 UTC

    In your httpd.conf, try adding the following lines:

    PerlPassEnv VAR PerlPassEnv AM_I_SET
      I did try the PerlPassEnv just to see if things worked, and they did.
      I did manage to get the values from the %ENV.

      The thing is, I would rather not have the VAR on the environment, since every once in a while someone dumps the environment thus leaving things less safe.
      Thought PerlSetVar would be a better approach.

      --
      olus

        I don't understand what you mean when you say 'someone dumps the environment'. You could use 'SetHandler modperl' instead of 'SetHandler perl-script' in httpd.conf, it keeps %ENV from being reset between requests IIRC