JavaScript EditorFreeware JavaScript Editor     Perl Tutorials 

Main Page Previous Section Next Section

Recipe 11.8 Creating References to Methods

11.8.1 Problem

You want to store a reference to a method.

11.8.2 Solution

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

11.8.3 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), but 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.

11.8.4 See Also

The discussion on methods in the Introduction to Chapter 13; the section on "Closures" in Chapter 8 of Programming Perl; Recipe 11.7; Recipe 13.8

    Main Page Previous Section Next Section

    JavaScript EditorJavaScript Verifier     Perl Tutorials