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

NAME

Object::Simple - Create attribute method, and provide constructor

SYNOPSIS

    package SomeClass;
    use Object::Simple -base;
    
    # Create a attribute method
    has 'foo';
    
    # Create a attribute method having default value
    has foo => 1;
    has foo => sub { [] };
    has foo => sub { {} };
    has foo => sub { OtherClass->new };
    
    # Create attribute methods at once
    has [qw/foo bar baz/];
    has [qw/foo bar baz/] => 0;
    
    # Create all attribute methods 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});
    
    # 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;
    # or use Object::Simple -base => 'Foo';

DESCRIPTION

Object::Simple is a generator of attribute 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 attribute method, 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. Object::Simple is similar with Mojo::Base. new can receive hash or hash reference as arguments. You can specify default value for the attribute. Compile speed is fast and used memory is small. Debugging is easy.

GUIDE

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

FUNCTIONS

If you specify -base flag, you can inherit Object::Simple and import has function. has function create attribute method.

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

strict and warnings is automatically enabled and Perl 5.10 features is imported.

You can use -base flag in sub class for inheritance.

    package Bar;
    use Foo -base;
    # or use Object::Simple -base => 'Foo';
    
    has z => 3;

This is equal to

    package Bar;
    
    use base 'Foo';
    use strict;
    use warnings;
    use feature ':5.10';
    sub has { __PACKAGE__->attr(@_) }
    

has

Create attribute method.

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

Create attribute method. has receive attribute name and default value. Default value is optional. If you want to create multipule attribute methods at once, specify attribute 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;
    $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 attribute return self object. So you can set a value repeatedly.

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

You can create all attribute methods at once.

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

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

    __PACKAGE__->attr('foo');
    __PACKAGE__->attr([qw/foo bar baz/]);
    __PACKAGE__->attr(foo => 1);
    __PACKAGE__->attr(foo => sub { {} });

    __PACKAGE__->attr(
        [qw/foo bar baz/],
        pot => 1,
        mer => sub { 5 }
    );

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

DEPRECATED FUNCTIONALITY

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

BACKWARDS COMPATIBILITY POLICY

If a functionality is DEPRECATED, you can know it by DEPRECATED warnings except for attribute method. 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)

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.