Class-EHierarchy-2.00/0000755000175000001440000000000013035727240014262 5ustar acorlissusersClass-EHierarchy-2.00/t/0000755000175000001440000000000013035727240014525 5ustar acorlissusersClass-EHierarchy-2.00/t/99_pod.t0000644000175000001440000000022112741127427016014 0ustar acorlissusers#!/usr/bin/perl use Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok(); Class-EHierarchy-2.00/t/98_pod_coverage.t0000644000175000001440000000032112741127427017667 0ustar acorlissusers#!/usr/bin/perl use Test::More; eval "use Test::Pod::Coverage 1.00"; plan skip_all => "Test::Pod::Coverage 1.00 required for testing POD coverage" if $@; all_pod_coverage_ok( { private => [ qr/^_/ ] } ); Class-EHierarchy-2.00/t/07_type_methods.t0000644000175000001440000000553113031277351017727 0ustar acorlissusers# 07_type_methods.t # # Tests the data type aware property methods use Test::More tests => 42; use strict; use warnings; package MyClass; use vars qw(@ISA @_properties); use Class::EHierarchy qw(:all); @ISA = qw(Class::EHierarchy); @_properties = ( [ CEH_PUB | CEH_ARRAY, 'array', [qw(foo bar)] ], [ CEH_PUB | CEH_HASH, 'hash', {qw(foo bar)} ], ); 1; package main; my $obj = new MyClass; my ( $rv, @rv, %rv ); # Create our objects ok( defined $obj, 'class object instantiation - 1' ); # Check initialized values @rv = $obj->get('array'); %rv = $obj->get('hash'); is( scalar @rv, 2, 'array initialization - 1' ); is( $rv[0], 'foo', 'array initialization - 2' ); is( scalar keys %rv, 1, 'hash initialization - 1' ); is( $rv{foo}, 'bar', 'hash initialization - 2' ); # Test array methods is( $obj->push('array'), 2, 'push - 1' ); is( $obj->push(qw(array roo)), 3, 'push - 2' ); is( $obj->push(qw(array x y)), 5, 'push - 3' ); is( $obj->pop('array'), 'y', 'pop - 1' ); is( $obj->unshift('array'), 4, 'unshift - 1' ); is( $obj->unshift(qw(array i)), 5, 'unshift - 2' ); is( $obj->unshift(qw(array j k)), 7, 'unshift - 3' ); is( $obj->shift('array'), 'j', 'unshift - 4' ); # Test hash methods ok( $obj->exists(qw(hash foo)), 'exists - 1' ); ok( !$obj->exists(qw(hash bar)), 'exists - 2' ); @rv = $obj->keys('hash'); is( scalar @rv, 1, 'keys - 1' ); is( $rv[0], 'foo', 'keys - 2' ); # Test unified methods # # Test merge ok( $obj->merge(qw(array 1 a 3 b 5 c)), 'array merge - 1' ); @rv = $obj->get('array'); is( $rv[0], 'k', 'array merge - 2' ); is( $rv[1], 'a', 'array merge - 3' ); is( $rv[3], 'b', 'array merge - 4' ); is( $rv[5], 'c', 'array merge - 5' ); ok( $obj->merge(qw(hash x y j k)), 'hash merge - 1' ); %rv = $obj->get('hash'); is( $rv{foo}, 'bar', 'hash merge - 2' ); is( $rv{x}, 'y', 'hash merge - 2' ); is( $rv{j}, 'k', 'hash merge - 2' ); # Test subset @rv = $obj->subset(qw(array 0 1 3 5)); is( scalar @rv, 4, 'array subset - 1' ); is( $rv[0], 'k', 'array subset - 2' ); is( $rv[3], 'c', 'array subset - 3' ); @rv = $obj->subset(qw(hash foo x)); is( scalar @rv, 2, 'hash subset - 1' ); is( $rv[0], 'bar', 'hash subset - 2' ); is( $rv[1], 'y', 'hash subset - 3' ); # Test remove ok( $obj->remove(qw(array 1 3 5)), 'array remove - 1' ); @rv = $obj->get('array'); is( $rv[1], 'foo', 'array remove - 2' ); is( $rv[2], 'roo', 'array remove - 3' ); ok( $obj->remove(qw(hash foo x)), 'hash remove - 1' ); %rv = $obj->get('hash'); is( $rv{j}, 'k', 'hash remove - 2' ); ok( !exists $rv{x}, 'hash remove - 3' ); # Test empty ok( $obj->empty('array'), 'array empty - 1' ); @rv = $obj->get('array'); is( scalar @rv, 0, 'array empty - 2' ); ok( $obj->empty('hash'), 'hash empty - 1' ); %rv = $obj->get('hash'); is( scalar keys %rv, 0, 'hash empty - 2' ); Class-EHierarchy-2.00/t/02_object_hierarchy.t0000644000175000001440000000354313031201410020502 0ustar acorlissusers# 02_object_hierarchy.t # # Tests the tracking of object relationships use Test::More tests => 27; use strict; use warnings; use Class::EHierarchy; sub dumpObjInfo { my $obj = shift; my ( $id, $parent, $children ); $id = $$obj; $parent = defined $obj->parent ? ${ $obj->parent } : 'undef'; $children = join ' ', map {$$_} $obj->children; warn "ID $id: P: $parent C: $children\n"; } my $obj1 = new Class::EHierarchy; my $obj2 = new Class::EHierarchy; my $obj3 = new Class::EHierarchy; my $obj4 = new Class::EHierarchy; # Test isStale $obj1->DESTROY; ok( $obj1->isStale, 'isStale 1' ); ok( !$obj1->root, 'isStale 2' ); ok( !$obj1->parent, 'isStale 3' ); ok( !$obj1->children, 'isStale 4' ); ok( !$obj1->siblings, 'isStale 5' ); ok( !$obj1->descendents, 'isStale 6' ); $obj1 = new Class::EHierarchy; is( $$obj1, 0, 'recover ID 1' ); # Test basic adoption ok( !$obj1->adopt($obj1), 'Adopt Self 1' ); ok( $obj1->adopt($obj2), 'Adopt Child 1' ); is( $obj2->children, 0, 'Children 1' ); is( $obj1->children, 1, 'Children 2' ); ok( !$obj2->adopt($obj1), 'Adopt Parent 1' ); ok( $obj2->adopt($obj3), 'Adopt Child 2' ); is( $obj1->children, 1, 'Children 3' ); is( $obj2->children, 1, 'Children 4' ); is( $obj1->descendents, 2, 'descendents 1' ); ok( !$obj3->adopt($obj1), 'Adopt Root 1' ); # Test parent is( $obj1->parent, undef, 'Parent 1' ); is( $obj3->parent, $obj2, 'Parent 2' ); # Test root is( $obj1->root, $obj1, 'Root 1' ); is( $obj3->root, $obj1, 'Root 2' ); # Test descendents my @children = $obj1->descendents; is( $children[0], $obj2, 'descendent 1' ); is( $children[1], $obj3, 'descendent 2' ); # Adopt the root with obj4 ok( $obj4->adopt($obj1), 'Adopt Child 3' ); # Test disowning ok( $obj1->disown($obj2), 'Disown 1' ); is( $obj1->children, 0, 'Children 6' ); is( $obj2->parent, undef, 'Parent 3' ); Class-EHierarchy-2.00/t/06_methods.t0000644000175000001440000000632213031201454016653 0ustar acorlissusers# 06_methods.t # # Tests the method scoping use Test::More tests => 21; use strict; use warnings; package Foo; use vars qw(@ISA @_methods); use Class::EHierarchy qw(:all); @ISA = qw(Class::EHierarchy); @_methods = ( [ CEH_PRIV, qw(mpriv) ], [ CEH_RESTR, qw(mrestr) ], [ CEH_PUB, qw(mpub) ], ); sub mpriv { my $self = shift; return 2; } sub mrestr { my $self = shift; return 4; } sub mpub { my $self = shift; return 8; } sub callpriv { my $self = shift; my $obj = shift; return $obj->mpriv; } sub callrestr { my $self = shift; my $obj = shift; return $obj->mrestr; } 1; package Bar; use vars qw(@ISA); use Class::EHierarchy qw(:all); @ISA = qw(Foo); sub _initialize { my $self = shift; my @args = @_; _declMethod( CEH_PRIV, qw(mpriv) ); _declMethod( CEH_RESTR, qw(mrestr) ); _declMethod( CEH_PUB, qw(mpub) ); return 1; } sub mpriv { my $self = shift; return 4; } sub mrestr { my $self = shift; return 8; } sub mpub { my $self = shift; return 16; } sub callpriv { my $self = shift; my $obj = shift; return $obj->mpriv; } sub callrestr { my $self = shift; my $obj = shift; return $obj->mrestr; } 1; package main; my $class1a = new Foo; my $class1b = new Foo; my $class2a = new Bar; my $class2b = new Bar; my $rv; # Test subclass instantiation ok( defined $class1a, 'Created object for class Foo 1' ); ok( defined $class1b, 'Created object for class Foo 2' ); ok( $class1a->isa('Foo'), 'Verify class Foo 1' ); ok( $class1a->isa('Class::EHierarchy'), 'Verify class Foo inheritance 1' ); ok( defined $class2a, 'Created object for class Bar 1' ); ok( defined $class2b, 'Created object for class Bar 2' ); ok( $class2a->isa('Bar'), 'Verify class Bar 1' ); ok( $class2a->isa('Class::EHierarchy'), 'Verify class Bar inheritance 1' ); ok( $class2a->isa('Foo'), 'Verify class Bar inheritance 2' ); # Private method tests # # Private calls should fail ok( !$class1a->mpriv(), 'main calling Foo Private Method 1' ); # Call from same class should succeed is( $class1a->callpriv($class1b), 2, 'Foo calling Foo Private Method 1' ); is( $class2a->callpriv($class2b), 4, 'Bar calling Bar Private Method 1' ); # Call from different class shoud fail ok( !$class2a->callpriv($class1a), 'Bar calling Foo Private Method 1' ); ok( !$class1a->callpriv($class2a), 'Foo calling Bar Private Method 1' ); # Restricted method tests # # Restricted calls should fail ok( !$class1a->mrestr(), 'main calling Foo Restricted Method 1' ); # Call from same class should succeed is( $class1a->callrestr($class1b), 4, 'Foo calling Foo Restricted Method 1' ); is( $class2a->callrestr($class2b), 8, 'Bar calling Bar Restricted Method 1' ); # Call from subclass should succeed is( $class2a->callrestr($class1a), 4, 'Bar calling Foo Restricted Method 1' ); # Call from non-subclass should fail ok( !$class1a->callrestr($class2a), 'Foo calling Bar Restricted Method 1' ); # Public method tests # # Calls should succeed is( $class1a->mpub, 8, 'Foo Public Method 1' ); is( $class2a->mpub, 16, 'Bar Public Method 1' ); Class-EHierarchy-2.00/t/01_init.t0000644000175000001440000000100713031201377016145 0ustar acorlissusers# 01_init.t # # Tests for proper loading of the module use Test::More tests => 6; use strict; use warnings; ok( eval 'require Class::EHierarchy;', 'Loaded Class::EHierarchy' ); my $obj = new Class::EHierarchy; ok( defined $obj, 'Created object 1' ); ok( $obj->isa('Class::EHierarchy'), 'Verified object class' ); ok( $$obj == 0, 'Verified object ID 1' ); my $obj2 = new Class::EHierarchy; ok( defined $obj2, 'Created object 2' ); ok( $$obj2 == 1, 'Verified object ID 2' ); Class-EHierarchy-2.00/t/04_alias.t0000644000175000001440000000615413031201436016302 0ustar acorlissusers# 04_alias.t # # Tests the tracking of object aliases use Test::More tests => 46; use strict; use warnings; use Class::EHierarchy; sub dumpObjInfo { my $obj = shift; my ( $id, $parent, $children ); $id = $$obj; $parent = defined $obj->parent ? ${ $obj->parent } : 'undef'; $children = join ' ', map {$$_} $obj->children; warn "ID $id: P: $parent C: $children\n"; } my $obj1 = new Class::EHierarchy; my $obj2 = new Class::EHierarchy; my $obj3 = new Class::EHierarchy; my $obj4 = new Class::EHierarchy; # pre-flight ok( defined $obj1, 'instantiation 1' ); ok( defined $obj2, 'instantiation 2' ); ok( defined $obj3, 'instantiation 3' ); ok( defined $obj4, 'instantiation 4' ); # Pre-emptively apply aliases to 2 & 3 ok( $obj2->alias('o2'), 'alias 1' ); ok( $obj3->alias('o3'), 'alias 2' ); # Test realiasing ok( !$obj3->alias('o33'), 'realias 1'); # Check pre-adoption aliases is( $obj2->getByAlias('o2'), $obj2, 'pre-adoption alias 1'); is( $obj3->getByAlias('o3'), $obj3, 'pre-adoption alias 2'); # Build object hierarchy: # obj1 -> obj2 -> obj3, obj4 ok( $obj2->adopt( $obj3, $obj4 ), 'adopt 1' ); ok( $obj1->adopt($obj2), 'adopt 2' ); # Test realiasing ok( !$obj3->alias('o33'), 'realias 2'); # Test inherited aliases via every object is( $obj1->getByAlias('o2'), $obj2, 'get alias 1' ); is( $obj2->getByAlias('o2'), $obj2, 'get alias 2' ); is( $obj3->getByAlias('o2'), $obj2, 'get alias 3' ); is( $obj4->getByAlias('o2'), $obj2, 'get alias 4' ); is( $obj1->getByAlias('o3'), $obj3, 'get alias 5' ); is( $obj2->getByAlias('o3'), $obj3, 'get alias 6' ); is( $obj3->getByAlias('o3'), $obj3, 'get alias 7' ); is( $obj4->getByAlias('o3'), $obj3, 'get alias 8' ); # Test non-existent alias is( $obj1->getByAlias('o1'), undef, 'get alias 9' ); is( $obj4->getByAlias(), undef, 'get alias 10' ); # Alias o1 and o4 ok( $obj1->alias('o1'), 'alias 1' ); ok( $obj4->alias('o4'), 'alias 2' ); # Test new aliases via every object is( $obj1->getByAlias('o1'), $obj1, 'get alias 11' ); is( $obj2->getByAlias('o1'), $obj1, 'get alias 12' ); is( $obj3->getByAlias('o1'), $obj1, 'get alias 13' ); is( $obj4->getByAlias('o1'), $obj1, 'get alias 14' ); is( $obj1->getByAlias('o4'), $obj4, 'get alias 15' ); is( $obj2->getByAlias('o4'), $obj4, 'get alias 16' ); is( $obj3->getByAlias('o4'), $obj4, 'get alias 17' ); is( $obj4->getByAlias('o4'), $obj4, 'get alias 18' ); # Disown o3 and test aliases again ok( $obj2->disown($obj3), 'disown 1' ); is( $obj1->getByAlias('o3'), undef, 'get alias 19'); is( $obj4->getByAlias('o3'), undef, 'get alias 19'); is( $obj1->getByAlias('o2'), $obj2, 'get alias 20'); is( $obj4->getByAlias('o2'), $obj2, 'get alias 21'); is( $obj3->getByAlias('o1'), undef, 'get alias 22'); is( $obj3->getByAlias('o3'), $obj3, 'get alias 23'); # Disown o2 and test aliases ok( $obj1->disown($obj2), 'disown 2'); is($obj1->getByAlias('o2'), undef, 'get alias 24'); is($obj1->getByAlias('o4'), undef, 'get alias 25'); is($obj1->getByAlias('o1'), $obj1, 'get alias 26'); is($obj2->getByAlias('o2'), $obj2, 'get alias 27'); is($obj2->getByAlias('o4'), $obj4, 'get alias 28'); is($obj4->getByAlias('o2'), $obj2, 'get alias 29'); Class-EHierarchy-2.00/t/05_properties.t0000644000175000001440000001361213031271777017422 0ustar acorlissusers# 05_properties.t # # Tests the various property types and scoping use Test::More tests => 61; use strict; use warnings; package MyPi; use vars qw(@ISA @_properties); use Class::EHierarchy qw(:all); @ISA = qw(Class::EHierarchy); @_properties = ( [ CEH_PUB | CEH_SCALAR, 'pi', 3.14 ], [ CEH_RESTR | CEH_SCALAR, '2xpi' ], [ CEH_PRIV | CEH_SCALAR, '3xpi' ], ); sub _initialize { my $self = shift; my @args = @_; # Initialize the double/triple PIs $self->set( '2xpi', $self->get('pi') * 2 ); $self->set( '3xpi', $self->get('pi') * 3 ); return 1; } sub double { my $self = shift; return $self->get('2xpi'); } sub triple { my $self = shift; return $self->get('3xpi'); } sub dump { my $self = shift; return $self->properties; } 1; package MySquaredPi; use vars qw(@ISA @_properties); use Class::EHierarchy qw(:all); @ISA = qw(MyPi); @_properties = ( [ CEH_PUB | CEH_SCALAR, 'pi', 3.14**2 ], [ CEH_PUB | CEH_SCALAR, 'custompi' ], [ CEH_PUB | CEH_SCALAR | CEH_NO_UNDEF, 'noundef', 5 ], [ CEH_PUB | CEH_REF, 'ref' ], [ CEH_PUB | CEH_GLOB, 'glob' ], [ CEH_PUB | CEH_CODE, 'code' ], [ CEH_PUB | CEH_ARRAY, 'array' ], [ CEH_PUB | CEH_HASH, 'hash' ], ); sub _initialize { my $self = shift; my @args = @_; $self->set( 'custompi', $self->get('pi') * $args[0] ); return 1; } sub double { my $self = shift; return $self->get('2xpi'); } sub triple { my $self = shift; return $self->get('3xpi'); } sub dynprop { my $self = shift; _declProperty( $self, '5xpi', CEH_PRIV | CEH_SCALAR ); return $self->set( '5xpi', $self->get('pi') * 5 ); } sub quintuple { my $self = shift; return $self->get('5xpi'); } sub dump { my $self = shift; return $self->properties; } 1; package MyRedundantPi; use vars qw(@ISA); use Class::EHierarchy qw(:all); @ISA = qw(MyPi); 1; package main; my $mypi = new MyPi; my $mysqpi = new MySquaredPi 12; my $myrpi = new MyRedundantPi; my $rv; # Create our objects ok( defined $mypi, 'class object instantiation - 1' ); ok( defined $mysqpi, 'subclass object instantiation - 1' ); ok( defined $myrpi, 'subclass object instantiation - 2' ); # Check the public property values is( $mypi->get('pi'), 3.14, 'public property - 1' ); is( $mysqpi->get('pi'), 3.14**2, 'overriden public property - 1' ); is( $myrpi->get('pi'), 3.14, 'inherited public property - 1' ); # Check restricted property values is( $mypi->get('2xpi'), undef, 'restricted property - 1' ); is( $mypi->double, 3.14 * 2, 'restricted property - 2' ); is( $mysqpi->get('2xpi'), undef, 'restricted property - 3' ); is( $mysqpi->double, 3.14**2 * 2, 'restricted property - 4' ); is( $myrpi->get('2xpi'), undef, 'restricted property - 5' ); is( $myrpi->double, 3.14 * 2, 'restricted property - 6' ); # Check private property values is( $mypi->get('3xpi'), undef, 'private property - 1' ); is( $mypi->triple, 3.14 * 3, 'private property - 2' ); is( $mysqpi->get('3xpi'), undef, 'private property - 3' ); is( $mysqpi->triple, undef, 'private property - 4' ); is( $myrpi->get('3xpi'), undef, 'private property - 5' ); is( $myrpi->triple, 3.14 * 3, 'private property - 6' ); # Safety check is( $mypi->get('MyPi*3xpi'), undef, 'private property - 7' ); # Check arg initialization code is( $mysqpi->get('custompi'), 3.14**2 * 12, 'arg init property - 1' ); # Check dynamic property ok( $mysqpi->dynprop, 'dynamic property - 1' ); is( $mysqpi->get('5xpi'), undef, 'private property - 8' ); is( $mysqpi->quintuple, 3.14**2 * 5, 'private property - 9' ); # Test noundef ok( !$mysqpi->set( 'noundef', undef ), 'no undef - 1' ); is( $mysqpi->get('noundef'), 5, 'no undef - 2' ); ok( $mysqpi->set( 'noundef', 100 ), 'no undef - 3' ); is( $mysqpi->get('noundef'), 100, 'no undef - 4' ); ok( !$mysqpi->set( 'noundef', $mypi ), 'no ref - 1' ); # Test code refs my $sub = sub {1}; ok( !$mysqpi->set( 'code', 21 ), 'code - 1' ); ok( $mysqpi->set( 'code', $sub ), 'code - 2' ); is( $mysqpi->get('code'), $sub, 'code - 3' ); ok( $mysqpi->set('code'), 'code - 4' ); is( $mysqpi->get('code'), undef, 'code - 5' ); # Test glob refs ok( !$mysqpi->set( 'glob', 21 ), 'glob - 1' ); ok( $mysqpi->set( 'glob', \*STDOUT ), 'glob - 2' ); is( $mysqpi->get('glob'), \*STDOUT, 'glob - 3' ); ok( $mysqpi->set('glob'), 'glob - 4' ); is( $mysqpi->get('glob'), undef, 'glob - 5' ); # Test refs ok( !$mysqpi->set( 'ref', 21 ), 'ref - 1' ); ok( $mysqpi->set( 'ref', \$rv ), 'ref - 2' ); is( $mysqpi->get('ref'), \$rv, 'ref - 3' ); ok( $mysqpi->set('ref'), 'ref - 4' ); is( $mysqpi->get('ref'), undef, 'ref - 5' ); # Test array my @array = qw(foo bar); my @rv; ok( $mysqpi->set( 'array', @array ), 'array - 1' ); @rv = $mysqpi->get('array'); is( scalar @rv, 2, 'array - 2' ); is( $rv[0], 'foo', 'array - 3' ); ok( $mysqpi->set('array'), 'array - 4' ); @rv = $mysqpi->get('array'); is( scalar @rv, 0, 'array - 5' ); # Test hash my %hash = ( foo => 'one', bar => 'two' ); my %rv; ok( $mysqpi->set( 'hash', %hash ), 'hash - 1' ); %rv = $mysqpi->get('hash'); is( scalar keys %rv, 2, 'hash - 2' ); ok( exists $rv{foo}, 'hash - 3' ); ok( $mysqpi->set('hash'), 'hash - 4' ); %rv = $mysqpi->get('hash'); is( scalar keys %rv, 0, 'hash - 5' ); # Test properties my @props = $mysqpi->properties; is( scalar @props, 8, 'property names - 1' ); ok( !grep({ $_ eq '2xpi' } @props), 'property names - 2' ); @props = $mysqpi->dump; is( scalar @props, 10, 'property names - 3' ); ok( grep({ $_ eq '2xpi' } @props), 'property names - 4' ); ok( !grep({ $_ eq '3xpi' } @props), 'property names - 5' ); @props = $mypi->dump; is( scalar @props, 3, 'property names - 6' ); ok( grep({ $_ eq '2xpi' } @props), 'property names - 7' ); ok( grep({ $_ eq '3xpi' } @props), 'property names - 8' ); Class-EHierarchy-2.00/t/03_class_hierarchy.t0000644000175000001440000000435413031304055020354 0ustar acorlissusers# 03_class_hierarchy.t # # Tests the hierarchal class initialization code use Test::More tests => 22; use strict; use warnings; our $counter1 = 0; our $counter2 = 0; package MyClass; use Class::EHierarchy qw(:all); use vars qw(@ISA); @ISA = qw(Class::EHierarchy); sub _initialize { my $obj = shift; #warn "Initializing MyClass for $obj\n"; $counter1 = 200; } sub _deconstruct { my $obj = shift; #warn "Deconstructing MyClass for $obj\n"; $counter1 = 100; } package MySubClass; use Class::EHierarchy qw(:all); use vars qw(@ISA); @ISA = qw(MyClass); sub _initialize { my $obj = shift; #warn "Initializing MySubClass for $obj\n"; $counter2 = $counter1**2; } sub _deconstruct { my $obj = shift; #warn "Deconstructing MySubClass for $obj\n"; $counter2 = $counter1 / 4; } package main; my @objects; my $obj1 = new MyClass; ok( $obj1, 'create parent 1' ); is( $counter1, 200, 'counter1 check 1' ); is( $counter2, 0, 'counter2 check 1' ); my $obj2 = new MySubClass; ok( $obj2, 'create child 1' ); ok( $obj1->adopt($obj2), 'adopt child 1' ); is( $counter1, 200, 'counter1 check 2' ); is( $counter2, 40000, 'counter2 check 2' ); @objects = Class::EHierarchy::_dumpObjects(); is( @objects, 2, 'object count 1' ); @objects = (); $obj2 = undef; is( $counter1, 200, 'counter1 check 3' ); is( $counter2, 40000, 'counter2 check 3' ); @objects = Class::EHierarchy::_dumpObjects(); is( @objects, 2, 'object count 2' ); @objects = (); $obj1 = undef; is( $counter1, 100, 'counter1 check 4' ); is( $counter2, 50, 'counter2 check 4' ); @objects = Class::EHierarchy::_dumpObjects(); is( @objects, 0, 'object count 3' ); @objects = (); $obj1 = new MyClass; $obj2 = new MySubClass; ok( $obj1, 'create parent 2' ); ok( $obj2, 'create child 2' ); ok( $obj1->adopt($obj2), 'adopt child 2' ); $obj2 = undef; @objects = Class::EHierarchy::_dumpObjects(); is( @objects, 2, 'object count 4' ); @objects = (); ok( $obj1->disown( $obj1->children ), 'disown child 1' ); @objects = Class::EHierarchy::_dumpObjects(); is( @objects, 1, 'object count 5' ); ok( MySubClass->conceive($obj1), 'conceive child 1' ); @objects = Class::EHierarchy::_dumpObjects(); is( @objects, 2, 'object count 6' ); Class-EHierarchy-2.00/CHANGELOG0000644000175000001440000000210713034742440015472 0ustar acorlissusersCHANGELOG v2.00 (2017/01/23) ================== --Complete rewrite --New API v0.93 (2013/07/06) ================== --Modified DESTROY method to call all superclass _deconstruct methods from the bottom up v0.92 (2013/06/18) ================== --Added more relationship methods --Added alias functionality --Updated & improved documentation --Bumped minimum required perl to 5.8.3 v0.91 (2012/01/30) ================== --Added automated loading of methods, properties, and default property values via class variables --Fixed bug where every object created rewraps scoped methods in the class symbol table --Improved documentation with better examples and explanations v0.90 (2011/08/18) ================== --Completely rewritten into something (hopefully) more useful than the original incarnation. Completely breaks old API and loses some functionality, while gaining some new functionality. Possibly more congruent with the actual OOP concepts it claims to follow. v0.06 (2003/03/18) ================== --Old version. Should be ashamed of itself. May it die in obscurity. Class-EHierarchy-2.00/MANIFEST0000644000175000001440000000062213035727240015413 0ustar acorlissusersMakefile.PL CHANGELOG README INSTALL MANIFEST LICENSE lib/Class/EHierarchy.pm t/01_init.t t/02_object_hierarchy.t t/03_class_hierarchy.t t/04_alias.t t/05_properties.t t/06_methods.t t/07_type_methods.t t/98_pod_coverage.t t/99_pod.t META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Class-EHierarchy-2.00/lib/0000755000175000001440000000000013035727240015030 5ustar acorlissusersClass-EHierarchy-2.00/lib/Class/0000755000175000001440000000000013035727240016075 5ustar acorlissusersClass-EHierarchy-2.00/lib/Class/EHierarchy.pm0000644000175000001440000020320513035727145020464 0ustar acorlissusers# Class::EHierarchy -- Base class for hierarchally ordered objects # # (c) 2017, Arthur Corliss # # $Id: lib/Class/EHierarchy.pm, 2.00 2017/01/09 08:47:12 acorliss Exp $ # # This software is licensed under the same terms as Perl, itself. # Please see http://dev.perl.org/licenses/ for more information. # ##################################################################### ##################################################################### # # Environment definitions # ##################################################################### package Class::EHierarchy; use 5.008003; use strict; use warnings; use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS); use base qw(Exporter); use Carp; use Scalar::Util qw(weaken); ($VERSION) = ( q$Revision: 2.00 $ =~ /(\d+(?:\.(\d+))+)/sm ); # Ordinal indexes for the @objects element records use constant CEH_OREF => 0; use constant CEH_PID => 1; use constant CEH_PKG => 2; use constant CEH_CLASSES => 3; use constant CEH_CREF => 4; # Ordinal indexes for the @properties element records use constant CEH_PATTR => 0; use constant CEH_PNAME => 1; use constant CEH_PPKG => 1; use constant CEH_PVAL => 2; # Property attribute masks use constant CEH_PATTR_SCOPE => 7; use constant CEH_PATTR_TYPE => 504; # Property attribute scopes use constant CEH_PUB => 1; use constant CEH_RESTR => 2; use constant CEH_PRIV => 4; # Property attribute types use constant CEH_SCALAR => 8; use constant CEH_ARRAY => 16; use constant CEH_HASH => 32; use constant CEH_CODE => 64; use constant CEH_REF => 128; use constant CEH_GLOB => 256; # Property flags use constant CEH_NO_UNDEF => 512; @EXPORT = qw(); @EXPORT_OK = qw(CEH_PUB CEH_RESTR CEH_PRIV CEH_SCALAR CEH_ARRAY CEH_HASH CEH_CODE CEH_REF CEH_GLOB CEH_NO_UNDEF _declProperty _declMethod ); %EXPORT_TAGS = ( all => [@EXPORT_OK] ); ##################################################################### # # Module code follows # ##################################################################### ########################################################## # Hierarchal code support ########################################################## { # Array of object references and metadata my @objects; # Array of recycled IDs availabe for use my @recoveredIDs; sub _dumpObjects { # Purpose: Provides a list of objects # Returns: List of refs # Usage: @objects = _dumpObjects(); return map { $$_[CEH_OREF] } grep {defined} @objects; } sub _getID { # Purpose: Generates and assigns a unique ID to the passed # object, and initializes the internal records # Returns: Integer # Usage: $id = _genID(); my $obj = CORE::shift; my $id = @recoveredIDs ? CORE::shift @recoveredIDs : $#objects + 1; $$obj = $id; $objects[$id] = []; $objects[$id][CEH_CREF] = []; $objects[$id][CEH_CLASSES] = []; $objects[$id][CEH_OREF] = $obj; $objects[$id][CEH_PKG] = ref $obj; weaken( $objects[$$obj][CEH_OREF] ); $id = '0 but true' if $id == 0; # Build object class list { no strict 'refs'; my ( $isaref, $tclass, $nclass, @classes, $n, $l ); my $class = ref $obj; # Get the first level of classes we're subclassed from $isaref = *{"${class}::ISA"}{ARRAY}; $isaref = [] unless defined $isaref; foreach $tclass (@$isaref) { CORE::push @classes, $tclass if $tclass ne __PACKAGE__ and "$tclass"->isa(__PACKAGE__); } # Now, recurse into parent classes. $n = 0; $l = scalar @classes; while ( $n < $l ) { foreach $tclass ( @classes[ $n .. ( $l - 1 ) ] ) { $isaref = *{"${tclass}::ISA"}{ARRAY}; $isaref = [] unless defined $isaref; foreach $nclass (@$isaref) { CORE::push @classes, $nclass if $nclass ne __PACKAGE__ and "$nclass"->isa(__PACKAGE__); } } $n = scalar @classes - $l + 1; $l = scalar @classes; } # Add our current class CORE::push @classes, $class; # Save the list foreach (@classes) { _addClass( $obj, $_ ) } } return $id; } sub _delID { # Purpose: Recovers the ID for re-use while deleting the # old data structures # Returns: Boolean # Usage: _recoverID($id); my $obj = CORE::shift; my $pid = $objects[$$obj][CEH_PID]; my @children = @{ $objects[$$obj][CEH_CREF] }; # Have the parent disown this child _disown( $objects[$pid][CEH_OREF], $obj ) if defined $pid; _disown( $obj, $objects[$_][CEH_OREF] ) if @children; # Clean up internal data structures $objects[$$obj] = undef; CORE::push @recoveredIDs, $$obj; return 1; } sub isStale { # Purpose: Checks to see if the object reference is # stale # Returns: Boolean # Usage: $rv = $obj->isStale; my $obj = CORE::shift; return not( defined $obj and defined $objects[$$obj] and defined $objects[$$obj][CEH_OREF] and $obj eq $objects[$$obj][CEH_OREF] ); } sub _addClass { # Purpose: Records a super class for the object # Returns: Boolean # Usage: $rv = _addClass($obj, $class); my $obj = CORE::shift; my $class = CORE::shift; CORE::push @{ $objects[$$obj][CEH_CLASSES] }, $class if defined $class and not grep /^$class$/s, @{ $objects[$$obj][CEH_CLASSES] }; return 1; } sub _getClasses { # Purpose: Returns a list of classes # Returns: Array # Usage: @classes = _getClasses($obj); my $obj = CORE::shift; return @{ $objects[$$obj][CEH_CLASSES] }; } sub _adopt { # Purpose: Updates the object records to establish the relationship # Returns: Boolean # Usage: $rv = _adopt($parent, @children); my $obj = CORE::shift; my @orphans = @_; my $rv = 1; my $child; foreach $child (@orphans) { next if $child->isStale; if ( !defined $objects[$$child][CEH_PID] ) { # Eligible for adoption, record the relationship $objects[$$child][CEH_PID] = $$obj; CORE::push @{ $objects[$$obj][CEH_CREF] }, $child; } else { # Already adopted if ( $objects[$$child][CEH_PID] != $$obj ) { $@ = "object $$child already adopted by another parent"; carp $@; $rv = 0; } } } # Merge aliases $obj->_mergeAliases; return $rv; } sub _disown { # Purpose: Severs the relationship between the parent and children # Returns: Boolean # Usage: $rv = _disown($parent, @children); my $obj = CORE::shift; my @orphans = @_; my $rv = 1; my ($child); foreach $child (@orphans) { if ( defined $objects[$$child][CEH_PID] and $objects[$$child][CEH_PID] == $$obj ) { # A little alias glue code $child->_pruneAliases(); # Emancipate the child $objects[$$child][CEH_PID] = undef; $objects[$$obj][CEH_CREF] = [ grep { $_ != $child } @{ $objects[$$obj][CEH_CREF] } ]; # More alias glue code $child->_mergeAliases(); } } return $rv; } sub parent { # Purpose: Returns a reference to the parent object # Returns: Object reference/undef # Usage: $ref = $obj->parent; my $obj = CORE::shift; my $parent; if ( $obj->isStale ) { $@ = 'parent method called on stale object'; carp $@; } else { $parent = $objects[$$obj][CEH_PID]; $parent = defined $parent ? $objects[$parent][CEH_OREF] : undef; } return $parent; } sub children { # Purpose: Returns a list of child objects # Returns: List of object references # Usage: @children = $obj->children; my $obj = CORE::shift; my @children; if ( $obj->isStale ) { $@ = 'children method called on stale object'; carp $@; } else { @children = @{ $objects[$$obj][CEH_CREF] }; } return @children; } sub siblings { # Purpose: Returns a list of siblings # Returns: List of object references # Usage: @sibling = $obj->siblings; my $obj = CORE::shift; my $parent; if ( $obj->isStale ) { $@ = 'siblings method called on stale object'; carp $@; } else { $parent = $objects[$$obj][CEH_PID]; $parent = $objects[$parent][CEH_OREF] if defined $parent; } return defined $parent ? $parent->children : (); } sub root { # Purpose: Returns the root object of the tree # Returns: Object reference # Usage: $root = $obj->root; my $obj = CORE::shift; my $pid = $objects[$$obj][CEH_PID]; my $parent; if ( $obj->isStale ) { $@ = 'root method called on stale object'; carp $@; } else { # Walk up the tree until we find an undefined PID $pid = $objects[$$obj][CEH_PID]; while ( defined $pid ) { $parent = $objects[$pid][CEH_OREF]; $pid = $objects[$$parent][CEH_PID]; } # The object is the root if no parent was ever found $parent = $obj unless defined $parent; } return $parent; } sub _getRefById { # Purpose: Returns an object reference by id from the objects array # Returns: Reference # Usage: $obj = _getRefById($index); my $id = CORE::shift; return defined $id ? $objects[$id][CEH_OREF] : undef; } } sub adopt { # Purpose: Formally adopts the children # Returns: Boolean # Usage: $rv = $obj->adopt(@children); my $obj = CORE::shift; my @children = @_; my $root = $obj->root; my $rv; if ( $obj->isStale ) { $rv = 0; $@ = 'adopt method called on stale object'; carp $@; } else { if ( grep { $$obj == $$_ } @children ) { $rv = 0; $@ = 'object attempted to adopt itself'; carp $@; } elsif ( grep { $root eq $_ } @children ) { $rv = 0; $@ = 'object attempted to adopt the root'; carp $@; } elsif ( grep { !defined or !$_->isa(__PACKAGE__) } @children ) { $rv = 0; $@ = 'non-eligible values passed as children for adoption'; carp $@; } else { $rv = _adopt( $obj, @children ); } } return $rv; } sub disown { # Purpose: Formally adopts the children # Returns: Boolean # Usage: $rv = $obj->adopt(@children); my $obj = CORE::shift; my @children = @_; my $rv; if ( $obj->isStale ) { $rv = 0; $@ = 'disown method called on stale object'; carp $@; } else { if ( grep { !defined or !$_->isa(__PACKAGE__) } @children ) { $rv = 0; $@ = 'non-eligible values passed as children for disowning'; carp $@; } else { $rv = _disown( $obj, @children ); } } return $rv; } sub descendents { # Purpose: Returns all descendents of the object # Returns: List of object references # Usage: @descendents = $obj->descendents; my $obj = CORE::shift; my ( @children, @descendents, $child ); if ( $obj->isStale ) { $@ = 'descendents method called on stale object'; carp $@; } else { @children = $obj->children; while (@children) { $child = CORE::shift @children; CORE::push @descendents, $child; CORE::push @children, $child->children; } } return @descendents; } sub _initHierarchy { # Purpose: Initializes the object & class hierarchal data for an object # Returns: Boolean # Usage: $rv = _initHierarchy($obj, $class, @args); my $obj = CORE::shift; my $class = CORE::shift; my @args = @_; my @classes = _getClasses($obj); my ( $rv, $tclass, %classes ); # uniq the class list and save it %classes = map { $_ => 0 } @classes; # Begin initialization from the top down foreach $tclass ( reverse @classes ) { unless ( $classes{$tclass} ) { { no strict 'refs'; # call class _initialize() $rv = defined *{"${tclass}::_initialize"} ? &{"${tclass}::_initialize"}( $obj, @args ) : 1; } # Track each class initialization so we only do # it once $classes{$tclass}++; } last unless $rv; } return $rv; } sub _destrHierarchy { # Purpose: Destroys hierarchal data for an object # Returns: Boolean # Usage: $rv = _destrHierarchy($obj); my $obj = CORE::shift; my @classes = _getClasses($obj); my $tclass; # Attempt to run all the _deconstruct methods { no strict 'refs'; foreach $tclass ( reverse @classes ) { &{"${tclass}::_deconstruct"}($obj) if defined *{"${tclass}::_deconstruct"}; } } return 1; } ########################################################## # Alias support ########################################################## { # Array of object aliases my @aliases; # Array of alias maps my @amaps; sub _initAlias { # Purpose: Initializes alias data for an object # Returns: Boolean # Usage: $rv = _initAlias($obj, $alias); my $obj = CORE::shift; my $alias = CORE::shift; # Store the object aliases and initialize a private map $aliases[$$obj] = $alias; $amaps[$$obj] = defined $alias ? { $alias => $$obj } : {}; return 1; } sub _destrAlias { # Purpose: Destroys alias data for an object # Returns: Boolean # Usage: $rv = _destrAlias($obj); my $obj = CORE::shift; my $alias = $aliases[$$obj]; my $root = $obj->root; # Remove aliases from root alias map delete $amaps[$$root]{$alias} if defined $alias and $amaps[$$root]{$alias} == $$obj; # Clean up object data $aliases[$$obj] = undef; $amaps[$$obj] = undef; return 1; } sub _mergeAliases { # Purpose: Merges an alias with the family tree alias index # Returns: Boolean # Usage: $rv = _mergeAliases($obj); my $obj = CORE::shift; my $rv = 1; my ( $child, $alias, $root ); # The alias index is associated with the root of the tree $root = $obj->root; foreach $child ( $root->descendents ) { # Skip objects without an alias next unless defined $aliases[$$child]; # Get the child's private alias index $alias = $aliases[$$child]; # Update the index if the alias is unclaimed if ( CORE::exists $amaps[$$root]{$alias} and $amaps[$$root]{$alias} != $$child ) { $@ = "alias name collision: $alias"; carp $@; $rv = 0; } else { $amaps[$$root]{$alias} = $$child; } # Store the child's prefered alias in its private index, # regardless $amaps[$$child] = { $alias => $$child }; } return $rv; } sub _pruneAliases { # Purpose: Removes all aliases from this object and its descendents # Returns: Boolean # Usage: $rv = _prunAliases($obj); my $obj = CORE::shift; my $rv = 1; my ( $root, $child, $alias ); $root = $obj->root; foreach $child ( $obj, $obj->descendents ) { # We never prune aliases from an object's own index for itself next if $$child == $$root; # Get the alias and remove it from the root's index if the # alias if valid and pointing to the child in question $alias = $aliases[$$child]; if ( defined $alias ) { delete $amaps[$$root]{$alias} if defined $alias and $amaps[$$root]{$alias} == $$child; } } return $rv; } sub alias { # Purpose: Assigns an alias to an object # Returns: Boolean # Usage: $rv = $obj->alias($name); my $obj = CORE::shift; my $alias = CORE::shift; my $rv = 1; my $root; if ( $obj->isStale ) { $rv = 0; $@ = 'alias method called on stale object'; carp $@; } else { if ( defined $aliases[$$obj] and length $aliases[$$obj] ) { $rv = 0; $@ = "object already has an alias: $aliases[$$obj]"; carp $@; } elsif ( !defined $alias or !length $alias ) { $rv = 0; $@ = 'attempt to assign an invalid alias'; carp $@; } else { # Get the root and record the alias in the object's private # map $root = $obj->root; $aliases[$$obj] = $alias; $amaps[$$obj]{$alias} = $$obj; if ( $$root != $$obj ) { # Update the root index # # Make sure no name collisions if ( CORE::exists $amaps[$$root]{$alias} and $amaps[$$root]{$alias} != $$obj ) { $@ = "alias name collision: $alias"; carp $@; $rv = 0; } else { $root = $obj->root; $amaps[$$root]{$alias} = $$obj; } } } } return $rv; } sub getByAlias { # Purpose: Returns an object reference associated with a given name # Returns: Reference # Usage: $oref = $obj->getByAlias($alias); my $obj = CORE::shift; my $alias = CORE::shift; my ( $root, $rv ); if ( $obj->isStale ) { $rv = 0; $@ = 'getByAlias method called on stale object'; carp $@; } elsif ( defined $alias ) { $root = $obj->root; $rv = $amaps[$$root]{$alias} if CORE::exists $amaps[$$root]{$alias}; $rv = _getRefById($rv) if defined $rv; } return $rv; } } ########################################################## # Property/Method support ########################################################## { # Property storage my @properties; sub __declProperty { # Purpose: Creates a named property record with associated meta data # Returns: Boolean # Usage: $rv = __declProperty($caller, $obj, $name, $attr); my $caller = CORE::shift; my $obj = CORE::shift; my $name = CORE::shift; my $attr = CORE::shift; # Prepend package scoping in front of private properties $name = "$caller*$name" if $attr & CEH_PRIV; # Apply default attributes $attr |= CEH_SCALAR unless ( $attr ^ CEH_PATTR_TYPE ) > 0; $attr |= CEH_PUB unless ( $attr ^ CEH_PATTR_SCOPE ) > 0; # Save the properties ${ $properties[$$obj] }{$name} = []; ${ $properties[$$obj] }{$name}[CEH_PATTR] = $attr; ${ $properties[$$obj] }{$name}[CEH_PPKG] = $caller; ${ $properties[$$obj] }{$name}[CEH_PVAL] = $attr & CEH_ARRAY ? [] : $attr & CEH_HASH ? {} : undef; return 1; } sub _declProperty { # Purpose: Creates a named property record with associated meta data. # This is the public function available for use by # subclasses # Returns: Boolean # Usage: $rv = _declProperty($obj, $name, $attr); my $obj = CORE::shift; my $name = CORE::shift; my $attr = CORE::shift; my $caller = caller; my $rv = !$obj->isStale; if ($rv) { if ( defined $name and length $name ) { $rv = __declProperty( $caller, $obj, $name, $attr ); } else { $@ = '_declProperty function called with an invalid property'; carp $@; $rv = 0; } } else { $@ = '_declProperty function called with a stale object'; carp $@; } return $rv; } sub _gatekeeper { # Purpose: Checks for a valid property name, and checks ACLs for the # caller # Returns: Property name if allowed, undef otherwise # Usage: $name = $obj->gatekeeper($caller, $name); my $obj = CORE::shift; my $caller = CORE::shift; my $name = CORE::shift; my ( $rv, $class, $cscope, $pscope ); if ( defined $name and length $name ) { # Check scope and adjust for privately scoped properties $name = "$caller*$name" if CORE::exists $properties[$$obj]{"$caller*$name"}; if ( CORE::exists $properties[$$obj]{$name} ) { # Get the property's class $class = $properties[$$obj]{$name}[CEH_PPKG]; # Get the property's scope $pscope = $properties[$$obj]{$name}[CEH_PATTR] & CEH_PATTR_SCOPE; # Get the caller's scope $cscope = $caller eq $class ? CEH_PRIV : "$caller"->isa($class) ? CEH_RESTR : CEH_PUB; # Set the values if allowed if ( $cscope >= $pscope ) { $rv = $name; } else { $@ = 'property access violation'; carp $@; } } else { $@ = 'method called with an nonexistent property'; carp $@; } } else { $@ = 'method called with an invalid property name'; carp $@; } return $rv; } sub _setProperty { # Purpose: Sets the named property to the passed values # Returns: Boolean # Usage: $rv = $obj->_setProperty($name, @values); my $obj = CORE::shift; my $name = CORE::shift; my @val = @_; my ( $rv, $ptype, $pundef, $pref ); # Get some meta data $ptype = ${ $properties[$$obj] }{$name}[CEH_PATTR] & CEH_PATTR_TYPE; $pundef = ${ $properties[$$obj] }{$name}[CEH_PATTR] & CEH_NO_UNDEF; if ( $ptype != CEH_ARRAY and $ptype != CEH_HASH ) { $pref = ref $val[0]; # Check for undef restrictions $rv = 1 if !$pundef or defined $val[0]; if ($rv) { # Check types for correctness $rv = ( !defined $val[0] ) ? 1 : $ptype == CEH_SCALAR ? ( $pref eq '' ) : $ptype == CEH_CODE ? ( $pref eq 'CODE' ) : $ptype == CEH_GLOB ? ( $pref eq 'GLOB' ) : $ptype == CEH_REF ? ( length $pref ) : 0; $@ = "data type mismatch for $name"; carp $@ unless $rv; } } else { # No validation for array/hash types $rv = 1; } # Assign the value(s) if ($rv) { if ( $ptype == CEH_ARRAY ) { ${ $properties[$$obj] }{$name}[CEH_PVAL] = [@val]; } elsif ( $ptype == CEH_HASH ) { ${ $properties[$$obj] }{$name}[CEH_PVAL] = {@val}; } else { ${ $properties[$$obj] }{$name}[CEH_PVAL] = $val[0]; } } return $rv; } sub set { # Purpose: Sets the named properties to the passed value(s) # Returns: Boolean # Usage: $rv = $obj->set($name, @values); my $obj = CORE::shift; my $name = CORE::shift; my @val = @_; my $caller = caller; my $rv = !$obj->isStale; if ($rv) { $name = $obj->_gatekeeper( $caller, $name ); if ( defined $name ) { $rv = $obj->_setProperty( $name, @val ); } else { $rv = 0; } } else { $@ = 'set method called on a stale object'; carp $@; } return $rv; } sub _getProperty { # Purpose: Gets the named property's value(s) # Returns: Scalar, Array, Hash, etc. # Usage: @rv = $obj->getProperty($name); my $obj = CORE::shift; my $name = CORE::shift; my ( @rv, $ptype ); # Get some meta data $ptype = $properties[$$obj]{$name}[CEH_PATTR] & CEH_PATTR_TYPE; # Retrieve the content @rv = $ptype == CEH_HASH ? %{ $properties[$$obj]{$name}[CEH_PVAL] } : $ptype == CEH_ARRAY ? @{ $properties[$$obj]{$name}[CEH_PVAL] } : ( $properties[$$obj]{$name}[CEH_PVAL] ); return $ptype == CEH_HASH ? @rv : $ptype == CEH_ARRAY ? @rv : $rv[0]; } sub get { # Purpose: Gets the named property's value(s) # Returns: Scalar, Array, Hash, etc. # Usage: @rv = $obj->get($name); my $obj = CORE::shift; my $name = CORE::shift; my $caller = caller; my @rv; if ( !$obj->isStale ) { $name = $obj->_gatekeeper( $caller, $name ); if ( defined $name ) { @rv = $obj->_getProperty($name); } } else { $@ = 'set method called on a stale object'; carp $@; } return wantarray ? @rv : $rv[0]; } sub push { # Purpose: Performs a push operation on an array property # Returns: RV of CORE::push or undef # Usage: $rv = $obj->push($name, @values); my $obj = CORE::shift; my $name = CORE::shift; my @val = @_; my $caller = caller; my $rv = !$obj->isStale; if ($rv) { $rv = undef; $name = $obj->_gatekeeper( $caller, $name ); if ( defined $name ) { if ( ref $properties[$$obj]{$name}[CEH_PVAL] eq 'ARRAY' ) { $rv = CORE::push @{ $properties[$$obj]{$name}[CEH_PVAL] }, @val; } else { $@ = 'push attempted on a non-array property'; carp $@; } } } else { $@ = 'push method called on a stale object'; carp $@; } return $rv; } sub pop { # Purpose: Performs a pop operation on an array property # Returns: RV of CORE::pop or undef # Usage: $rv = $obj->pop($name); my $obj = CORE::shift; my $name = CORE::shift; my $caller = caller; my $rv = !$obj->isStale; if ($rv) { $rv = undef; $name = $obj->_gatekeeper( $caller, $name ); if ( defined $name ) { if ( ref $properties[$$obj]{$name}[CEH_PVAL] eq 'ARRAY' ) { $rv = CORE::pop @{ $properties[$$obj]{$name}[CEH_PVAL] }; } else { $@ = 'pop attempted on a non-array property'; carp $@; } } } else { $@ = 'pop method called on a stale object'; carp $@; } return $rv; } sub unshift { # Purpose: Performs an unshift operation on an array property # Returns: RV of CORE::unshift or undef # Usage: $rv = $obj->unshift($name, @values); my $obj = CORE::shift; my $name = CORE::shift; my @val = @_; my $caller = caller; my $rv = !$obj->isStale; if ($rv) { $rv = undef; $name = $obj->_gatekeeper( $caller, $name ); if ( defined $name ) { if ( ref $properties[$$obj]{$name}[CEH_PVAL] eq 'ARRAY' ) { $rv = CORE::unshift @{ $properties[$$obj]{$name}[CEH_PVAL] }, @val; } else { $@ = 'unshift attempted on a non-array property'; carp $@; } } } else { $@ = 'unshift method called on a stale object'; carp $@; } return $rv; } sub shift { # Purpose: Performs a shift operation on an array property # Returns: RV of CORE::shift or undef # Usage: $rv = $obj->shift($name); my $obj = CORE::shift; my $name = CORE::shift; my $caller = caller; my $rv = !$obj->isStale; if ($rv) { $rv = undef; $name = $obj->_gatekeeper( $caller, $name ); if ( defined $name ) { if ( ref $properties[$$obj]{$name}[CEH_PVAL] eq 'ARRAY' ) { $rv = CORE::shift @{ $properties[$$obj]{$name}[CEH_PVAL] }; } else { $@ = 'shift attempted on a non-array property'; carp $@; } } } else { $@ = 'shift method called on a stale object'; carp $@; } return $rv; } sub exists { # Purpose: Performs an exists operation on a hash property # Returns: RV of CORE::exists or undef # Usage: $rv = $obj->exists($name, $key); my $obj = CORE::shift; my $name = CORE::shift; my $key = CORE::shift; my $caller = caller; my $rv = !$obj->isStale; if ($rv) { $rv = undef; $name = $obj->_gatekeeper( $caller, $name ); if ( defined $name ) { if ( ref $properties[$$obj]{$name}[CEH_PVAL] eq 'HASH' ) { $rv = CORE::exists $properties[$$obj]{$name}[CEH_PVAL] {$key}; } else { $@ = 'exists attempted on a non-hash property'; carp $@; } } } else { $@ = 'exists method called on a stale object'; carp $@; } return $rv; } sub keys { # Purpose: Performs a keys operation on a hash property # Returns: RV of CORE::keys or empty array # Usage: $rv = $obj->keys($name); my $obj = CORE::shift; my $name = CORE::shift; my $caller = caller; my @rv; if ( !$obj->isStale ) { $name = $obj->_gatekeeper( $caller, $name ); if ( defined $name ) { if ( ref $properties[$$obj]{$name}[CEH_PVAL] eq 'HASH' ) { @rv = CORE::keys %{ $properties[$$obj]{$name}[CEH_PVAL] }; } else { $@ = 'keys attempted on a non-hash property'; carp $@; } } } else { $@ = 'keys method called on a stale object'; carp $@; } return @rv; } sub merge { # Purpose: Merges the specified ordinal or associated records into # the named property # Returns: Boolean # Usage: $rv = $obj->merge($name, 'foo' => 'bar'); # Usage: $rv = $obj->merge($name, 1 => 'bar'); my $obj = CORE::shift; my $name = CORE::shift; my %updates = @_; my $rv = !$obj->isStale; my $caller = caller; my ( $k, $v ); if ($rv) { $name = $obj->_gatekeeper( $caller, $name ); if ( defined $name ) { if ( ref $properties[$$obj]{$name}[CEH_PVAL] eq 'ARRAY' ) { while ( ( $k, $v ) = each %updates ) { $properties[$$obj]{$name}[CEH_PVAL][$k] = $v; } } elsif ( ref $properties[$$obj]{$name}[CEH_PVAL] eq 'HASH' ) { while ( ( $k, $v ) = each %updates ) { $properties[$$obj]{$name}[CEH_PVAL]{$k} = $v; } } else { $@ = 'merge attempted on a non-hash/array property'; carp $@; } } } else { $@ = 'merge method called on a stale object'; carp $@; } return $rv; } sub subset { # Purpose: Returns the associated or ordinal values from the named # property # Returns: Array of values # Usage: @values = $obj->subset($name, qw(foo bar)); # Usage: @values = $obj->subset($name, 1, 7); my $obj = CORE::shift; my $name = CORE::shift; my @keys = @_; my $caller = caller; my ( @rv, $k, $l ); if ( !$obj->isStale ) { $name = $obj->_gatekeeper( $caller, $name ); if ( defined $name ) { if ( ref $properties[$$obj]{$name}[CEH_PVAL] eq 'ARRAY' ) { $l = $#{ $properties[$$obj]{$name}[CEH_PVAL] }; foreach $k (@keys) { CORE::push @rv, ( $k <= $l ? $properties[$$obj]{$name}[CEH_PVAL][$k] : undef ); } } elsif ( ref $properties[$$obj]{$name}[CEH_PVAL] eq 'HASH' ) { foreach $k (@keys) { CORE::push @rv, ( CORE::exists $properties[$$obj]{$name}[CEH_PVAL] {$k} ? $properties[$$obj]{$name}[CEH_PVAL]{$k} : undef ); } } else { $@ = 'subset attempted on a non-hash/array property'; carp $@; } } } else { $@ = 'subset method called on a stale object'; carp $@; } return @rv; } sub remove { # Purpose: Removes the ordinal or associated values from the named # property # Returns: Boolean # Usage: $rv = $obj->remove($name, qw(foo bar)); # Usage: $rv = $obj->remove($name, 5, 8); my $obj = CORE::shift; my $name = CORE::shift; my @keys = @_; my $caller = caller; my $rv = !$obj->isStale; my ( $k, $l ); if ($rv) { $name = $obj->_gatekeeper( $caller, $name ); if ( defined $name ) { if ( ref $properties[$$obj]{$name}[CEH_PVAL] eq 'ARRAY' ) { $l = $#{ $properties[$$obj]{$name}[CEH_PVAL] }; foreach $k ( sort { $b <=> $a } @keys ) { splice @{ $properties[$$obj]{$name}[CEH_PVAL] }, $k, 1 unless $k > $l; } } elsif ( ref $properties[$$obj]{$name}[CEH_PVAL] eq 'HASH' ) { foreach $k (@keys) { delete $properties[$$obj]{$name}[CEH_PVAL]{$k}; } } else { $@ = 'remove attempted on a non-hash/array property'; carp $@; } } } else { $@ = 'remove method called on a stale object'; carp $@; } return $rv; } sub empty { # Purpose: Empties the named array or hash property # Returns: Boolean # Usage: $rv = $obj->empty($name); my $obj = CORE::shift; my $name = CORE::shift; my $caller = caller; my $rv = !$obj->isStale; if ($rv) { $name = $obj->_gatekeeper( $caller, $name ); if ( defined $name ) { if ( ref $properties[$$obj]{$name}[CEH_PVAL] eq 'ARRAY' ) { @{ $properties[$$obj]{$name}[CEH_PVAL] } = (); } elsif ( ref $properties[$$obj]{$name}[CEH_PVAL] eq 'HASH' ) { %{ $properties[$$obj]{$name}[CEH_PVAL] } = (); } else { $@ = 'empty attempted on a non-hash/array property'; carp $@; } } } else { $@ = 'empty method called on a stale object'; carp $@; } return $rv; } sub properties { # Purpose: Returns a list of property names visible to the caller # Returns: Array of scalars # Usage: @names = $obj->properties; my $obj = CORE::shift; my $caller = caller; my @pnames = CORE::keys %{ $properties[$$obj] }; my @rv; # Populate with all the public properties @rv = grep { $properties[$$obj]{$_}[CEH_PATTR] & CEH_PUB } @pnames; # Add restricted properties if the caller is a subclass if ( $caller eq ref $obj or "$caller"->isa($obj) ) { CORE::push @rv, grep { $properties[$$obj]{$_}[CEH_PATTR] & CEH_RESTR } @pnames; } # Add private properties if the caller is the same class if ( $caller eq ref $obj ) { foreach ( grep /^\Q$caller*\E/s, @pnames ) { CORE::push @rv, $_; $rv[$#rv] =~ s/^\Q$caller*\E//s; } } return @rv; } sub _initProperties { # Purpose: Initializes the property data for the object # Returns: Boolean # Usage: $rv = _initProperties($obj); my $obj = CORE::shift; my @classes = _getClasses($obj); my $rv = 1; my ( $class, @_properties, $prop, $pattr, $pscope, $pname ); # Initialize storage $properties[$$obj] = {}; # Load properties from top of class hierarchy down foreach $class (@classes) { # Get the contents of the class array { no strict 'refs'; @_properties = defined *{"${class}::_properties"} ? @{ *{"${class}::_properties"}{ARRAY} } : (); } # Process the list foreach $prop (@_properties) { next unless defined $prop; unless ( __declProperty( $class, $obj, @$prop[ CEH_PNAME, CEH_PATTR ] ) ) { $rv = 0; last; } # Set the default values if ( $rv and defined $$prop[CEH_PVAL] ) { # Get the attribute type, scope, and internal prop name $pattr = $$prop[CEH_PATTR] & CEH_PATTR_TYPE; $pscope = $$prop[CEH_PATTR] & CEH_PATTR_SCOPE; $pname = $pscope == CEH_PRIV ? "${class}::$$prop[CEH_PNAME]" : $$prop[CEH_PNAME]; # Store the default values $rv = $obj->_setProperty( $pname, $pattr == CEH_ARRAY ? @{ $$prop[CEH_PVAL] } : $pattr == CEH_HASH ? %{ $$prop[CEH_PVAL] } : $$prop[CEH_PVAL] ); } last unless $rv; } } return $rv; } sub _destrProperties { # Purpose: Destroys the object's property data # Returns: Boolean # Usage: $rv = _destrProperties($obj); my $obj = CORE::shift; $properties[$$obj] = undef; return 1; } } { my %classes; # Class => 1 my %methods; # Class::Method => 1 sub __declMethod { # Purpose: Registers a list of methods as scoped # Returns: Boolean # Usage: $rv = __declMethod($class, $attr, $methods); my $pkg = CORE::shift; my $attr = CORE::shift; my $method = CORE::shift; my $rv = 1; my ( $code, $mfqn ); if ( defined $attr and defined $method and length $method ) { # Quiet some warnings no warnings qw(redefine prototype); no strict 'refs'; # Get the fully qualified method name and associated code # block $mfqn = "${pkg}::${method}"; $code = *{$mfqn}{CODE}; # Quick check to see if we've done this already -- if so # we skip to the next return 1 if CORE::exists $methods{$mfqn}; if ( defined $code ) { # Repackage if ( $attr == CEH_PRIV ) { # Private methods *{$mfqn} = sub { my $caller = caller; goto &{$code} if $caller eq $pkg; $@ = 'Attempted to call private method ' . "$method from $caller"; carp $@; return 0; }; } elsif ( $attr == CEH_RESTR ) { # Restricted methods *{$mfqn} = sub { my $caller = caller; goto &{$code} if "$caller"->isa($pkg); $@ = 'Attempted to call restricted method ' . "$method from $caller"; carp $@; return 0; }; } elsif ( $attr == CEH_PUB ) { # Do nothing } else { $@ = 'invalid method declaration'; carp $@; $rv = 0; } # Record our handling of this method $methods{$mfqn} = 1 if $rv; } } else { $@ = 'invalid method declaration'; carp $@; $rv = 0; } return $rv; } sub _declMethod { # Purpose: Wrapper for __declMethod, this is the public interface # Returns: RV of __declMethod # Usage: $rv = _declMethod($attr, @propNames); my $attr = CORE::shift; my $method = CORE::shift; my $caller = caller; my $rv = 1; if ( defined $method and length $method ) { $rv = __declMethod( $caller, $attr, $method ); } else { $@ = '_declMethod function called with an invalid method'; carp $@; $rv = 0; } return $rv; } sub _initMethods { # Purpose: Loads methods from @_methods # Returns: Boolean # Usage: $rv = _loadMethods(); my $obj = CORE::shift; my @classes = _getClasses($obj); my $rv = 1; my ( $class, @_methods, $method ); # Load methods from the top of the class hierarchy down foreach $class (@classes) { # Skip if the class has already been processed next if CORE::exists $classes{$class}; # Get the contents of the class array { no strict 'refs'; @_methods = @{ *{"${class}::_methods"}{ARRAY} } if defined *{"${class}::_methods"}; } # Process the list foreach $method (@_methods) { next unless defined $method; unless ( __declMethod( $class, @$method[ CEH_PATTR, CEH_PPKG ] ) ) { $rv = 0; last; } } # Mark the class as processed $classes{$class} = 1; } return $rv; } } ########################################################## # Class Constructors/Destructors ########################################################## sub new { # Purpose: Class constructor for all (sub)classes # Returns: Reference # Usage: $obj = new SUBCLASS; my $class = CORE::shift; my @args = @_; my $obj = bless \do { my $anon_scalar }, $class; my $rv; # Get the next available ID $rv = _getID($obj); # Initialize alias support $rv = _initAlias($obj) if $rv; # Initialize property scope support $rv = _initProperties($obj) if $rv; # Initialize method scope support $rv = _initMethods($obj) if $rv; # Initialize the hierarchal code support $rv = _initHierarchy( $obj, $class, @args ) if $rv; return $rv ? $obj : undef; } sub conceive { # Purpose: Same as new() but with hierarchal relationships pre-installed # Returns: Reference # Usage: SubClass->conceive($parent, @args); my $class = CORE::shift; my $pobj = CORE::shift; my @args = @_; my $obj = bless \do { my $anon_scalar }, $class; my $rv = 1; # Get the next available ID $rv = _getID($obj) if $rv; # Adopt the object before we do anything else $rv = $pobj->_adopt($obj) if $rv; # Initialize property scope support $rv = _initProperties($obj) if $rv; # Initialize method scope support $rv = _initMethods($obj) if $rv; # Initialize the hierarchal code support $rv = _initHierarchy( $obj, $class, @args ) if $rv; return $rv ? $obj : undef; } sub DESTROY { # Purpose: Garbage collection # Returns: Boolean # Usage: $obj->DESTROY(); my $obj = CORE::shift; my ( $class, @classes ); # Test to see if this is a stale reference unless ( !defined $$obj or $obj->isStale ) { # Destroy from the top of the tree down foreach ( $obj->children ) { $_->DESTROY if defined } # Execute hierarchal destructors _destrHierarchy($obj); # Destroy aliases _destrAlias($obj); # Destroy properties _destrProperties($obj); # Recover the ID _delID($obj); } return 1; } END { foreach ( _dumpObjects() ) { $_->DESTROY if defined } } 1; __END__ =head1 NAME Class::EHierarchy - Base class for hierarchally ordered objects =head1 VERSION $Id: lib/Class/EHierarchy.pm, 2.00 2017/01/09 08:47:12 acorliss Exp $ =head1 SYNOPSIS package TelDirectory; use Class::EHierarchy qw(:all); use vars qw(@ISA @_properties @_methods); @ISA = qw(Class::EHierarchy); @_properties = ( [ CEH_PRIV | CEH_SCALAR, 'counter', 0 ], [ CEH_PUB | CEH_SCALAR, 'first', '' ], [ CEH_PUB | CEH_SCALAR, 'last', '' ], [ CEH_PUB | CEH_ARRAY, 'telephone' ] ); @_methods = ( [ CEH_PRIV, '_incrCounter' ], [ CEH_PUB, 'addTel' ] ); sub _initalize { my $obj = CORE::shift; my %args = @_; my $rv = 1; # Statically defined properties and methods are # defined above. Dynamically generated # properties and methods can be done here. return $rv; } ... package main; use TelDirectory; my $entry = new TelDirectory; $entry->set('first', 'John'); $entry->set('last', 'Doe'); $entry->push('telephone', '555-111-2222', '555-555'5555'); =head1 DESCRIPTION B is intended for use as a base class for objects that need support for class or object hierarchies. Additional features are also provided which can be useful for general property implementation and manipulation. =head2 OBJECT HIERARCHIES Object relationships are often implemented in application code, as well as the necessary reference storage to keep dependent objects in scope. This class attempts to relive the programmer of that necessity. To that end, the concept of an object hierarchy is implemented in this class. An OOP concept for RDBMS data, for instance, could be modeled as a collection of objects in the paradigm of a family tree. The root object could be your DBI connection handle, while all of the internal data structures as child objects: DBH connection +-> views | +-> view1 +-> tables +-> table1 +-> rows | +-> row1 +-> columns Each type of object in the RDBMS is necessarily defined in context of the parent object. This class simplifies the formalization of these relationships, which can have a couple of benefits. Consider a row object that was retrieved, for example. If each of the columns was implmented as a property in the object one could allow in-memory modification of data with a delayed commit. When the connection goes out of scope you could code your application to flush those in-memory modifications back to the database prior to garbage collection. This is because garbage collection of an object causes a top-down destruction of the object tree (or, in the depiction above, bottom-up), with the farthest removed children reaped first. Another benefit of defined object hierarchies is that you are no longer required to keep track of and maintain references to every object in the tree. Only the root reference needs to be tracked since the root can also act as an object container. All children references can be retrieved at any time via method calls. An alias system is also implemented to make children retrieval even more convenient. Each table, for instance, could be aliased by their table name. That allows you to retrieve a table object by name, then, instead of iterating over the collection of tables until you find one with the attributes you're seeking. =head2 CLASS HIERARCHIES Class hierarchies are another concept meant to allieviate some of the tedium of coding subclasses. Traditionally, if you subclassed a class that required any significant initialization, particularly if it relied on internal data structures, you would be reduced to executing superclass constructors, then possibly executing code paths again to account for a few changed properties. This class explicitly separates assignment of properties from initialization, allowing you to execute those code paths only once. OOP implemenations of mathematical constructs, for instance, could significantly alter the values derived from objects simply by subclassing and overriding some property values. The original class' initializer will be run once, but using the new property values. In addition to that this class provides both property and method compartmentalization so that the original class author can limit the invasiveness of subclasses. Both methods and properties can be scoped to restrict access to both. You can restrict access to use by only the implementation class, to subclasses, or keep everything publically available. =head2 ADDITIONAL FEATURES The class hierarchal features necessarily make objects derived from this class opaque objects. Objects aren't blessed hashes, they are scalar references with all properties stored in class data structures. The property implementation was made to be flexible to accomodate most needs. A property can be a scalar value, but it also can be an array, hash, or a number of specific types of references. To make non-scalar properties almost as convenient as the raw data structures many core functions have been implemented as methods. This is not just a semantic convenience, it also has the benefit of working directly on the raw data stored in the class storage. Data structures aren't copied, altered, and stored, they are altered in place for performance. =head1 CONSTANTS Functions and constants are provided strictly for use by derived classes within their defined methods. To avoid any confusion all of our exportable symbols are *not* exported by default. You have to specifically import the B tag set. Because these functions should not be used outside of the subclass they are all preceded by an underscore, like any other private function. The following constants are provided for use in defining your properties and methods. Scope --------------------------------------------------------- CEH_PRIV private scope CEH_RESTR restricted scope CEH_PUB public scope Type --------------------------------------------------------- CEH_SCALAR scalar value or reference CEH_ARRAY array CEH_HASH hash CEH_CODE code reference CEH_GLOB glob reference CEH_REF object reference Flag --------------------------------------------------------- CEH_NO_UNDEF No undef values are allowed to be assigned to the property You'll note that both I<@_properties> and I<@_methods> are arrays of arrays, which each subarray containing the elements for each property or method. The first element is always the attributes and the second the name of the property or method. In the case of the former a third argument is also allowed: a default value for the property: @_properties = ( [ CEH_PUB | CEH_SCALAR, 'first', 'John' ], [ CEH_PUB | CEH_SCALAR, 'last', 'Doe' ], [ CEH_PUB | CEH_ARRAY, 'telephone', [ qw(555-555-1212 555-555-5555) ] ], ); Properties lacking a data type attribute default to B. Likewise, scope defaults to B. Public methods can be omitted from I<@_methods> since they will be assumed to be public. Methods only support scoping for attributes. Data types and flags are not applicable to them. =head1 SUBROUTINES/METHODS =head2 new $obj = new MyClass; All of the hierarchal features require bootstrapping in order to work. For that reason a constructor is provided which performs that work. If you wish to provide additional initialization you can place a B<_initialize> method in your class which will be called after the core bootstrapping is complete. =head2 _initialize $rv = $obj->_initialize(@args); The use of this method is optional, but if present it will be called during the execution of the constructor. The boolean return value will determine if the constructor is successful or not. All superclasses with such a method will be called prior to the final subclass' method, allowing you to layer multiple levels of initialization. Initialization is performed I the assignment of default values to properties. If your code is dependent on those values this allows you the opportunity to override certain defaults -- assuming they are visible to the subclass -- simply by setting those new defaults in the subclass. As shown, this method is called with all of the arguments passed to the constructor, and it expects a boolean return value. =head2 conceive $child = MyClass->conceive($parent, @args); B is an alternate constructor that's intended for those subclasses with are dependent on relationships to parent objects during initialization. =head2 DESTROY $obj->DESTROY; Object hierarchal features require orderly destruction of children. For that purpose a B method is provided which performs those tasks. If you have specific tasks you need performed prior to the final destruction of an object you can place a B<_deconstruct> method in your subclass. =head2 _deconstruct $rv = $obj->_desconstruct; B<_deconstruct> is an optional method which, if present, will be called during the object's B phase. It will be called I all children have completed thier B phase. In keeping with the class hierarchal features all superclasses will have their B<_deconstruct> methods called after your subclass' method is called, but prior to finishing the B phase. =head2 isStale $rv = $obj->isStale; It is possible that you might have stored a reference to a child object in a tree. If you were to kick off destruction of the tne entire object tree by letting the root object's reference go out of scope the entire tree will be effectively destroyed. Your stored child reference will not prevent that from happening. At that point you effectively have a stale reference to a non-functioning object. This method allows you to detect that scenario. The primary use for this method is as part of your safety checks in your methods: sub my_method { my $obj = shift; my @args = @_; my $rv = !$obj->isStale; if ($rv) { # Do method work here, update $rv, etc. } else { carp "called my_method on a stale object!"; } return $rv; } It is important to note that this method is used in every public method provided by this base class. All method calls will therefore safely fail if called on a stale object. =head2 _declProp $rv = _declProp($obj, CEH_PUB | CEH_SCALAR | CEH_NO_UNDEF, @propNames); This function is used to dynamically create named properties while declaring their access scope and type. Constants describing property attributes are OR'ed together, and only one scope and one type from each list should be used at a time. Using multiple types or scopes to describe any particular property will make it essentially inaccessible. B I only applies to psuedo-scalar types like proper scalars, references, etc. This has no effect on array members or hash values. =head2 _declMethod $rv = _declMethod(CEH_RESTR, @methods); This function is is used to create wrappers for those functions whose access you want to restrict. It works along the same lines as properties and uses the same scoping constants for the attribute. Only methods defined within the subclass can have scoping declared. You cannot call this method for inherited methods. B Since scoping is applied to the class symbol table (B on a per object basis) any given method can only be scoped once. That means you can't do crazy things like make public methods private, or vice-versa. =head2 adopt $rv = $obj->adopt($cobj1, $cobj2); This method attempts to adopt the passed objects as children. It returns a boolean value which is true only if all objects were successfully adopted. Only subclasses for L can be adopted. Any object that isn't based on this class will cause this method to return a false value. =head2 disown $rv = $obj->disown($cobj1, $cobj2); This method attempts to disown all the passed objects as children. It returns a boolean value based on its success in doing so. Asking it to disown an object it had never adopted in the first place will be silently ignored and still return true. Disowning objects is a prerequisite for Perl's garbage collection to work and release those objects completely from memory. The B method provided by this class automatically does this for parent objects going out of scope. You may still need to do this explicitly if your parent object manages objects which may need to be released well prior to any garbage collection on the parent. =head2 parent $parent = $obj->parent; This method returns a reference to this object's parent object, or undef if it has no parent. =head2 children @crefs = $obj->children; This method returns an array of object references to every object that was adopted by the current object. =head2 descendents @descendents = $obj->descendents; This method returns an array of object references to every object descended from the current object. =head2 siblings @crefs = $obj->siblings; This method returns an array of object references to every object that shares the same parent as the current object. =head2 root $root = $obj->root; This method returns a reference to the root object in this object's ancestral tree. In other words, the senior most parent in the current hierarchy. =head2 alias $rv = $obj->alias($new_alias); This method sets the alias for the object, returning a boolean value. This can be false if the proposed alias is already in use by another object in its hierarchy. =head2 getByAlias $ref = $obj->getByAlias($name); This method returns an object reference from within the object's current object hierarchy by name. It will return undef if the alias is not in use. =head2 set $rv = $obj->set('FooScalar', 'random text or reference'); $rv = $obj->set('FooArray', @foo); $rv = $obj->set('FooHash', %foo); This method provides a generic property write accessor that abides by the scoping attributes given by B<_declProp> or B<@_properties>. This means that basic reference types are checked for during assignment, as well as flags like B. =head2 get $val = $obj->get('FooScalar'); @val = $obj->get('FooArray'); %val = $obj->get('FooHash'); This method provides a generic property read accessor. This will return an undef for nonexistent properties. =head2 properties @properties = $obj->properties; This method returns a list of all registered properties for the current object. Property names will be filtered appropriately by the caller's context. =head3 push $rv = $obj->push($prop, @values); This method pushes additional elements onto the specified array property. It returns the return value from the B function, or undef on non-existent properties or invalid types. =head3 pop $rv = $obj->pop($prop); This method pops an element off of the specified array property. It returns the return value from the B function, or undef on non-existent properties or invalid types. =head3 unshift $rv = $obj->unshift($prop, @values); This method unshifts additional elements onto the specified array property. It returns the return value from the B function, or undef on non-existent properties or invalid types. =head3 shift $rv = $obj->shift($prop); This method shifts an element off of the specified array property. It returns the return value from the B function, or undef on non-existent properties or invalid types. =head3 exists $rv = $obj->exists($prop, $key); This method checks for the existance of the specified key in the hash property. It returns the return value from the B function, or undef on non-existent properties or invalid types. =head3 keys @keys = $obj->keys($prop); This method returns a list of keys from the specified hash property. It returns the return value from the B function, or undef on non-existent properties or invalid types. =head3 merge $obj->merge($prop, foo => bar); $obj->merge($prop, 4 => foo, 5 => bar); This method is a unified method for storing elements in both hashes and arrays. Hashes elements are simply key/value pairs, while array elements are provided as ordinal index/value pairs. It returns a boolean value. =head3 subset @values = $obj->subset($hash, qw(foo bar) ); @values = $obj->subset($array, 3 .. 5 ); This method is a unified method for retrieving specific element(s) from both hashes and arrays. Hash values are retrieved in the order of the specified keys, while array elements are retrieved in the order of the specified ordinal indexes. =head3 remove $obj->remove($prop, @keys); $obj->remove($prop, 5, 8 .. 10); This method is a unified method for removing specific elements from both hashes and arrays. A list of keys is needed for hash elements, a list of ordinal indexes is needed for arrays. B In the case of arrays please note that an element removed in the middle of an array does cause the following elements to be shifted accordingly. This method is really only useful for removing a few elements at a time from an array. Using it for large swaths of elements will likely prove it to be poorly performing. You're better of retrieving the entire array yourself via the B method, splicing what you need, and calling B again to set the new array contents. =head3 empty $rv = $obj->empty($name); This is a unified method for emptying both array and hash properties. This returns a boolean value. =head1 DEPENDENCIES None. =head1 BUGS AND LIMITATIONS =head1 CREDIT The notion and portions of the implementation of opaque objects were lifted from Damian Conway's L module. Conway has a multitude of great ideas, and I'm grateful that he shares so much with the community. =head1 AUTHOR Arthur Corliss (corliss@digitalmages.com) =head1 LICENSE AND COPYRIGHT This software is licensed under the same terms as Perl, itself. Please see http://dev.perl.org/licenses/ for more information. (c) 2017, Arthur Corliss (corliss@digitalmages.com) Class-EHierarchy-2.00/INSTALL0000644000175000001440000000042712741127430015314 0ustar acorlissusersInstallation Instructions: ========================== If you have root access you can simply execute: perl Makefile.PL && make && make install If you're installing this in your own personal space use something akin to: perl Makefile.PL LIB=~/lib && make && make install Class-EHierarchy-2.00/Makefile.PL0000644000175000001440000000131312741127430016230 0ustar acorlissusersuse ExtUtils::MakeMaker; use 5.008003; WriteMakefile( NAME => 'Class::EHierarchy', AUTHOR => 'Arthur Corliss ', ABSTRACT => 'Base class for hierarchally ordered objects', VERSION_FROM => 'lib/Class/EHierarchy.pm', PREREQ_PM => {}, ( $ExtUtils::MakeMaker::VERSION ge '6.30_00' ? ( LICENSE => 'perl' ) : () ), ( $ExtUtils::MakeMaker::VERSION ge '6.48' ? ( MIN_PERL_VERSION => 5.008003 ) : () ), dist => { COMPRESS => 'gzip', SUFFIX => '.gz', CI => 'cvs ci', RCS_LABEL => 'cvs tag -c -F $(NAME_SYM)-$(VERSION_SYM)', }, ); exit 0; Class-EHierarchy-2.00/README0000644000175000001440000000113613034745425015147 0ustar acorlissusersClass::EHierarchy ================= NOTICE: One might wonder how we've jumped from 0.x releases to 2.x. I've decided to skip the 1.x releases to make it (hopefully) more obvious that the API has changed and is not compatible with the 0.x series. My fear was that the average devmight see 0.x to 1.x and assume that I was simply declaring the API stable. While 2.x is a stable API it is not compatible with 0.x. -- Class::EHierarchy is intended to provide a base class for objects that require both object and class hierarchal features. For more information on what that means, please see the POD. Class-EHierarchy-2.00/LICENSE0000644000175000001440000004737112741127430015301 0ustar acorlissusers Terms of Perl itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" ---------------------------------------------------------------------------- The General Public License (GPL) Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 675 Mass Ave, Cambridge, MA 02139, USA. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS ---------------------------------------------------------------------------- The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Class-EHierarchy-2.00/META.yml0000644000175000001440000000100513035727240015527 0ustar acorlissusers--- abstract: 'Base class for hierarchally ordered objects' author: - 'Arthur Corliss ' build_requires: ExtUtils::MakeMaker: 0 configure_requires: ExtUtils::MakeMaker: 0 dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 6.86, CPAN::Meta::Converter version 2.120351' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Class-EHierarchy no_index: directory: - t - inc requires: perl: 5.008003 version: 2.00 Class-EHierarchy-2.00/META.json0000644000175000001440000000161113035727240015702 0ustar acorlissusers{ "abstract" : "Base class for hierarchally ordered objects", "author" : [ "Arthur Corliss " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 6.86, CPAN::Meta::Converter version 2.120351", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Class-EHierarchy", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "perl" : "5.008003" } } }, "release_status" : "stable", "version" : "2.00" }