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

Dear monks,

has anyone ever dealt with the following problem?

We have quite a few DBIC "business" classes installed in different environments - webserver, application server, ...

In some of these environments (webserver) we would like to strip the classes of the code which is never called in the environment.

The reason is security, we don't want application logic to be exposed on webserver.

My first (and so far the only) idea is to add some type of smart comments into the modules and preprocess them when they are installed. Since we use Module::Build subclass for the installation, this approach is usable although a bit cumbersome.

package MyApp::Schema::Request; .... #%% if IN_APP_SERVER then use MyApp::SOAPClient::SAP; #%% end if #%% if IN_APP_SERVER then sub send_to_sap { } sub workflow { ... } #%% end if

Does anyone know a better solution or the whole idea is somehow distorted?

Replies are listed 'Best First'.
Re: How to strip off parts of the code according to environment
by cdarke (Prior) on Sep 12, 2008 at 15:41 UTC
    Use the 'if' pragma. The condition must be on something which is available at compile time, unless you place it in a BEGIN block, like an environment variable, Config setting, or Perl built-in variable, for example:
    use if ($ENV{IN_APP_SERVER}), 'MyApp::SOAPClient::SAP'; use if ($^O ne 'MSWin32'), 'POSIX';
    update: corrected Homerbug typo
Re: How to strip off parts of the code according to environment
by jhourcle (Prior) on Sep 12, 2008 at 14:40 UTC

    I've written modules before that would use functionality if other modules were installed ... and it was in the module itself, so the checks were done each run, in case it got installed later. (so I could have the debugging modules on the development servers, and put them into production if necessary). Basic logic was:

    BEGIN { eval { require 'module' }; if ( $@ ) { # ... whatever extra code for when it's not installed } else { # ... whatever extra code for when it _is_ installed } }

    Of course, I was running under mod_perl, so the checks weren't run every time the module was used ... you'd have to see if the overhead is worth it for your system.

Re: How to strip off parts of the code according to environment
by Anonymous Monk on Sep 13, 2008 at 07:06 UTC
    my $foo = Foo->new( IN_APP_SERVER => FOO:IN_APP_SERVER->new() ); $foo->send_to_sap; package FOO:IN_APP_SERVER; use MyApp::SOAPClient::SAP; sub send_to_sap { } sub workflow { ... } ;