Book HomePerl CookbookSearch this book

11.8. Creating References to Methods

Problem

You want to store a reference to a method.

Solution

Create a closure that makes the proper method call on the appropriate object.

Discussion

When you ask for a reference to a method, you're asking for more than just a raw function pointer. You also need to record which object the method needs to be called upon as the object contains the data the method will work with. The best way to do this is using a closure. Assuming $obj is lexically scoped, you can say:

$mref = sub { $obj->meth(@_) }; 
# later...  
$mref->("args", "go", "here");

Even when $obj goes out of scope, the closure stored in $mref has captured it. Later when it's called indirectly, the correct object is used for the method call.

Be aware that the notation:

$sref = \$obj->meth;

doesn't do what you probably expected. It first calls the method on that object and gives you either a reference to the return value or a reference to the last of the return values if the method returns a list.

The can method from the UNIVERSAL base class, while appealing, is also unlikely to produce what you want.

$cref = $obj->can("meth");

This produces a code ref to the appropriate method (should one be found), one that carries no object information. Think of it as a raw function pointer. The information about the object is lost. That's why you need a closure to capture both the object state as well as the method to call.

See Also

The discussion on methods in the Introduction to Chapter 13; Recipe 11.7; Recipe 13.7


Previous: 11.7. Using Closures Instead of ObjectsPerl CookbookNext: 11.9. Constructing Records
11.7. Using Closures Instead of ObjectsBook Index11.9. Constructing Records

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.