Skip to content
No description or website provided.
Perl
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib/Class/Mixin
t
xt
.gitignore
.shipit
Changes
MANIFEST
MANIFEST.SKIP
Makefile.PL
README.mkdn

README.mkdn

NAME

Class::Mixin::Container - Minimalistic container mixin

SYNOPSIS

# make your container class
package MyApp;
use Class::Mixin::Container qw/register get/;

sub new {
    my $class = shift;
    my %args = @_ == 1 ? %{$_[0]}  : @_;
    bless { %args }, $class;
}

__PACKAGE__->register(cookie_jar => sub {
    $self->cookie_jar
});

__PACKAGE__->register('LWP::UserAgent' => sub {
    my $self = shift;
    LWP::UserAgent->new(cookie_jar => $self->cookie_jar);
});

# in your application
package main;

my $container = MyApp->new;
my $ua = $container->get('LWP::UserAgent');

DESCRIPTION

Class::Mixin::Container is a minimalistic container stuff.

This module is Mixin class.

It is useful to create application framework.

EXPORTABLE METHODS

  • MyApp->register($name, $code)

This method is a class method. This method register the $code as $name.

  • $app->get($name)

This is a instance method. This method creates the instance of $name from $code.

COOKBOOK

HOW DO YOU USE SINGLETON?

The great Class::Singleton is available on CPAN.

package MyApp;
use base qw/Class::Singleton/;
use Class::Mixin::Container qw/register get/;

sub _new_instance { bless { }, shift }

package main;


my $app = MyApp->instance;
$app->register('Foo' => sub { Foo->new });
...
my $foo = $app->get('Foo');

HOW DO YOU RESOLVE METHOD NAME CONFLICTION?

You can alias to import the method name.

use Class::Mixin::Container 'register', 'get' => { -as => 'component' };

AUTHOR

Tokuhiro Matsuno

SEE ALSO

Object::Container, Object::Registrar

LICENSE

Copyright (C) Tokuhiro Matsuno

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Something went wrong with that request. Please try again.