perlcatalyst

Loading modules upon config variables on a Perl Catalyst application


I'm setting an Catalyst webapp for deploy and would like to remove the -Debug and StackTrace Catalyst plugins from my app.pm in production environment. However, since I keep a testing environment, I'd like to avoid "hardcoding" this. Instead, I'd like to have a config variable defining the environment and load the modules accordingly. All source is on SVN and it would get messy if I were to update the repo and then manually change the modules I load in the app.pm.

The app currently uses configLoader module to read a local_app.yml file with all configs, which is unversioned and allows some control over the way the app works in either dev, test or prod environments; but these are only read after the application is started (hence, after the modules have been loaded).

Here's a simplification of the app.pm:

package app;

use strict;
use warnings;
use Hash::Merge ();
use Sys::Hostname;

use Catalyst::Runtime '5.80';

use Catalyst qw/    
  -Debug
  StackTrace
  ConfigLoader
  SomeOtherModule
  AndYetAnotherOne
/;

__PACKAGE__->config(

  # Some local config stuff

);

# Start the application
__PACKAGE__->setup();

Is there a way to do this without having to use a Unix command to find out which host is running the app?


Solution

  • One way to control how this runs is via the environment. From Catalyst

    -Debug

    Enables debug output. You can also force this setting from the system environment with CATALYST_DEBUG or _DEBUG. The environment settings override the application, with _DEBUG having the highest priority.

    Then you can set things up, via a driver for example, so to be able to first set the environment variable, while by default have it off for production runs. This should be possible to automate as needed. Then StackTrace plugin follows suit since

    This plugin is only active in -Debug mode by default, [...]

    and so the environment variable can take care of both.

    Another option is to list the ConfigLoader plugin first, as its docs say in Synopsis

    package MyApp;
    
    # ConfigLoader should be first in your list so
    # other plugins can get the config information
    use Catalyst qw( ConfigLoader ... );
    

    Then you should be able to control StackTrace plugin, and -Debug mode-or-not, separately.