use strict; use warnings; package RoboPart; sub new { my ($class, %options) = @_; return bless \%options, $class; } sub dispatch { my ($self, $verb, @params) = @_; my $handler = $self->can($verb); die ref $self . " can't $verb\n" if !defined $handler; return $handler->($self, @params); } sub does { my ($self, $verb) = @_; return $self->can($verb); } package RoboArm; use parent -norequire, 'RoboPart'; sub pick { my ($self, $target) = @_; print "I am grabbing $target\n"; } package RoboFeet; use parent -norequire, 'RoboPart'; sub walk { my ($self) = shift; my ($x, $y) = @{$self->{Position}} ; if ( $x > 10 || $y > 10 ) { print "Out of boundary. I stop walk\n"; } else { print "I start walk from pos $x $y\n"; } } package Robo; sub new { my ($class) = @_; return bless { Arm => RoboArm->new(), Feet => RoboFeet->new(Position => [11, 0]), }, $class; } sub send { my ($self, $verb, @params) = @_; for my $part (values %$self) { $part->dispatch($verb, @params) if $part->does($verb); } } package main; my $robo = Robo->new(); $robo->send("pick", "Apple"); $robo->send("walk");