NAME
JIP::Object - A simple object system.
VERSION
Version 0.03
SYNOPSIS
use JIP::Object;
my $obj = JIP::Object->new;
$obj->has('foo', (get => '+', set => '+'));
$obj->set_foo(42)->foo; # 42
$obj->method('say_foo', sub {
my $self = shift;
print $self->foo, "\n";
});
$obj->say_foo;
Inheritance with the prototype chain:
my $proto = JIP::Object->new;
# Creates an own method 'as_string' on $proto
$proto->method('as_string', sub { 'prototype' });
my $obj = JIP::Object->new(proto => $proto);
ref $obj->own_method('as_string'); # q{}
$obj->as_string; # q{prototype}
# Creates an own method 'as_string' on $obj
$obj->method('as_string', sub { 'object' });
# If the method is found in $obj, then proto is not checked.
ref $obj->own_method('as_string'); # q{CODE}
$obj->as_string; # q{object}
# The end of the prototype chain as undef.
ref $obj->proto->proto; # q{}
METHODS
new
Construct a new JIP::Object object.
my $obj = JIP::Object->new;
my $obj = JIP::Object->new(proto => JIP::Object->new);;
has
Define a new property.
# Public
$obj->has('foo', (get => '+', set => '+'));
$obj->set_foo(42)->foo; # 42
# Private
$obj->has('bar', (get => '-', set => '-'));
$obj->_set_bar(42)->_bar; # 42
# Create user-defined names for getters/setters
$obj->has('wtf' => (get => 'wtf_getter', set => 'wtf_setter'));
$obj->wtf_setter(42)->wtf_getter; # 42
# Pass an optional first argument of setter to set
# a default value, it should be a constant or callback.
$obj->has('baz' => (get => '+', set => '+', default => 42));
($self->set_baz->baz; # 42
$obj->has('qux' => (get => '+', set => '+', default => sub {
my $self = shift;
return $self->baz;
}));
$self->set_qux->qux; # 42
# Declaring multiple attributes in a single declaration
$obj->has([qw(one two three)]);
method
Define a new method.
$obj = $obj->method('get_x', sub {
my $self = shift;
return $self->x;
});
proto
Returns the value of the 'proto' attribute.
$obj->proto;
set_proto
Sets the value of the 'proto' attribute.
$obj->set_proto($proto);
own_method
$code = $obj->own_method('string value of a method name');
The own_method returns CODE if object has a method of the specified name, undef if it does not.
SEE ALSO
Mock::Quick::Object.
AUTHOR
Vladimir Zhavoronkov, <flyweight at yandex.ru>
LICENSE AND COPYRIGHT
Copyright 2015 Vladimir Zhavoronkov.
This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
http://www.perlfoundation.org/artistic_license_2_0
Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.