PrePAN

Sign in to PrePAN

Const::Exporter Simplified framework for defining exportable constants

Good

Synopsis

 package MyApp::Constants;

  use Const::Fast;

  const our $zoo => 1234;

  use Const::Exporter

     tag_a => [                  # use MyApp::Constants /:tag_a/;
        'foo'  => 1,             # exports "foo"
        '$bar' => 2,             # exports "$bar"
        '@baz' => [qw/ a b c /], # exports "@baz"
        '%bo'  => { a => 1 },    # exports "%bo"
     ],

     tag_b => [
        'foo',                   # exports "foo" (same as from ":tag_a")
        'moo' => \ '$bar',       # exports "moo" (same value as "$bar")
        '$zoo',                  # exports "$zoo" (as defined above)
     ],

     enums => [

       [qw/ goo gab gub /] => 0, # exports enumerated symbols, from 0..2

     ],

     default => [qw/ fo $bar /]; # exported by default

Description

Basically, this makes it easier to define exportable constants to be shared in your code.

Edit: I've looked at similar modules, and they don't do what I'm looking for (which is mainly to export a variety of types as constants, including interpolated readonly values, organised into tags).

A prototype is on GitHub

Comments

Not immediately clear what this lets me do. Can I then write the following?

use MyApp::Constants ':ports';

and by default I'll get the constants in the default array?

Might be helpful to clarify how your module compares to Exporter::Constants, Constant::Exporter and Constant::Export::Lazy? What gap in the market are you looking to fill? ;-)
@neilbowers: if you specify a tag, then you'll just get what's in the tag. If you don't specify any tags or specific constants, then you just get what's in the defaults.

I wasn't aware of the others. I'll take a look, as they may mean I don't need to write this.
So would you have to duplicate values to have multiple groups? i.e.

const_export_tags
ports => {
HTTP => ...
},
more_ports => {
HTTP => ...
};
mjemmison: you could define the values beforehand and pass the variables or references to it.

Please sign up to post a review.