Badger

 view release on metacpan or  search on metacpan

lib/Badger/Class.pm  view on Meta::CPAN

delegate to any symbols in any of the *::Util modules).

    use Badger::Class
        utils => 'blessed xprintf';

    sub welcome {
        my ($self, $name) = @_;

        $name = $name->get_name
            if blessed $name && $name->can('get_name');

        xprintf('Hello %s!', $name);
    }

See the L<utils()> method and L<Badger::Utils> for further details.

=head2 config

This can be used to define configuration options for your module.
It delegates to the L<Badger::Class::Config> module.

    package Your::Module;

    use Badger::Class
        base      => 'Badger::Base',
        accessors => 'foo bar baz wig woot toot zoot zang',
        config    => [
            'foo',                      # optional item
            'bar!',                     # mandatory item
            'baz=42',                   # item with default
            'wig|wam|bam',              # item with aliases
            'woot|pkg:WOOT',            # fallback to $WOOT pkg var
            'toot|class:WOOT',          # fallback to $WOOT class var
            'zoot|method:ZOOT',         # fallback to ZOOT() method/constant
            'zing|zang|pkg:ZING=99',    # combination of above
        ];

    sub init {
        my ($self, $config) = @_;

        # call the configure() method provided by the above
        $self->configure($config);

        return $self;
    }

The L<configure()|Badger::Class::Config/configure()> method is exported into
your module as a configuration method for initialising object instances.
A C<$CONFIG> package variable is also exported containing a reduced (i.e.
optimised for performance) version of the configuration scheme which the
L<configure()|Badger::Class::Config/configure()> method uses.

=head2 codec

This can be used to import a single codec from L<Badger::Codecs>.

    use Badger::Class
        codec => 'base64';

    my $encoded = encode('Some text');
    my $decoded = decode($encoded);

See the L<codec()> method and L<Badger::Codecs> for further details.

=head2 codecs

This can be used to import multiple codecs from L<Badger::Codecs>.

    use Badger::Class
        codecs => 'base64 storable';

    my $encoded = encode_base64( encode_storable( $some_data ) );
    my $decoded = decode_storable( decode_base64( $encoded ) );

Codecs can be composed as a pipeline of other codecs. In the following
example, we define a C<session> codec which encodes data by first passing it
through the C<storable> codec (which uses the L<Storable> C<freeze()>
subroutine) and then onto the C<base64> codec (which uses the L<MIME::Base64>
C<encode_base64> subroutine).

    use Badger::Class
        codecs => {
            session => 'base64+storable',
        };

    my $encoded = encode_session( $some_data );
    my $decoded = decode_session( $encoded );

In case you were wondering about the significance of this particular codec
combination, the C<Storable> module can generate NULL characters in the
output stream which will make some databases (e.g. Postgres) choke.  Adding
a second level of Base 64 encoding solves the problem.

See the L<codecs()> method and L<Badger::Codecs> for further details.

=head2 methods

This can be used to define methods for a class on-the-fly or patch existing
subroutines or methods into a class.

    use Badger::Class
        methods => {
            foo => sub { print "This is the foo method" },
            bar => \&Some::Other::Method,
        };

=head2 alias

This can be used to define aliases to existing methods.

    use Badger::Class
        alias => {
            foo => \&bar,
            baz => 'bam',
        };

See the L<alias()> method for further details.

=head2 slots

This can be used to define methods for list-based objects.

    use Badger::Class
        slots => 'size colour object';

See the L<slots()> method for further details.

=head2 accessors / get_methods

This can be used to define simple read-only accessor methods for a class.

    use Badger::Class
        accessors => 'foo bar';

You can use C<get_methods> as an alias for C<accessors> if you prefer.

    use Badger::Class
        get_methods => 'foo bar';

See the L<accessors()> method for further details.

=head2 mutators / set_methods

This can be used to define simple read/write mutator methods for a class.

    use Badger::Class
        mutators => 'foo bar';



( run in 0.555 second using v1.01-cache-2.11-cpan-39bf76dae61 )