Ævar Arnfjörð Bjarmason > Constant-Export-Lazy-0.01 > Constant::Export::Lazy

Download:
Constant-Export-Lazy-0.01.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

Open  0
View/Report Bugs
Module Version: 0.01   Source   Latest Release: Constant-Export-Lazy-0.08

NAME ^

Constant::Export::Lazy - Utility to write lazy exporters of constant subroutines

DESCRIPTION ^

This is a utility to write lazy exporters of constant subroutines. I.e. it's not meant to be a user-facing constant exporting API, it's meant to write user-facing constant exporting APIs.

There's dozens of similar constant defining modules on the CPAN, why did I need to write this one?

SYNOPSIS ^

So how does all this work? This example demonstrates all our features. This is an example of your My::Constants package that you write using Constant::Export::Lazy:

    package My::Constants;
    use strict;
    use warnings;
    use Exporter 'import';
    use constant {
        X => -2,
        Y => -1,
    };
    our @EXPORT_OK = qw(X Y);
    use Constant::Export::Lazy (
        constants => {
            A => sub { 1 },
            B => sub { 2 },
            SUM => sub {
                # You get a $ctx object that you can ->call() to retrieve
                # the values of other constants if some of your constants
                # depend on others. Constants are still guaranteed to only
                # be fleshened once!
                my ($ctx) = @_;
                $ctx->call('A') + $ctx->call('B'),
            },
            # We won't call this and die unless someone requests it when
            # they import us.
            DIE => sub { die },
            PI  => {
                # We can also supply a HashRef with "call" with the sub,
                # and "options" with options that clobber the global
                # options.
                call    => sub { 3.14 },
                options => {
                    override => sub {
                        my ($ctx, $name) = @_;
                        # You can simply "return;" here to say "I don't
                        # want to override", and "return undef;" if you
                        # want the constant to be undef.
                        return $ENV{PI} ? "Pi is = $ENV{PI}" : $ctx->call($name);
                    },
                },
            },
        },
        options => {
            # We're still exporting some legacy constants via Exporter.pm
            wrap_existing_import => 1,
            # A general override so you can override other constants in
            # %ENV
            override => sub {
                my ($ctx, $name) = @_;
                return unless exists $ENV{$name};
                return $ENV{$name};
            },
        },
    );

    1;

And this is an example of using it in some user code:

    package My::User::Code;
    use strict;
    use warnings;
    use Test::More qw(no_plan);
    use lib 't/lib';
    BEGIN {
        # Supply a more accurate PI
        $ENV{PI} = 3.14159;
        # Override B
        $ENV{B} = 3;
    }
    use My::Constants qw(
        X
        Y
        A
        B
        SUM
        PI
    );

    is(X, -2);
    is(Y, -1);
    is(A, 1);
    is(B, 3);
    is(SUM, 4);
    is(PI,  "Pi is = 3.14159");

Things to note about this example:

AUTHOR ^

Ævar Arnfjörð Bjarmason <avar@cpan.org>

syntax highlighting: