From Code to Community: Sponsoring The Perl and Raku Conference 2025 Learn more

NAME

Object::Container::Exporter - strong shortcuts to your classes.

SYNOPSIS

#your application tree
`-- MyApp
|-- Api
| |-- Form
| | `-- Foo.pm
| `-- User.pm
|-- Container.pm
`-- Foo.pm
#your sub class
register_namespace form => 'Mock::Api::Form';
register 'foo' => sub {
my $self = shift;
$self->load_class('Mock::Foo');
Mock::Foo->new;
};
#your main script
use MyApp::Container qw/api form/;
container('foo')->say;
my $row = api('User')->fetch;
form('foo')->fillin($row->get_columns);

DESCRIPTION

Object::Container::Exporter is object container like Object::Container. The difference is that it has bulk registering the class object in your indeicate directory with container.

Bulk registering your indicated directory's class objects

Object::Container::Exporter provide the shortcut function to call the class object in your application' second directory.There is no 'use' and 'new' in your main script to access your class object.In this case, you must indicate the decamelized directory name as export function.

Examples are:

#your application tree
`-- MyApp
|-- Api
| `-- Password.pm
`-- Container.pm
#your sub class
#your main script
use MyApp::Container qw/api/;
my $hash_val = api('Password')->generate($pass);

When you wanna export shortcut function to call the class object in any directory, you can register your original shortcut function in the sub class.

Examples are:

#your application tree
`-- MyApp
|-- Model
| |-- Api
| | `-- User.pm
| `-- Command
| `-- Password.pm
`-- Container.pm
#your sub class
register_namespace cmd => 'Mock::Model::Api::Command';
#your main script
use MyApp::Container qw/api cmd/;
my $hash_val = cmd('Password')->generate($pass);
my $row = api('User')->register(
id => 'nekokak',
pass => $hash_val,
);

Now, you have efficiently fun life development.

METHODS

register

Register classes to container.

Examples are:

#register($register_name, $initializer_code);
register db => sub {
my $self = shift;
$self->load_class('DBI');
DBI->connect('dbi:mysql:sandbox', 'root', 'pass', +{
mysql_enable_utf8 => 1,
PrintError => 0,
RaiseError => 1,
},);
};
#register($load_class,@opts);
register 'WWW::Mechanize', @args;

register_namespace

You can register your original function name to call your application calss objects.

Example is:

register_namespace form => 'MyApp::Api::Form';

register_default_container_name

To call the object registered your sub class, the 'container' function exported. But you can change the export function name.

Example is:

#your sub class
register_default_container_name 'con';
register db => sub {
my $self = shift;
$self->load_class('DBI');
DBI->connect('dbi:mysql:sandbox', 'root', 'pass',);
};
#your main script
my $user_bodys = con('db')->selectcolcall_arrayref('SELECT body FROM user');

get

Get the object that registered by 'register' method.

Examples are: #your sub class package MyApp::Container; use Object::Container::Exporter -base;

register dbh => sub {
my $self = shift;
$self->load_class('DBI');
DBI->connect('dbi:mysql:sandbox', 'root', 'pass',);
};
register teng => sub {
my $self = shift;
$self->load_class('MyApp::DB');
MyApp::DB->new(
dbh => $self->get('dbh'),#get registered object
);
};
#your main script
use MyApp::Container -no_export;
my $obj = MyApp::Container->instance;
my $row = $obj->get('teng')->single('user');
##or
my $obj = container;
my $row = $obj->get('teng')->single('user');

remove

Remove the cached object that is created at get method above. Return value is the deleted object if it's exists.

load_class

Like require function.

Example is:

register db => sub {
my $self = shift;
$self->load_class('DBI');#load when call this code reference.
DBI->connect('dbi:mysql:sandbox', 'root', 'pass',);
};

AUTHOR

Atsushi Kobayashi <nekokak _at_ gmail _dot_ com>

CONTRIBUTORS

Hiroyuki Akabane: hirobanex

THANKS

Much of this documentation was taken from Object::Container

SEE ALSO

Object::Container.

LICENSE

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