use MooseX::Declare; class Utils::File::Section { use Utils::TypeConstraints; has 'id' => ( is => 'ro', isa => 'Str', required => 1, ); has 'entities' => ( is => 'rw', isa => 'HashRef[Entity]', default => sub { {} } ); # "Entity" is a Type Constraint defined in the Utils::TypeConstraints method add(Entity $entity) { # To access the section from the entity $entity->section( $self ); $self->{'entities'}->{$entity->id} = $entity; } # This returns the entity object corresponding to $id method entity(Str $id) { return $self->{'entities'}->{$id}; } } #### use MooseX::Declare; class Utils::File::Macro extends Utils::File::Section { use Utils::TypeConstraints; has '+id' => ( default => 'macro' ); # Just adding an extra layer of validation method add(DefaultEntity|MacrosEntity $entity) { $self->SUPER::add($entity); } } #### method instantiate(Str $type) { # _getclass will return something like 'Utils::File::Something' my $class = $self->_getclass($type); eval("use $class"); my $obj = $class->new( {'id' => $type} ); if ( $obj->isa('Utils::File::Section') ) { return $obj; } else { die("The $class class is not a type of Section!"); } } #### not ok 19 - $obj isa Utils::File::Section # Failed test '$obj isa Utils::File::Section' # in /.../Utils/File/SectionFactory.pm at line 29. # $obj isn't a 'Utils::File::Section' it's a 'Utils::File::Macro' #### use strict; use Test::More qw( no_plan ); use_ok( 'Utils::File::Macro' ); my $macro = Utils::File::Macro->new( {'id' => 'test'} ); isa_ok( $macro, 'Utils::File::Macro', '$macro' ); isa_ok( $macro, 'Utils::File::Section', '$macro' ); #### not ok 1 - use Utils::File::Macro; # Failed test 'use Utils::File::Macro;' # in Macro.t at line 12. # Tried to use 'Utils::File::Macro'. # Error: Can't locate object method "name" via package "MacrosEntity" (perhaps you forgot to load "MacrosEntity"?) at /.../lib/perl5/Moose/Meta/TypeConstraint/Union.pm line 28. # Compilation failed in require at (eval 46) line 2.