develooper Front page | perl.perl6.language.objects | Postings from December 2000

Re: RFC from a newbie: Method References

Thread Previous | Thread Next
Michael G Schwern
December 16, 2000 14:30
Re: RFC from a newbie: Method References
Message ID:
On Fri, Dec 15, 2000 at 11:23:23PM -0800, Ian Hickson wrote:
> Having said that: The only feature that I really miss from Perl 5 is the
> lack of method pointers -- that is, references to functions in a package
> with an associated object.

Oh, that's easy.  Use a closure...

my $foo = Foo->new;
my $meth_ref = sub { $foo->method };

In fact, here's an example implementation...

package Class::MethRef;
use strict;

sub meth_ref {
	my($proto, $method, @args) = @_;
	return sub { $proto->$method(@args) };

So this...

    my $meth_ref = $obj->meth_ref('foo', @some_stuff);

is equivalent to this..


You could even make the meth_ref take additional (or overriding) arguments.

Its a good idea, I'll put Class::MethRef on CPAN soon.

Michael G. Schwern   <>

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About