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
In reply to Re: Dynamic object extensions
by dws
in thread Dynamic object extensions
by jepri
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |