Discussion:
Custom Configuration Best Practice
David E. Wheeler
2015-03-02 01:00:25 UTC
Permalink
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] },
]);
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
David E. Wheeler
2015-03-02 01:05:59 UTC
Permalink
Post by David E. Wheeler
Fellow mod_perlers,
Oy vey, major autocomplete fail. Sorry about that, DBI folks, please ignore.

Best,

David
Steven Lembark
2015-03-31 09:25:10 UTC
Permalink
On Sun, 1 Mar 2015 17:05:59 -0800
Post by David E. Wheeler
Post by David E. Wheeler
Fellow mod_perlers,
Oy vey, major autocomplete fail. Sorry about that, DBI folks, please ignore.
At least not an insult...

If foo is called with your package followed by the original stack
then maybe you could just to the entire job in foo, leaving the
handler to return only OK:


sub foo
{
# do all of the work here
}


sub handler
{
OK
}

Short of that, I think you are stuck.

The only way to constrain $foo would be something like:

{
my $foo = '';

sub foo
{
...
}

sub handler
{
}
}

and at least $foo is scoped reasonably.

--
Steven Lembark 3646 Flora Pl
Workhorse Computing St Louis, MO 63110
***@wrkhors.com +1 888 359 3508
Loading...