Skip to content

Object Orientation in Perl is just a Design Pattern

Stevan Little edited this page Sep 11, 2016 · 2 revisions

The mechanisms of OO Perl are few and they all operate off of the assumption that users have followed the agreed upon patterns.

  • classes are packages
  • methods are subs in a package
  • our @ISA is used to store inheritance relationship
  • bless is used to attach a reference (instance) to a package (class)
  • methods are dispatched using the -> operator

This proposal aims to add feature support for roles and for specifying attributes (or slots). In order to do this, we want to add a few more things to the pattern.

  • roles are just packages
  • role methods are subs that are defined in a package
    • inherited methods are ignored
    • imported subs are ignored
  • required methods are just pre-declared subs (sub foo;)
  • our @DOES is used to store role relationships
  • our %HAS is used to store attribute/member/slot information

As with the other patterns, we will need to invoke certain "magic" in order to integrate them into the language, but in general these mechanisms can be accomplished manually using a few functions that are called from well placed BEGIN blocks.

A few nice features just "happen" that I would like to capitalize upon.

  • Abstract classes, they are basically a class which has one or more required methods.
    • The same "required" methods we have in roles can be abstract methods in abstract classes.
  • It is possible to close a class/package (see Module System), which implies a promise of no future alteration will be done, which means we can do some work to optimize things.