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}; } }