Multiple Dispatch and Subroutine Overloading in Perl

Damian Conway

School of Computer Science and Software Engineering
Monash University
Clayton 3168, Australia

damian@csse.monash.edu.au
http://www.csse.monash.edu.au/~damian

Abstract

Sometimes Perl's standard polymorphic method dispatch mechanism isn't sophisticated enough to cope with the complexities of finding the right method to handle a given situation. For example, in a graphical user interface, objects representing events may be passed to objects representing windows. What happen next depends on the type of window, but also on the type of event. It's not enough to invoke a receive_event method on the window object, since that won't distinguish between the various possible kinds of event. Nor is it sufficient to invoke a send_to_window method on the event object, since that won't distinguish between the various possible kinds of window. What's needed is the ability to polymorphically select a suitable method for the appropriate combination of window and event types. This talk describes a new CPAN module -- Class::Multimethods -- that provides such a mechanism.

The talk

The talk will focus on explaining why you might want (or need) to use multiple dispatch and on how the Class::Multimethods module makes it easy to do so.  Some ugly examples of "hand-crafted" multiple dispatch will be shown to motivate the need for an automated solution. I will also show how the Class::Multimethods module offers the ability to overload subroutines by their argument types, and why you might ever do such an "un-Perlish" thing.

After the talk the presentation will be available on-line in HTML format.

The paper

An extended abstract is available in HTML format.

The full paper is available in both HTML and PostScript.
 

The software

The Class::Multimethods module is available from the CPAN.

Code examples from the talk will available from the author's web site.

Futher reading

  1. Conway, D., Object Oriented Perl, Chapter 13, Manning Publications, 1999.
  2. Conway, D., Multiple Dispatch in Perl,The Perl Journal (to appear).