[Catalyst] best practice: where to put constants

John Napiorkowski jjn1056 at yahoo.com
Thu Jun 4 17:09:20 GMT 2009




--- On Thu, 6/4/09, Tomas Doran <bobtfish at bobtfish.net> wrote:

> From: Tomas Doran <bobtfish at bobtfish.net>
> Subject: Re: [Catalyst] best practice: where to put constants
> To: "The elegant MVC web framework" <catalyst at lists.scsys.co.uk>
> Date: Thursday, June 4, 2009, 11:31 AM
> Jarom Smith wrote:
> > In the end, this is what I decided to do because I
> have relatively few of these guys (so far) and I'd rather
> have them thrown together all in one place than spread all
> over the system.  But I'm wondering if there is a best
> practice?
> > 
> > I don't want to put them in the config hash or in a
> configuration file
> > because these are not things that a user should be
> able to change or override.
> 
> Not sure this is the 'best practice' way of doing what
> you're trying to achieve, but that aside - to answer your
> actual question:
> 
> I'd just put them all into their own package, and arrange
> for them to be exportable, something like this:
> 
> package MyApp::Constants;
> use strict;
> use warnings;
> use Exporter qw/import/;
> 
> use constant {
>     THING_FOO => 0,
>     THING_BAR => 1,
> };
> 
> our @EXPORT = qw(
>     &THING_FOO
>     &THING_BAR
> );
> 
> then just 'use MyApp::Constants;' where you need them, job
> done..
> 
> Cheers
> t0m
> 

I do basically the same thing, except I use Sub::Exporter instead.  Then I have a MyApp::Constants to go along with my MyApp::Types, which contain all my MooseX::Type based type constraints.

john

> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
> 


      



More information about the Catalyst mailing list