in reply to Dynamic object extensions

At the moment all I can think of is a hash dispatch table, but this seems to rule out calls like $object->custom_method unless maybe autoload (which I am kind of frightened of) can be persuaded to do it.

Face your fears. Embrace AUTOLOAD.

Here is one approach:

package Frob; sub new { my $pkg = shift; bless {}, $pkg } sub customize { my ($self, $method, $code) = @_; $self->{_custom}->{$method} = $code; } sub AUTOLOAD { my $self = shift; my $method = $AUTOLOAD; $method =~ s/.*:://; if ( exists $self->{_custom}->{$method} ) { $self->{_custom}->{$method}->(@_) } else { die "no code for '$method'"; } } package main; $|++; # so as not to suffer from buffering my $frob = new Frob(); $frob->customize('foo', sub { print "foo @_!\n" }); $frob->foo(42); # should work $frob->bar(42); # should fail