NAME
Object::Simple - Generate accessor having default value, and provide constructor
SYNOPSIS
package SomeClass;
use base 'Object::Simple';
# Generate a accessor
__PACKAGE__->attr('foo');
# Generate a accessor having default attribute value
__PACKAGE__->attr(foo => 1);
__PACKAGE__->attr(foo => sub { [] });
__PACKAGE__->attr(foo => sub { {} });
__PACKAGE__->attr(foo => sub { OtherClass->new });
# Generate accessors at once
__PACKAGE__->attr([qw/foo bar baz/]);
__PACKAGE__->attr([qw/foo bar baz/] => 0);
# Generate accessors more easy way
__PACKAGE__->attr(
[qw/foo bar baz/],
some => 1,
other => sub { 5 }
);
More Simple
package SomeClass;
use Object::Simple -base;
# Generate a accessor
has 'foo';
# Generate a accessor having default attribute value
has foo => 1;
has foo => sub { [] };
has foo => sub { {} };
has foo => sub { OtherClass->new };
# Generate accessors at once
has [qw/foo bar baz/];
has [qw/foo bar baz/] => 0;
# Generate accessors more easy way
has [qw/foo bar baz/],
some => 1,
other => sub { 5 };
Use the class.
# Create a new object
my $obj = SomeClass->new;
my $obj = SomeClass->new(foo => 1, bar => 2);
my $obj = SomeClass->new({foo => 1, bar => 2});
# Get and set a attribute value
my $foo = $obj->foo;
$obj->foo(1);
Inheritance
package Foo;
use Object::Simple -base;
package Bar;
use Foo -base;
has x => 1;
DESCRIPTION
Object::Simple is a generator of accessor, such as Class::Accessor, Mojo::Base, or Moose. Class::Accessor is simple, but lack offten used features. new()
method can't receive hash arguments. Default value can't be specified. If multipule values is set through the accssor, its value is converted to array reference without warnings.
Moose is too complex for many people to use, and depends on many modules. Moose is almost another language, and don't fit familiar perl syntax. Moose increase the complexity of projects, rather than increase production efficiency. In addition, its complie speed is slow and used memroy is large.
Object::Simple is the middle area between Class::Accessor and complex class builder. Only offten used features is implemented. new()
can receive hash or hash reference as arguments. You can specify default value for the accessor. Compile speed is fast and used memory is small. Debugging is easy. Object::Simple is compatible of Mojo::Base
See Object::Simple::Guide to know Object::Simple details.
GUIDE
METHODS
new
my $obj = Object::Simple->new(foo => 1, bar => 2);
my $obj = Object::Simple->new({foo => 1, bar => 2});
Create a new object. new()
receive hash or hash reference as arguments.
attr
Generate accessor.
__PACKAGE__->attr('foo');
__PACKAGE__->attr([qw/foo bar baz/]);
__PACKAGE__->attr(foo => 1);
__PACKAGE__->attr(foo => sub { {} });
Generate accessor. attr()
receive accessor name and default attribute value. Default value is optional. If you want to create multipule accessors at once, specify accessor names as array reference at first argument.
If you want to specify refrence or object as default value, it must be code reference not to share the value with other objects.
Get and set a attribute value.
my $foo = $obj->foo;
$obj->foo(1);
If a default value is specified and the value is not exists, you can get default value.
If a value is set, the accessor return self object. So you can set a value repeatedly.
$obj->foo(1)->bar(2);
You can create accessors more easy way.
__PACKAGE__->attr(
[qw/foo bar baz/],
pot => 1,
mer => sub { 5 }
);
First argument are accessors without default. Rest arguments are accessors with default.
BUGS
Tell me the bugs by mail or github http://github.com/yuki-kimoto/Object-Simple
AUTHOR
Yuki Kimoto, <kimoto.yuki at gmail.com>
COPYRIGHT & LICENSE
Copyright 2008 Yuki Kimoto, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.