We've looked at several approaches to accessing per-object data values. Sometimes, though, you want some common state shared by all objects of a class. Instead of being an attribute of just one instance of the class, these variables are global to the entire class, no matter which class instance (object) you use to access them through. (C++ programmers would think of these as static member data.) Here are some situations where class variables might come in handy:
To keep a count of all objects ever created, or how many are still kicking around.
To keep a list of all objects over which you can iterate.
To store the name or file descriptor of a log file used by a class-wide debugging method.
To keep collective data, like the total amount of cash dispensed by all ATMs in a network in a given day.
To track the last object created by a class, or the most accessed object.
To keep a cache of in-memory objects that have already been reconstituted from persistent memory.
To provide an inverted lookup table so you can find an object based on the value one of its attributes.
The question comes down to deciding where to store the state for those shared attributes. Perl has no particular syntactic mechanism to declare class attributes, any more than it has for instance attributes. Perl provides the developer with a broad set of powerful but flexible features that can be uniquely crafted to the particular demands of the situation. You can then select the mechanism that makes the ...