More easily explained with an example:
my $o = SpecialEffects->new( "config" => 'a' );
my $p = SpecialEffects->new( "config" => 'b' );
$o->sound(); # aliased to fizz(); same as $o->fizz()
$p->sound(); # aliased to clonk(); same as $p->clonk()
Is it possible to do this in Perl? Perhaps using some typeglob or coderef trickery?
I’m trying to keep the SpecialEffects interface simple. I’d prefer not to start building an object hierarchy. The sound() method is what’s exposed, only its behaviour can be configured slightly.
I already know that you can alias with *sound = \&fizz; but that is a global thing as far as I know, and I’d like it encapsulated in the object.
The simple, easy, non-wizardry way is to just store a method name in your
SpecialEffectsobject, set it according to whatever you want to happen, and call it fromsound().You can store and use coderefs about as easily as method names, if you want to be more wizardly.