package Guff; use Carp; use strict; use Guff::Child1; use Data::Dumper; our $child = 'DESKTOP'; our %dispatch = ( first=>'Guff::Child1', ); sub new { my $class = shift; my $in = shift; #my %opt = %{$_[0]} ; my %opt = %{$in}; my $kid = $class . "::child"; no strict 'refs'; do { confess "Missing child attribute $kid ", $$kid unless exists $opt{$$kid}; } if defined ( $opt{$$kid} ); use strict 'refs'; my $self = bless \%opt, $class; $self->init; return $self; } sub init { my $self = shift; my $child = $self->childkey; return unless defined $self->{type}; confess "Invalid child $child -" , Dumper $self unless exists $self->{ $child }; if ( $self->dispatchto ) { $self->{ $child } = $self->dispatchto->new ( $self->{ $child } ) or confess "Failed to dispatch " , Dumper $self; } } sub childkey { my $self = shift; my $kid = ref($self) . "::child"; no strict 'refs'; my $child = $$kid; use strict 'refs'; return $child; } sub dispatchto { my $self = shift; my $key = shift; my $dsp = ref($self) . "::dispatch"; no strict 'refs'; my $dispatch = $$dsp{$self->{type}}; use strict 'refs'; return $dispatch; } 1;