# ---8<--- ProcessData.pm ---8<--- package ProcessData; use warnings; use strict; use Carp; sub new { my ($class) = @_; my $self = { _dispatch_table => {} }; return bless $self, $class; } sub add_subroutine { @_==3 or croak "Insufficient arguments passed to add_subroutine()."; my ($self,$name,$code) = @_; croak "The reference passed to add_subroutine() is not a CODE reference." unless ref $code eq "CODE"; $self->{_dispatch_table}{$name} = $code; } sub process_data { @_==2 or croak "Insufficient arguments passed to process_data()."; my ($self,$name) = @_; $self->{_dispatch_table}{$name}->(); } sub process_locations { my ($self) = @_; $self->process_data("locations"); } 1; # ---8<--- test.pl ---8<--- use warnings; use strict; use ProcessData; my $pd = ProcessData->new(); $pd->add_subroutine("locations", \&get_locations); $pd->process_locations; sub get_locations { print "get_locations...\n" }