The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Object::Simple - Simplest class builder, Mojo::Base porting, fast and less memory

  • Simplest class builder. All you learn is only has function!

  • 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.

SYNOPSIS

  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';
  
  # Another way to inherit(This is Object::Simple original)
  package Bar;
  use Object::Simple -base => 'Foo';

  # Another way to inherit (This is Object::Simple original)
  use Foo -base;

DESCRIPTION

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.

TUTORIAL

1. Create class and accessor

At first, you create class.

  package SomeClass;
  use Object::Simple -base;

By using -base option, SomeClass inherit Object::Simple and import has method.

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.

  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;

2. Override method

Method can be overridden.

Example:

Initialize the object

  sub new {
    my $self = shift->SUPER::new(@_);
    
    # Initialization
    
    return $self;
  }

Example:

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);

3. Examples - class, accessor, inheritance and method overriding

I introduce Object::Simple example.

Point class: two accessor x and y, and clear method to set x and y to 0.

  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.

  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;

WHAT IS OBJECT-ORIENTED PROGRAMING?

I introduce essence of Object-Oriented programing.

1. Inheritance

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

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.

  my $class = ref $obj;

If you know what class the object inherits, use isa method.

  $obj->isa('SomeClass');

If you know what method the object(or class) can use, use can method

  SomeClass->can('method1');
  $obj->can('method1');

2. Encapsulation

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);

3. Polymorphism

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.

FUNCTIONS

has

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.

  $obj->foo(1)->bar(2);

METHODS

new

  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.

attr

  __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.

OPTIONS

-base

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;

FAQ

Really enough object-oriented programing with this few features?

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.

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;
  }

Object::Simple is fastest OO module?

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};

What is benefits comparing with Mojo::Base?

  • 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.

Why Object::Simple is different from Mojo::Base in some points?

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.

BACKWARDS COMPATIBILITY POLICY

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)

DEPRECATED

  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
  
  class_attr method # will be removed 2017/1/1
  dual_attr method # will be removed 2017/1/1

BUGS

Tell me the bugs by mail(<kimoto.yuki at gmail.com>) or github http://github.com/yuki-kimoto/Object-Simple

AUTHOR

Yuki Kimoto(<kimoto.yuki at gmail.com>)

I'm pleasure if you send message for cheer. I can get power by only your messages!

USERS

Projects using Object::Simple.

SEE ALSO

CPAN have various class builders. Let's compare it with Object::Simple.

Mojo::Base, Class::Accessor, Class::Accessor::Fast, Moose, Moo, Class::Tiny.

COPYRIGHT & LICENSE

Copyright 2008-2016 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:

Around line 683:

You forgot a '=back' before '=head2'

Around line 689:

=back without =over