David E. Wheeler
2015-03-02 01:00:25 UTC
Fellow mod_perlers,
I followed the instructions in the Server Configuration Customization guide to create a custom Apache configuration directive like so:
my $foo;
sub foo { $foo = $_[2] }
Apache2::Module::add(__PACKAGE__, [
{ name => 'MyFoo', func => __PACKAGE__ . '::foo' },
]);
sub handler {
my $r = shift;
$r->print($foo);
return OK;
}
This works pretty well; I can now set the MyFoo directive in my httpd.conf. But it feels a little hinky to set up package globals that get set on every request like this. It feels like it would make more sense if they were getting passed to an object method, but foo() is called as a function here; the first parameter is the class name, not an object. Also, I tried to use an anonymous sub in the add call:
my $foo;
Apache2::Module::add(__PACKAGE__, [
{ name => 'MyFoo', func => sub { $foo = $_[2] },
]);
So none of this feels quite right to me. Anyone got a point to best practices for setting up custom configuration directives? Or is this close to what everyone does anyway?
Thanks,
David
I followed the instructions in the Server Configuration Customization guide to create a custom Apache configuration directive like so:
my $foo;
sub foo { $foo = $_[2] }
Apache2::Module::add(__PACKAGE__, [
{ name => 'MyFoo', func => __PACKAGE__ . '::foo' },
]);
sub handler {
my $r = shift;
$r->print($foo);
return OK;
}
This works pretty well; I can now set the MyFoo directive in my httpd.conf. But it feels a little hinky to set up package globals that get set on every request like this. It feels like it would make more sense if they were getting passed to an object method, but foo() is called as a function here; the first parameter is the class name, not an object. Also, I tried to use an anonymous sub in the add call:
my $foo;
Apache2::Module::add(__PACKAGE__, [
{ name => 'MyFoo', func => sub { $foo = $_[2] },
]);
The func attribute expects a reference to a function or a function name.
https://perl.apache.org/docs/2.0/user/config/custom.html#C_func_So none of this feels quite right to me. Anyone got a point to best practices for setting up custom configuration directives? Or is this close to what everyone does anyway?
Thanks,
David