How can I access the Apache server configuration in a BEGIN block in mod_perl?
Question
I've been trying to switch from using PerlSetEnv to using custom configuration directives. I have my configuration module with a copy of set_val
from the docs:
sub set_val
{
local our ($key, $self, $parms, $arg) = @_;
$self->{$key} = $arg;
unless ($parms->path)
{
local our $srv_cfg = Apache2::Module::get_config($self, $parms->server);
$srv_cfg->{$key} = $arg;
}
}
...which is called by every custom directive sub. Then I have in my .conf:
PerlLoadModule MyModule::ServerConfig
MyCustomDirective 'hello'
This works fine in that httpd -t okays the file's syntax. The problem is that I can't seem to get at the value from the config file from within a BEGIN block, which I need to do.
I've tried tinkering with all sorts of things:
BEGIN
{
use Apache2::CmdParms ();
# use Apache2::Directive ();
use Apache2::Module ();
# use Apache2::ServerUtil ();
# use Apache2::RequestUtil ();
use Data::Dump;
warn ddx(Apache2::Module::get_config('MyModule::ServerConfig', Apache2::CmdParms->server));
# warn ddx(Apache2::Directive->as_hash);
# warn Apache2::ServerUtil->dir_config('MyCustomDirective);
# warn Apache2::CmdParms->server->server_hostname();
}
...but to no avail. Most of my efforts (trying to access CmdParms->server
for instance) result in Parent: child process exited with status 3221225477 -- Restarting
and an automatic restart of Apache as it says. If I pass ServerUtil->server to get_config()
, the server stays alive but the warning only prints out '1'.
I read somewhere that this is because you can't get at anything request-related within a BEGIN
block, because requests vary. It kind of makes sense, except that with PerlOptions +GlobalRequest I have been able to see $ENV
within a BEGIN
block, so why wouldn't I be able to see my own directives, just as dependent as they are on how the request happens? Especially confusing is that if I try to pass Apache2::RequestUtil->request->per\_dir\_config()
to get_config()
, it says Global $r object is not available.
If that's true in a BEGIN
block, how is it I can get at $ENV
?
Solution 2
Partly, Dump isn't being used correctly. This works better:
use Data::Dump qw(pp);
warn pp(Apache2::Module::get_config('MyModule::ServerConfig', Apache2::ServerUtil->server));
However, it doesn't show any directives that appear within <Directory> blocks.
In my particular case, though, I don't need that functionality, on second thought; that just happens to be where I had stuck them.
OTHER TIPS
Try add what you want to import
function to other module and use
this module in code where you usually put BEGIN
block. It should work same. May be it helps.