Ambrosia::core::Object - an abstract base class for classes that are created by Ambrosia::Meta.
version 0.010
# file Foo.pm package Foo; use strict; use Ambrosia::Meta; class { public => [qw/foo_pub1 foo_pub2/], protected => [qw/foo_pro1 foo_pro2/], private => [qw/foo_pri1 foo_pri2/], }; sub _init { my $self = shift; $self->SUPER::_init(@_); $self->foo_pub1 ||= 'foo_pub1'; $self->foo_pub2 ||= 'foo_pub2'; $self->foo_pri1 ||= 'foo_pri1'; $self->foo_pri2 ||= 'foo_pri2'; $self->foo_pro1 ||= 2; $self->foo_pro2 ||= 'foo_pro2'; } sub count { shift->foo_pro1 } 1; # file Bar.pm package Bar; use strict; use Ambrosia::Meta; class sealed { extends => [qw/Foo/], public => [qw/bar_pub1 bar_pub2/], protected => [qw/bar_pro1 bar_pro2/], private => [qw/bar_pri1 bar_pri2 list/], }; sub _init { my $self = shift; #Ignore all input data $self->SUPER::_init(foo_pri1=>4); $self->bar_pub1 = 'bar_pub1'; $self->bar_pub2 = 'bar_pub2'; $self->bar_pri1 = 'bar_pri1'; $self->bar_pri2 = 'bar_pri2'; $self->bar_pro1 = 'bar_pro1'; $self->bar_pro2 = 'bar_pro2'; $self->list = [] unless defined $self->list; push @{$self->list}, (new Foo(foo_pub1 => 'list1.1', foo_pub2 => 'list1.2'), new Foo(foo_pub1 => 'list2.1', foo_pub2 => 'list2.2') ); } 1; # file test.pl #!/usr/bin/perl -w use strict; use Data::Dumper; use Bar; my $obj1 = new Bar; $obj1->foo_pub1 = 1; print $obj1->foo_pub1, "\n"; use Time::HiRes qw ( time ); my $s = 0; my $t1=time; foreach ( 1..10000 ) { $s += $obj1->count; } print "time:",(time-$t1), "\n"; print "sum=$s\n"; $t1=time; foreach ( 1..10000 ) { my $obj1 = Bar->new; } print "time:",(time-$t1), "\n"; print Dumper($obj1); my $r; read STDIN, $r, 1; print $r; ############################################################################
Ambrosia::core::Object is the abstract base class for classes that are created by Ambrosia::Meta.
Ambrosia::core::Object
This method creates the new object of specified type. Input params is a hash or a reference to hash. The hash keys are the fields of the class that have been created by Ambrosia::Meta
_init is called from new to initialize fields (include private and protected fields) of an object by input parameters. This method may be overriden in child class.
_init
new
Returns public fields of class (include public fields of parent classes) that have been created by Ambrosia::Meta.
Returns values of specified public fields or values of all public fields if not stated.
Returns the dump of the object as string to store. print $obj->string_dump()
print $obj->string_dump()
Static method of class (package). Restores the object from dump created early by method string_dump(). my $obj = Ambrosia::core::Object::string_restore($obj->string_dump())
string_dump()
my $obj = Ambrosia::core::Object::string_restore($obj->string_dump())
WARNING! When restoring an object from dump this method does not call _init. Also see documentation in Data::Serializer.
If $error_ignore is true all errors occurred in this method will be ignored.
$error_ignore
Returns the object as hash structure. You can also declare methods of class that will be called and their returned values will also be stored in hash.
Rule for declare methods. real_method_of_class:name_for_key_in_hash(params_for_method_split_by_comma){methods_of_returned_objects}
real_method_of_class:name_for_key_in_hash(params_for_method_split_by_comma){methods_of_returned_objects}
Or in BNF (in square brackets is optional): methods := method_declare[,methods] method_declare := method[{methods_of_class_of_returned_object}] method := real_name_of_method_in_class[:alias[(params)]] params := param[,params] #just any data methods_of_class_of_returned_object := methods #if method return reference to another object then you can present any methods of that result object and etc.
For example:
{ package Bar; Ambrosia::Meta; class { public => [qw/b1 b2/], }; sub join { my $self = shift; return $self->b1 . ';' . $self->b2; } 1; } { package Foo; Ambrosia::Meta; class { public => [qw/f1 f2 bar/], }; sub m1 { return 'method of m1 run with: ' . $_[1]; } sub getBar { return $_[0]->bar; } sub dump { my $self = shift; return $self->as_hash(1, 'm1:method1(123)', 'getBar{join}'); } 1; }
Makes copy of the source object to the destination object (only public fields are copied). $source->copy_to($dest)
$source->copy_to($dest)
Makes clone of object. If $deep is true it will create a deep clone. And vice versa if $deep is false it will create a simple clone. If a field is a reference to some data then the field of simple clone will also refer to these data.
$deep
Note for deep clone: if any field is the reference to any object, this object will also be cloned but only if it has the method clone
clone
Converts the object to the XML Document (XML::LibXML::Document). Is called with the following params
The xml document. Optional. If not defined, the document will be created.
Charset of xml document if it has not been defined. Optional. Default is 'utf8'.
Name of root node. Optional. If not presented, the class name will be used.
True or false in perl notation (1 or 0). Optional. Default value is true. In the case of error and if $bool is true, the error will be ignored.
$bool
item methods
Optional. See as_hash.
Compares two objects. $object->equal($other_object, $deep, $identical); If $deep is true, deep compare will be executed. If $identical is true, only references of objects will be compared.
my $obj = new SomeObject(); my $ref = $obj; my $obj2 = new SomeObject(); $obj->equal($ref,0,1); #is true $obj->equal($obj2,0,1); #is false $obj->equal($obj2); #is true $obj->equal($obj2,1); #is true
XML::LibXML Data::Serializer Ambrosia::error::Exceptions Ambrosia::core::Nil
Not tested.
Please report bugs relevant to Ambrosia to <knm[at]cpan.org>.
Ambrosia
Ambrosia::Meta
Copyright (C) 2010-2012 Nickolay Kuritsyn. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Nikolay Kuritsyn (knm[at]cpan.org)
To install Ambrosia, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Ambrosia
CPAN shell
perl -MCPAN -e shell install Ambrosia
For more information on module installation, please visit the detailed CPAN module installation guide.