Object::Simple - Simplest class builder, Mojo::Base porting, fast and less memory
Simplest class builder. All you learn is only has function!
has
Mojo::Base porting. Do you like Mojolicious? If so, this is good choices!
Fast and less memory. Fast new and accessor method. Memory saving implementation.
new
package SomeClass; use Object::Simple -base; # Create accessor has 'foo'; # Create accessor with default value has foo => 1; has foo => sub { [] }; has foo => sub { {} }; has foo => sub { OtherClass->new }; # Create accessors at once has ['foo', 'bar', 'baz']; has ['foo', 'bar', 'baz'] => 0;
Create object.
# Create a new object my $obj = SomeClass->new; my $obj = SomeClass->new(foo => 1, bar => 2); my $obj = SomeClass->new({foo => 1, bar => 2}); # Set and get value my $foo = $obj->foo; $obj->foo(1); # Setter can be chained $obj->foo(1)->bar(2);
Inheritance
package Foo; use Object::Simple -base; # Bar inherit Foo package Bar; use Object::Simple 'Foo';
Object::Simple is Simplest class builder. All you learn is only has function. You can learn all features of Object::Simple in an hour. There is nothing difficult.
Do you like Mojolicious? In fact, Object::Simple is Mojo::Base porting. Mojo::Base is basic class builder in Mojolicious project. If you like Mojolicious, this is good choice. If you have known Mojo::Base, you learn nothing.
new and accessor method is fast. Implementation is pure perl and plain old hash-base object. Memory is saved. Extra objects is not created at all. Very light-weight object-oriented module.
Comparison with Class::Accessor::Fast
Class::Accessor::Fast is simple, but lack often used features. new method can't receive hash arguments. Default value can't be specified. If multiple values is set through the accessor, its value is converted to array reference without warnings.
Comparison with Moose
Moose has very complex syntax and depend on much many modules. You have to learn many things to do object-oriented programing. Understanding source code is difficult. Compile-time is very slow and memory usage is very large. Execution speed is not fast. For simple OO, Moose is overkill. Moo is improved in this point.
At first, you create class.
package SomeClass; use Object::Simple -base;
By using -base option, SomeClass inherit Object::Simple and import has method.
-base
Object::Simple have new method. new method is constructor. new method can receive hash or hash reference.
my $obj = SomeClass->new; my $obj = SomeClass->new(foo => 1, bar => 2); my $obj = SomeClass->new({foo => 1, bar => 2});
Create accessor by using has function.
has 'foo';
If you create accessor, you can set or get value
# Set value $obj->foo(1); # Get value my $foo = $obj->foo;
Setter can be chained.
$obj->foo(1)->bar(2);
You can define default value.
has foo => 1;
If foo value is not exists, default value is used.
foo
my $foo_default = $obj->foo;
If you want to use reference or object as default value, default value must be surrounded by code reference. the return value become default value.
has foo => sub { [] }; has foo => sub { {} }; has foo => sub { SomeClass->new };
You can create multiple accessors at once.
has ['foo', 'bar', 'baz']; has ['foo', 'bar', 'baz'] => 0;
Method can be overridden.
Example:
Initialize the object
sub new { my $self = shift->SUPER::new(@_); # Initialization return $self; }
Change arguments of new.
sub new { my $self = shift; $self->SUPER::new(x => $_[0], y => $_[1]); return $self; }
You can pass array to new method.
my $point = Point->new(4, 5);
I introduce Object::Simple example.
Point class: two accessor x and y, and clear method to set x and y to 0.
x
y
clear
package Point; use Object::Simple -base; has x => 0; has y => 0; sub clear { my $self = shift; $self->x(0); $self->y(0); }
Use Point class.
use Point; my $point = Point->new(x => 3, y => 5); print $point->x; $point->y(9); $point->clear;
Point3D class: Point3D inherit Point class. Point3D class has z accessor in addition to x and y. clear method is overridden to clear x, y and z.
z
package Point3D; use Object::Simple 'Point'; has z => 0; sub clear { my $self = shift; $self->SUPER::clear; $self->z(0); }
Use Point3D class.
use Point3D; my $point = Point->new(x => 3, y => 5, z => 8); print $point->z; $point->z(9); $point->clear;
I introduce essence of Object-Oriented programing.
First concept is inheritance. Inheritance means that if Class Q inherit Class P, Class Q call all methods of class P.
+---+ | P | Base class +---+ have method1 and method2 | +---+ | Q | Sub class +---+ have method3
Class Q inherits Class P, Q can call all methods of P in addition to methods of Q.
In other words, Q can call method1, method2, and method3
method1
method2
method3
You can inherit other class by the following way.
# P.pm package P; use Object::Simple -base; sub method1 { ... } sub method2 { ... } # Q.pm package Q; use Object::Simple 'P'; sub method3 { ... }
Perl have useful functions and methods to help Object-Oriented programing.
If you know what class the object is belonged to, use ref function.
ref
my $class = ref $obj;
If you know what class the object inherits, use isa method.
isa
$obj->isa('SomeClass');
If you know what method the object(or class) can use, use can method
can
SomeClass->can('method1'); $obj->can('method1');
Second concept is encapsulation. Encapsulation means that you don't touch internal data directory. You must use public method when you access internal data.
Create accessor and use it to keep this rule.
my $value = $obj->foo; $obj->foo(1);
Third concept is polymorphism. Polymorphism is divided into two concepts, overload and override
Perl programmer don't need to care overload. Perl is dynamic type language. Subroutine can receive any value.
Override means that you can change method behavior in sub class.
# P.pm package P; use Object::Simple -base; sub method1 { return 1 } # Q.pm package Q; use Object::Simple 'P'; sub method1 { return 2 }
P method1 return 1. Q method1 return 2. Q method1 override P method1.
# P method1 return 1 my $obj_a = P->new; $obj_p->method1; # Q method1 return 2 my $obj_b = Q->new; $obj_q->method1;
If you want to call super class method from sub class, use SUPER pseudo-class.
package Q; use Object::Simple 'P'; sub method1 { my $self = shift; # Call supper class P method1 my $value = $self->SUPER::method1; return 2 + $value; }
If you understand three concepts, you have learned Object-Oriented programming primary parts.
Create accessor.
has 'foo'; has ['foo', 'bar', 'baz']; has foo => 1; has foo => sub { {} }; has ['foo', 'bar', 'baz']; has ['foo', 'bar', 'baz'] => 0;
has function receive accessor name and default value. Default value is optional. If you want to create multiple accessors at once, specify accessor names as array reference at first argument.
If you want to specify reference or object as default value, it must be code reference not to share the value with other objects.
Get and set a 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.
Setter return invocant. so you can do chained call.
my $obj = Object::Simple->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.
__PACKAGE__->attr('foo'); __PACKAGE__->attr(['foo', 'bar', 'baz']); __PACKAGE__->attr(foo => 1); __PACKAGE__->attr(foo => sub { {} }); __PACKAGE__->attr(['foo', 'bar', 'baz']); __PACKAGE__->attr(['foo', 'bar', 'baz'] => 0);
Create accessor. attr method usage is equal to has function.
attr
By using -base option, the class inherit Object::Simple and import has function.
package Foo; use Object::Simple -base; has x => 1; has y => 2;
strict and warnings is automatically enabled.
If you want to inherit class, let's write the following way.
# Bar inherit Foo package Bar; use Object::Simple 'Foo';
You can also use the following syntax. This is Object::Simple only.
# Same as above package Bar; use Object::Simple -base => 'Foo';
You can also use -base option in sub class to inherit other class. This is Object::Simple only.
# Same as above package Bar; use Foo -base;
Yes, for example, Mojolicious is very big project, but in fact, source code is clean only using single inheritance. Generally speaking, readable source code is build on simple concepts, not complex features.
BUILD, BUILDARGS and DEMOLISH methods in Moo are needed for good object-oriented programming? If you want to use multiple inheritance or role, these methods is needed.
BUILD
BUILDARGS
DEMOLISH
But I strongly recommend you use only single inheritance in object-oriented programming. Single inheritance is clean and readable.
If you use only single inheritance, You can create custom constructor and call constructors in correct order. and You can create custom destructor and call destructors in correct order,
Creating custom constructor is very very easy. There is nothing difficult.
# Custom constructor sub new { # At first Call super class constructor. Next do what you want my $self = shift->SUPER::new(@_); # What you want return $self; } # Custom destructor sub DESTROY { my $self = shift; # What you want # At first, do what you want, Next call super class destructor $selft->SUPER::DESTROY; return $self; }
No, Object::Simple is not fastest module, but enough fast. If you really need performance, you can access hash value directory.
# I want performance in some places. Let's access hash value directory! # Object::Simple is plain old hash-based object $self->{x};
Support Perl 5.8
Installation is very fast because there are a few files.
Some people think that my module want not to depend on whole Mojolicious to use Mojo::Base only. Object::Simple satisfy the demand.
In old days, Object::Simple wasn't Mojo::Base porting. I tried different things.
Now, I want Object::Simple to be same as Mojo::Base completely except supporting Perl 5.8.
If a functionality is DEPRECATED, you can know it by DEPRECATED warnings. You can check all DEPRECATED functionalities by document. DEPRECATED functionality is removed after five years, but if at least one person use the functionality and tell me that thing I extend one year each time he tell me it.
EXPERIMENTAL functionality will be changed without warnings.
(This policy was changed at 2011/10/22)
function exporting of C<new> and C<attr> method # Will be removed 2021/6/1 The syntax of multiple key-value arguments has x => 1, y => 2; __PACAKGE__->attr(x => 1, y => 2); # Will be removed 2021/6/1
Tell me the bugs by mail(<kimoto.yuki at gmail.com>) or github http://github.com/yuki-kimoto/Object-Simple
<kimoto.yuki at gmail.com>
If you have any questions the documentation might not yet answer, don't hesitate to ask on the mailing list or the official IRC channel #object-simple on irc.perl.org.
Yuki Kimoto(<kimoto.yuki at gmail.com>)
I'm pleasure if you send message for cheer. I can get power by only your messages!
Projects using Object::Simple.
GitPrep - Portable GitHub system into your own server. https://github.com/yuki-kimoto/gitprep
DBIx::Custom - DBI extension to execute insert, update, delete, and select easily
Validator::Custom - HTML form Validation, simple and good flexibility
CPAN have various class builders. Let's compare it with Object::Simple.
Mojo::Base, Class::Accessor, Class::Accessor::Fast, Moose, Moo, Class::Tiny.
Copyright 2008-2017 Yuki Kimoto, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Artistic v2.
This is same as Mojolicious licence.
2 POD Errors
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head2'
=back without =over
To install Object::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Object::Simple
CPAN shell
perl -MCPAN -e shell install Object::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.