in reply to How do I create a reference to an object method?
If you want to call the method on that particular object, I think the closure solution is best.
my $action = { doit => sub { $object->it( @_ ) } };
That way, you can call $action->{doit}->( ... ), and it's the same as calling $object->it( ... ). That's nice if you want to forget about the particular object.
If you want a reference to the sub that would be called, you can use UNIVERSAL::can:
my $action = { doit => $object->can( 'it' ) };
In that case, you'd have a reference to the method, but the object itself is not in your hash. To call it as an instance method as above, you'd have to $action->{doit}->( $object, ... ). That's nice if you want to call it with different objects.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How do I create a reference to an object method?
by ysth (Canon) on Jun 26, 2008 at 02:02 UTC |