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 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 ->
. It may be used after a bare word:
Point->new(...);
or after a scalar variable (usually holding a reference):
my @polar = $point->polar_coordinates;
What is to the left of the arrow is prepended to the given argument list of the method. For example, after call
Point->new(1, 2);
array @_
in new
will contain three arguments: ('Point', 1, 2)
.
Packages representing classes should take this convention into account and expect that all their methods will have one extra argument.