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

Re: RFC from a newbie: Method References

Thread Previous | Thread Next
Jeremy Howard
December 16, 2000 17:50
Re: RFC from a newbie: Method References
Message ID:
Michael G Schwern wrote:
> 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);
>     $meth_ref->();
> is equivalent to this..
>     $obj->foo(@some_stuff);
> You could even make the meth_ref take additional (or overriding)
> Its a good idea, I'll put Class::MethRef on CPAN soon.
Something to be careful of--it's easy to create a circular reference when
using method pointers. As a result, neither the referrer nor referee objects
are ever destroyed.

When using method references it's important that your class keeps track of
its references, and explicitly removes them when done to allow object
destruction to occur. Perhaps this could be incorporated into

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