| Start |
|---|
| Defining classes in modern Perl Although available, defining a class from scratch (opens new window) is not recommended in modern Perl. Use one of helper OO systems which provide more features and convenience. Among these systems are: Moose Class::Accessor (Moose syntax) Class::Accessor (native syntax) Class::Tiny |
| Creating Objects Unlike many other languages, Perl does not have constructors that allocate memory for your objects. Instead, one should write a class method that both create a data structure and populate it with data (you may know it as the Factory Method design pattern). This method can be used as follows: Whenever the arrow operator There is nothing special in the name There is nothing special in hashes. You could do the same in the following way: In general, any reference may be an object, even a scalar reference. But most often, hashes are the most convenient way to represent object data. |
| Defining Classes In general, classes in Perl are just packages. They can contain data and methods, as usual packages. It is important to note that the variables declared in a package are class variables, not object (instance) variables. Changing of a package-level variable affects all objects of the class. How to store object-specific data, see in "Creating Objects". What makes class packages specific, is the arrow operator or after a scalar variable (usually holding a reference): What is to the left of the arrow is prepended to the given argument list of the method. For example, after call array Packages representing classes should take this convention into account and expect that all their methods will have one extra argument. |
| Inheritance and methods resolution To make a class a subclass of another class, use Perl allows for multiple inheritance: Inheritance is all about resolution which method is to be called in a particular situation. Since pure Perl does not prescribe any rules about the data structure used to store object data, inheritance has nothing to do with that. Consider the following class hierarchy: The method resolution works as follows:
Point2D::new exists, will simply call it. parent classes is performed. In the example above, the search order will be as follows:
the method that will be called is PlanarObject::transpose like so: will call
|
| Class and Object Methods In Perl, the difference between class (static) and object (instance) methods is not so strong as in some other languages, but it still exists. The left operand of the arrow operator or an object reference: Class methods are just the ones that expect their first argument to be a string, and object methods are the ones that expect their first argument to be an object reference. Class methods typically do not do anything with their first argument, which is just a name of the class. Generally, it is only used by Perl itself for method resolution. Therefore, a typical class method can be called for an object as well: Although this syntax is allowed, it is often misleading, so it is better to avoid it. Object methods receive an object reference as the first argument, so they can address the object data (unlike class methods): The same method can track both cases: when it is called as a class or an object method: |
| Roles A role in Perl is essentially
A role provides a piece of functionality which can be composed into (or applied to) any class (which is said to consume the role). A role cannot be inherited but may be consumed by another role. A role may also require consuming classes to implement some methods instead of implementing the methods itself (just like interfaces in Java or C#). Perl does not have built-in support for roles but there are CPAN classes which provide such support. Moose::Role (opens new window) Use if your OO system does not provide support for roles (e.g. |
Read Article
https://develop.horje.com/learn/1434/reference