[Catalyst] best practice: where to put constants
Mihai Bazon
mihai at bazon.net
Thu Jun 4 18:01:31 GMT 2009
I'm doing pretty much the same thing, although I dislike the fact that
declaring/exporting constants tends to be very verbose. You have to name
those constants both in "use constant" and when declaring them in
EXPORT_OK and/or EXPORT_TAGS.
The following sample is what suits me best for now:
package TF::Constants;
use strict;
use warnings;
use base 'Exporter';
our %PERMISSIONS;
our %ERRORS;
our %EXPORT_TAGS = (
perms => [ keys %PERMISSIONS ],
errors => [ keys %ERRORS ]
);
our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
$EXPORT_TAGS{all} = \@EXPORT_OK;
### "use constant" implies a BEGIN block, so unless we declare our
### variables in a BEGIN block they won't be defined by the time
### constant::import is called. Still learning Perl quirks after 8
### years.
BEGIN {
%PERMISSIONS = (
TF_PERM_EXEC_OTHER => 1,
TF_PERM_WRITE_OTHER => 2,
TF_PERM_READ_OTHER => 4,
TF_PERM_EXEC_GROUP => 8,
TF_PERM_WRITE_GROUP => 16,
TF_PERM_READ_GROUP => 32,
TF_PERM_EXEC_USER => 64,
TF_PERM_WRITE_USER => 128,
TF_PERM_READ_USER => 256,
TF_PERM_DEFAULT => 0664, # rw-rw-r--
);
%ERRORS = (
TF_ERR_NOPERMS => 'NOPERMS',
);
}
use constant \%PERMISSIONS;
use constant \%ERRORS;
1;
That is in short: declare some global (our) hashes, that contain your
constants. Then define them in a BEGIN block. Then pass reference to
those hashes to use constant and finally, use some map/keys tricks to
export them automatically (not having to list each name again is a big
win for my lazy ass).
Cheers,
-Mihai
PS: yes this is totally unrelated to Catalyst. :-)
Tomas Doran bobtfish at bobtfish.net wrote:
> 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 { THINGFOO => 0, THINGBAR => 1, };
>
> our @EXPORT = qw( &THINGFOO &THINGBAR );
>
> then just 'use MyApp::Constants;' where you need them, job done..
>
> Cheers t0m
>
> ------------------------------------------------------------------------
>
> 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/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20090604/4775c76d/attachment.htm
More information about the Catalyst
mailing list