Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  OBJECT::SIMPLE (3)

.ds Aq ’


Object::Simple - Simple class builder(Mojo::Base porting)



  package SomeClass;
  use Object::Simple -base;
  # Create a accessor
  has foo;
  # Create a accessor having default value
  has foo => 1;
  has foo => sub { [] };
  has foo => sub { {} };
  has foo => sub { OtherClass->new };
  # Create accessors at once
  has [qw/foo bar baz/];
  has [qw/foo bar baz/] => 0;
  # Create all accessors at once
  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});
  # Set and get value
  my $foo = $obj->foo;


  package Foo;
  use Object::Simple -base;
  package Bar;
  use Foo -base;
  # Another way
  package Bar;
  use Object::Simple -base => Foo;


Object::Simple is Mojo::Base porting. you can use Mojo::Base features.

Object::Simple is a generator of accessor method, 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 accessor, its value is converted to array reference without warnings.

Some people find Moose too complex, and dislike that it depends on outside modules. Some say that Moose is almost like another language and does not fit the familiar perl syntax. In some cases, in particular smaller projects, some people feel that Moose will increase complexity and therefore decrease programmer efficiency. In addition, Moose can be slow at compile-time and its memory usage can get large.

Object::Simple is the middle way between Class::Accessor and complex class builder. Only offten used features is implemented has no dependency. Object::Simple is almost same as Mojo::Base.

new method can receive hash or hash reference. You can specify default value.

If you like Mojo::Base, Object::Simple is good choice.


See Object::Simple::Guide to know Object::Simple details.



you can inherit Object::Simple and has function is imported by -base option.

  package Foo;
  use Object::Simple -base;
  has x => 1;
  has y => 2;

strict and warnings is automatically enabled and Perl 5.10 features(say, state, given is imported.

You can also use -base option in sub class to inherit other class.

  # Bar inherit Foo
  package Bar;
  use Foo -base;

You can also use the following syntax.

  # Same as above
  package Bar;
  use Object::Simple -base => Foo;



Create accessor.

  has foo;
  has [qw/foo bar baz/];
  has foo => 1;
  has foo => sub { {} };

Create accessor. has receive accessor name and default 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 reference 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;

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.


You can create all accessors at once.

  has [qw/foo bar baz/],
    pot => 1,
    mer => sub { 5 };



  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([qw/foo bar baz/]);
  __PACKAGE__->attr(foo => 1);
  __PACKAGE__->attr(foo => sub { {} });

    [qw/foo bar baz/],
    pot => 1,
    mer => sub { 5 }

Create accessor. attr method usage is equal to has method.


  class_attr method # will be removed 2017/1/1
  dual_attr method # will be removed 2017/1/1


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)


Tell me the bugs by mail or github <>


Yuki Kimoto, <kimoto.yuki at>


Copyright 2008-2013 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.

Search for    or go to Top of page |  Section 3 |  Main Index

perl v5.20.3 OBJECT::SIMPLE (3) 2014-02-03

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.