Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Journal of LTjake (4001)

Friday April 20, 2007
02:49 PM

Catalyst Tip: Don't pollute!

[ #33051 ]

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.