Object::Container::Exporter - strong shortcuts to your classes.
#your application tree `-- MyApp |-- Api | |-- Form | | `-- Foo.pm | `-- User.pm |-- Container.pm `-- Foo.pm #your sub class package MyApp::Container; use Object::Container::Exporter -base; 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);
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.
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 package MyApp::Container; use Object::Container::Exporter -base; #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.
#your application tree `-- MyApp |-- Model | |-- Api | | `-- User.pm | `-- Command | `-- Password.pm `-- Container.pm #your sub class package MyApp::Container; use Object::Container::Exporter -base; 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.
Register classes to container.
package MyApp::Container; use Object::Container::Exporter -base; #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;
You can register your original function name to call your application calss objects.
Example is:
package MyApp::Container; use Object::Container::Exporter -base; register_namespace form => 'MyApp::Api::Form';
To call the object registered your sub class, the 'container' function exported. But you can change the export function name.
#your sub class package MyApp::Container; use Object::Container::Exporter -base; 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 use MyApp::Container; my $user_bodys = con('db')->selectcolcall_arrayref('SELECT body FROM user');
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 use MyApp::Container; my $obj = container; my $row = $obj->get('teng')->single('user');
Remove the cached object that is created at get method above. Return value is the deleted object if it's exists.
get
Like require function.
package MyApp::Container; use Object::Container::Exporter -base; register db => sub { my $self = shift; $self->load_class('DBI');#load when call this code reference. DBI->connect('dbi:mysql:sandbox', 'root', 'pass',); };
Atsushi Kobayashi <nekokak _at_ gmail _dot_ com>
Hiroyuki Akabane: hirobanex
Much of this documentation was taken from Object::Container
Object::Container.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Object::Container::Exporter, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Object::Container::Exporter
CPAN shell
perl -MCPAN -e shell install Object::Container::Exporter
For more information on module installation, please visit the detailed CPAN module installation guide.