Tutorial by Examples

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). package Point; use strict; sub new { ...
In general, classes in Perl are just packages. They can contain data and methods, as usual packages. package Point; use strict; my $CANVAS_SIZE = [1000, 1000]; sub new { ... } sub polar_coordinates { ... } 1; It is important to note that the variables declared in a packa...
To make a class a subclass of another class, use parent pragma: package Point; use strict; ... 1; package Point2D; use strict; use parent qw(Point); ... 1; package Point3D; use strict; use parent qw(Point); ... 1; Perl allows for multiple inheritance: package Point2D; use stri...
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 -> becomes the first argument of the method to be called. It may be either a string: # the first argument of new ...
Although available, defining a class from scratch is not recommended in modern Perl. Use one of helper OO systems which provide more features and convenience. Among these systems are: Moose - inspired by Perl 6 OO design Class::Accessor - a lightweight alternative to Moose Class::Tiny...
A role in Perl is essentially a set of methods and attributes which injected into a class directly. 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 rol...

Page 1 of 1