NAME
Object::Simple - Very simple framework for Object Oriented Perl.
VERSION
Version 0.0201
CAUTION
Object::Simple is yet experimenta stage.
Please wait until Object::Simple will be stable.
FEATURES
Object::Simple is framework that simplify Object Oriented Perl.
The feature is that
- 1. You can define accessors in very simple way.
- 2. new method is prepared.
- 3. You can define default value of attribute.
If you use Object::Simple, you are free from bitter work writing new methods and accessors repeatedly.
SYNOPSIS
# Class definition( Book.pm )
package Book;
use Object::Simple;
sub title : Attr {}
sub author : Attr {}
sub price : Attr {}
Object::Simple->end; # End of module. Don't forget to call 'end' method
# Using class
use Book;
my $book = Book->new( title => 'a', author => 'b', price => 1000 );
# Default value of attribute
sub author : Attr { default => 'Kimoto' }
#Automatically build of attribute
sub author : Attr { auto_build => 1 }
sub build_author{
my $self = shift;
$self->author( $self->title . "b" );
}
# Constraint of attribute setting
sub price : Attr { type => 'Int' }
sub author : Attr { type => 'Person' }
# Read only accessor
sub year : Attr { read_only => 1 }
# Required attributes
sub width : Attr { required => 1 }
# weak reference
sub parent : Attr { weak => 1 }
# setter retur value
sub title : Attr { setter_return => 'old' }
sub title : Attr { setter_return => 'current' }
sub title : Attr { setter_return => 'self' }
sub title : Attr { setter_return => 'undef' }
# Inheritance
package Magazine;
use Object::Simple( base => 'Book' );
# Mixin
package Book;
use Object::Simple(
mixin => [
'Object::Simple::Mixin::AttrNames',
'Object::Simple::Mixin::AttrOptions'
]
);
METHODS
new
new method is prepared.
use Book;
my $book = Book->new( title => 'a', author => 'b', price => 1000 );
_arrange_args
This method receive hash or hash reference, and return hash ref by default.
You can override this method to arrange arguments like this.
sub _arrange_args {
my ($self, $x, $y ) = @_;
return {x => $x, y => $y};
}
This method must retrun hash ref.
_init
You can initialize object.
You can override this method
This method receive hash ref argments except attribrte by defined by Attr
sub _init {
my ($self, $args) = @_;
}
error
You can get error as Object::Simple::Error object
my $error = Object::Simple->error;
end
resist attribute and create accessors.
Script must end 'Object::Simple->end;'
Object::Simple->end;
ACCESSOR OPTIONS
default
You can define attribute default value
sub title : Attr {default => 'Good news'}
sub author : Attr {default => ['Ken', 'Taro']}
auto_build
You can automaticaly set attribute value when accessor is called first.
sub author : Attr { auto_build => 1 }
sub build_author{
my $self = shift;
$self->atuhor( Person->new );
}
Builder method name is build_ATTRIBUTE_NAME by default;
You can specify build method .
sub author : Attr { auto_build => 1 }
sub create_author{
my $self = shift;
$self->atuhor( Person->new );
}
type
You can define type of attribute value
sub price: Attr {type => 'Int'} # must be integer
sub author: Attr {type => 'Person'} # must be inherit Perlson class
list of default types
Bool => \&Object::Simple::Constraint::is_bool,
Undef => sub { !defined($_[0]) },
Defined => sub { defined($_[0]) },
Value => \&Object::Simple::Constraint::is_value,
Num => \&Object::Simple::Constraint::is_num,
Int => \&Object::Simple::Constraint::is_int,
Str => \&Object::Simple::Constraint::is_str,
ClassName => \&Object::Simple::Constraint::is_class_name,
Ref => sub { ref($_[0]) },
ScalarRef => \&Object::Simple::Constraint::is_scalar_ref,
ArrayRef => \&Object::Simple::Constraint::is_array_ref,
HashRef => \&Object::Simple::Constraint::is_hash_ref,
CodeRef => \&Object::Simple::Constraint::is_code_ref,
RegexpRef => \&Object::Simple::Constraint::is_regexp_ref,
GlobRef => \&Object::Simple::Constraint::is_glob_ref,
FileHandle => \&Object::Simple::Constraint::is_file_handle,
Object => \&Object::Simple::Constraint::is_object
You can specify code reference
sub price: Attr {type => sub{ $_[0] =~ /^\d+$/ }}
read_only
You can create read only accessor
sub title: Attr { read_only => 1 }
setter_return
You can spesicy return value when setting value
sub title : Attr { setter_return => 'old' }
list of setter_return option
old
current
self
undef
required
You can specify required attribute when instance is created.
sub title : Attr {required => 1}
weak
attribute value is weak reference.
sub parent : Attr {weak => 1}
SEE ALSO
Object::Simple::Constraint - Constraint methods for Object::Simple 'type' option.
Object::Simple::Error - Structured error system for Object::Simple.
AUTHOR
Yuki Kimoto, <kimoto.yuki at gmail.com>
BUGS
Please report any bugs or feature requests to bug-simo at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Object::Simple. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Object::Simple
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
SIMILAR MODULES
Class::Accessor,Class::Accessor::Fast, Moose, Mouse.
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.