in reply to replace object methods at runtime
In place of
$self->{m}->($self,@args);would you like $self->m(@args)?
All you have to do is cover up the syntax you dislike with a wrapper, like so:
package foo; # dispatch table my %m_implementation_for; # wrapper sub m { my ($self, @args) = @_; my $implementation = $m_implementation_for{$self}; $implementation->($self, @args); }
Afterthoughts: or more tersely,
sub m { my ($self, @args) = @_; $m_implementation_for{$self}($self, @args); }
To be still terser, replace the implementations with closures:
my %m_closure_for; # after the implementations have been defined foreach my $object (keys %m_implementation_for) { $m_closure_for{$object} = sub { $m_implementation_for{$object}($object, @_); }; } # terser wrapper sub m { my ($self, @args) = @_; $m_closure_for{$self}(@args); }
|
|---|