Previous Page
Next Page

16.7. Automating Class Hierarchies

Build the standard class infrastructure automatically.

The universal constructor and destructor demonstrated in the previous guideline are, by definition, supposed to be used for every class hierarchy, in every file of every program within every system you create. So it would make sense to factor them out into a separate module, from which they could then be supplied to every class that needs them.

There is already a CPAN module that does precisely that. It's called Class::Std, and it implements all of the class infrastructure[*] shown in Example 16-9. So classes like Wax::Floor, Topping::Dessert, and Shimmer (Example 16-10 and the code that immediately follows it) could be implemented without having to construct that infrastructure yourself, merely by using Class::Std inside each class:

[*] And many other useful features as well, as described in subsequent guidelines.

    package Wax::Floor;
    use Class::Std;
# [Class definition, exactly as in
Example 16-10

Loading Class::Std installs a generic constructor that creates and initializes inside-out objects using the approach explained in the preceding guidelines, but with some other convenient shortcuts (described later). The module also installs a destructor (see the next guideline, "Attribute Demolition") that greatly simplifies the cleanup of attributes. Class::Std also exports the ident( ) utility to your class's namespace.

Class::Std provides all the benefits of inside-out objects, as well as all the benefits of decoupled initialization and cleanup (i.e., it provides full support for BUILD( ) and DEMOLISH( ) methods). It is strongly recommended for any object-oriented Perl development.

    Previous Page
    Next Page