While Perl allows us infinite flexibility in how we organize our modules, we choose to stick to the particular set of conventions introduced in this chapter so that everyone deals with modules in a consistent fashion. Let us quickly summarize these conventions:
A module must be present in its own file called
module>.pm. (Remember that the last
executing global statement must return 1 to signify successful
All subroutines in a module should be designed as methods. That is, they should expect either the name of a class or an object reference as their first parameter. For added convenience, they should be able to deal with either.
Package names should never be hardcoded. You must always use the
package name obtained as the first argument to supply to
bless. This enables a constructor to be inherited.
Always provide accessor methods for class and instance attributes.
The following example puts all these techniques and conventions into practice.
Consider a store that sells computers and individual components. Each component has a model number, a price, and a rebate. A customer can buy individual components, but can also put together a custom computer with specific components. The store adds a sales tax to the final price. The objective of this example is to provide the net price on any item you can buy from the store.
We need to account for the facts that a part may consist of other parts, that the sales tax may depend on the type of part and the customer’s ...