Anonymous object. Anonymous simply means nameless. An object which has no reference is known as an anonymous object. It can be used at the time of object creation only. If you have to use an object only once, an anonymous object is a good approach.
=head1 SUBROUTINES/METHODS
=head2 new
Instantiate a new Anonymous::Object object.
my $anon = Anonymous::Object->new({
object_name => 'Custom::Object',
types => {
Str => 1,
HashRef => 1,
...
},
type_library => 'Types::Standard',
type_map => {
HASH => 'HashRef',
ARRAY => 'ArrayRef',
STRING => 'Str',
SCALAR => 'ScalarRef',
REF => 'Ref',
CODE => 'CodeRef',
GLOB => 'GlobRef',
NUM => 'Num',
INT => 'Int',
default => 'Any'
},
meta => {
sub1 => 'return $_[1] * $_[2]',
sub2 => 'return $_[0]->{sub2};'
},
default => {
sub2 => 'xyz'
}
});
=head3 object_name
The object name used when bulding the Anonymous::Object. Expects a Str.
=head3 types
The types that will be loaded into the Anonymous::Object when built. Expects a HashRef.
=head3 type_library
The type library that you would like the Anonymous::Object to use. The default is Types::Standard. Expects a Str.
=head3 type_map
The mapping that is used when auto detecting types from perl data structures. Expects a HashRef.
=head3 meta
The method meta store. Expects a HashRef.
=head3 default
The default values for Anonymous::Object accessors. Expects a HashRef.
=head2 clean
Instantiate a clean Anonymous::Object passing through object_name, type_library and type_map;
my $clean_anon = $anon->clean;
=head2 hash_to_object
Convert a perl hash into a single level perl object. Expects param $hash to be a HashRef.
my $hash = {
one => 1,
two => 2,
three => {
four => 4,
five => [
{
six => 6
}
],
}
};
my $obj = $obj->hash_to_object($hash, %method_options)
$hash->one; # 1
$hash->three->{four}; # 4
$hash->three->{five}->[0]->{six}; # 6
=head2 hash_to_object_context
Convert a perl hash that contain values which are sub routines where you need to keep context.
my $num = 1;
my $hash = {
add => { $num += $_[0] },
minus => { $num -= $_[0] },
};
my $obj = $obj->hash_to_object_context($hash, %method_options)
$hash->add(1); # 2
$hash->minus(1); # 1
=head2 hash_to_nested_object
Convert a perl hash into a multi level perl object. Expects param $hash to be a HashRef.
my $hash = {
one => 1,
two => 2,
three => {
four => 4,
five => [
{
six => 6
}
],
}
};
my $obj = $obj->hash_to_nested_object($hash, %method_options)
$hash->one; # 1
$hash->three->four; # 4
$hash->three->five->[0]->six; # 6
=head2 array_to_nested_object
Convert a perl array into a multi level perl object. Expects param $array to be a ArrayRef.
my $array = [{
one => 1,
two => 2,
three => {
four => 4,
five => [
{
six => 6
}
],
}
}];
my $obj = $obj->array_to_nested_object($hash, %method_options)
$array->[0]->one; # 1
$array->[0]->three->four; # 4
$array->[0]->three->five->[0]->six; # 6
=head2 add_new
Builds the 'new' method for the Anonymous::Object. Expects param $new to be a HashRef of default values.
$obj->add_new({
one => 1,
two => 2,
three => {
four => 4,
five => [
{
six => 6
}
],
}
});
=head2 add_methods
Add multiple methods to the Anonymous::Object. Expects param $methods to be a ArrayRef of HashRefs that represent a method..
my $anon = Anonymous::Object->new({});
$anon->add_methods([
{
name => 'test_accessor',
clearer => 1,
predicate => 1,
get => 1,
set => 1,
ref => 1,
type => 'Str',
reftype => 1,
default => 'xyz',
},
{
name => 'test_accessor2',
set => 1,
type => 'HashRef',
default => { a => 1, b => 2 },
}
]);
$anon->build;
=head2 add_method
Add a method to the Anonymous::Object. Expects param $method to be a HashRef.
my $anon = Anonymous::Object->new({});
$anon->add_method({
name => 'test_accessor',
clearer => 1,
predicate => 1,
get => 1,
set => 1,
ref => 1,
reftype => 1,
type => 'Str',
default => 'xyz',
});
$anon->build;
=head3 name
The name of the Anonymous::Object method.
=head3 clearer
Generates a clearer method.
$self->clear_$name;
=head3 predicate
Generates a predicate method.
$self->has_$name;
=head3 get
Generates a get method.
$self->get_$name;
=head3 set
Generates a set method.
$self->set_$name;
=head3 ref
Generates a ref method.
$self->ref_$name;
=head3 reftype
Generates a reftype method.
$self->reftype_$name;
=head3 type
Specify a type check for the set method.
=head3 autotype
Auto detect types based on the passed default values.
=head3 default
Set a default value for the method.
=head2 build
Build/Generate the Anonymous::Object. Expects no params.
$obj->build()
=head2 stringify_struct
Stringify a perl data structure. Expects param $struct to be any value including undef.
$obj->stringify_struct($struct)
=head2 add_type
Add a type constaint to the Anonymous::Object. Expects param $value to be a Str.
$obj->add_type('Str');
=head2 identify_type
Identify the type of the passed data. Expects param $value to be any value including undef.
my $type = $obj->identify_type($data);
=head1 ACCESSORS
=head2 object_name
get or set object_name.
$obj->object_name;
$obj->object_name($value);
=head2 default
get or set default.
$obj->default;
$obj->default($value);
=head2 meta
get or set meta.
$obj->meta;
$obj->meta($value);
=head2 types
get or set types.
$obj->types;
$obj->types($value);
=head2 type_library
get or set type_library.
$obj->type_library;
$obj->type_library($value);
=head2 type_map
get or set type_map.
$obj->type_map;
$obj->type_map($value);
=head1 AUTHOR
LNATION, C<< <email at lnation.org> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-anonymous::object at rt.cpan.org>, or through