package DynamicObject;
sub new {
my ( $pkg, %args ) = @_;
my $self = {
"method" => \&correlation,
%args
};
return bless $self, $pkg;
}
sub correlation {
print "This is the correlation function!\n";
}
sub setMethod {
my ( $self, $key, $function ) = @_;
$self -> { $key } = $function;
}
sub callMethod {
my ( $self, $function, @arguments ) = @_;
if( exists $self -> { $function } ) {
&{ $self -> { $function } }( @arguments );
}
else {
print "Unknown function '$function' called.\n";
}
}
1;
####
use DynamicObject;
my $object = DynamicObject -> new ();
$object -> callMethod( "method" );
$object -> setMethod( "method", \&another );
$object -> callMethod( "method" );
$object -> callMethod( "fnap" );
$object -> setMethod( "fnap", \&ya );
$object -> callMethod( "fnap", qw{ Hey ho! We wont go!} );
sub another {
print "This is another function!\n";
}
sub ya {
print "This is yet another function called with ".
"arguments '", join( " ", @_ ), "'!\n";
}
####
This is the correlation function!
This is another function!
Unknown function 'fnap' called.
This is yet another function called with arguments 'Hey ho! We wont go!'!