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

I use a module in a handler. I use one of the module's functions in the handler. Without checking environment variables and passing arguments to the function, is there any other way that the function can be aware that it is running under apache? Could I somehow check for the apache request object?

Replies are listed 'Best First'.
Re: mod_perl handler detection
by bmann (Priest) on Aug 13, 2005 at 08:19 UTC
    Without checking environment variables

    What's wrong with checking environment variables? If mod_perl is enabled, $ENV{ MOD_PERL } will be set to "mod_perl/VERSION" (in my case "mod_perl/1.29").

Re: mod_perl handler detection
by adrianh (Chancellor) on Aug 13, 2005 at 08:18 UTC
    is there any other way that the function can be aware that it is running under apache?

    If you want to know whether you're running under mod_perl you can check for $ENV{MOD_PERL} being set (see the mod_perl documentation for details).

Re: mod_perl handler detection
by cees (Curate) on Aug 13, 2005 at 14:14 UTC

    Just a general comment on asking questions here. If you know that the standard way of checking for mod_perl is to test for an environment variable ($ENV{MOD_PERL}), and then you specifically state that you do not want to look at environment variables, then at least give a reason why you don't want to use environment variables. This is more a suggestion for future questions, then a critisism.

    Otherwise you will just get a bunch of responses that (correctly so) ask you why you don't want to do things the standard way.

    This of course assumes that you know about checking for $ENV{MOD_PERL}, but since you specifically mentioned environment variables, I guess that you do.

    As for checking the apache request object to see if mod_perl is being used, I guess you could use eval and check for a failure.

    my $r = eval { Apache->instance }; if ($@) { # mod_perl probably not available }

    But checking the environment variable is much much cleaner.

    if (!$ENV{MOD_PERL}) { # mod_perl is not available }