Catalyst Tip: Don't pollute!

LTjake on 2007-04-20T19:49:35

Currently, your "MyApp.pm" file is both your application class and your context class (NB: this is expected to change in 5.8000). We've been slowing suggesting that people move things out of th context/app class so as not to pollute it with an abundance of mehods which may occasionally have unwanted consequences -- for example "login : Global {}" conflicting with the Authentication plugin's login() method.

This tip is broken into two parts:

  1. It's been said time and time again (c.f. this and this [phaylon++]), not everthing has to be a plugin! If the sum total of your plugin is this:

    package Catalyst::Plugin::Foo;
    
    use Foo;
    
    sub foo {
        my( $c, @rest ) = @_;
        return Foo->new( @rest );
    }
    
    You should reconsider. Either use the module directly, or make a controller base class. That should handle most cases.
  2. Be careful what you import into MyApp.pm! Some modules will export methods (and other symbols) by default, and sometimes you'll do it manually. Consider explicitly importing nothing:

    before:

    package MyApp;
    
    use Digest::MD5 qw(md5_hex); # MyApp now has the md5_hex method
    
    sub foo {
        # ...
        return md5_hex( $string );
    }
    
    after:

    package MyApp;
    
    use Digest::MD5 (); # no imports
    
    sub foo {
        # ...
        return Digest::MD5::md5_hex( $string );
    }
    
    If you want a quick-n-easy cleanup, try namespace::clean.