views:

173

answers:

2

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?

A: 

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.

Hynek -Pichi- Vychodil
Sorry, I'm not sure exactly what you mean...
Kev
when you wrote `use Module;` it works like `BEGIN {require Module; Module->import();}` Instead using `BEGIN` block you can make `Module` with work what you want inside `BEGIN` block in module body or its import function. Its more worth approach than using `BEGIN` block. Less magic.
Hynek -Pichi- Vychodil
Oh, okay. I didn't know that...thanks!
Kev
A: 

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.

Kev