Section 16.7. Automating Class Hierarchies


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.



Perl Best Practices
Perl Best Practices
ISBN: 0596001738
EAN: 2147483647
Year: 2004
Pages: 350
Authors: Damian Conway

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net