First, let's take a look at three basic definitions about Perl object-oriented programming:
1. A "object" refers to a simple reference that "there is a way to know which class it belongs to." (Object is a reference variable)
2. A "class" refers to a simple package that "has a way to provide some methods to objects that belong to it." (Class is package)
3. A "method" refers to a simple subroutine that "accepts an object or class name as the first parameter". (The class method is the method whose first parameter is the class name or object)
A class is just a simple package
Unlike C++, Perl does not provide any special syntax for class definitions. In fact, the class is just a package. You can use a package as a class, and use the functions in the package as a class method. However, there is a special array called @ISA, which explains "when Perl cannot find the desired method in the current package, where should it continue to look for it." This is the key to Perl's implementation of "inheritance". @ISA each element is the name of another package. When the class cannot find a method, it looks in turn from the @ISA array (depth first). Classes know which classes are its base classes by accessing @ISA.
All classes have an implicit base class (ancestral class): "UNIVERSAL". The "UNIVERSAL" class provides several general class methods for its subclasses. It provides the following methods: isa, can. Among them isa is used to determine whether the variable is inherited from a certain class, and the parameters after can are a method to determine whether this method is defined in this class or base class. In addition, you can add new methods to UNIVERSAL. Once a new method is added to it, all classes can be called.
Example of adding new methods to UNIBERSAL:
sub UNIVERSAL::log()
{
my($self,$msg)=@_;
print "$self: $msg\n";
}
This way, this function can be called in each class, and the class name will be printed out before logging.
Objects are just references
The constructor in Perl is just a subroutine, which returns a reference processed by bless. This reference processed by bless is what people call "object", and the function of bless is to indicate which "class" the object belongs to.
The simplest constructor:
package Critter;
sub new { bless {} }
If you want users to not only call your constructor using "CLASS->new()" form, but also call it in "$obj->new()", then do this:
sub new {
my $this = shift;
my $class = ref($this) || $this;
my $self = {};
bless $self, $class;
$self->initialize();
return $self;
}
One method is a simple subroutine
The method takes the object or class name when it is called as its first parameter. There are two different ways to call methods, namely "call class methods" and "call instance methods". The class method treats the class name as the first parameter. It provides functions for classes, not for a specific object. A constructor is usually a class method. Most class methods simply ignore the first parameter because the method knows what class it is in and does not care about what class it is called through.
Perl provides two different forms to call a method. The simplest form is to use arrow symbols:
my $fred = Critter->find("Fred");
$fred->display("Height", "Weight");
You've long been familiar with the referenced "->" operator. In fact, because the above $fred is a reference pointing to an object, you can also understand the arrow operator as another form of dereference.
The reference or class name that appears on the left of the arrow will be passed as the first parameter to the method to the right of the arrow. So the above codes are equivalent to this:
my $fred = Critter::find("Critter", "Fred");
Critter::display($fred, "Height", "Weight");
Simple example
1. Description
This program demonstrates how to perform object-oriented programming in Perl: The demo program contains two files: and , put and , and run "perl" to see the results. Defines a class called person. An instance of the person class is created in it and the member methods of the person are tested.
2. Content
#!/usr/bin/perl -w
package person;
use strict;
sub new {
my $class = shift();
print("CLASS = $class\n");
my $self = {};
$self->{"name"} = shift();
$self->{"sex"} = shift();
bless $self, $class;
return $self;
}
sub getName {
my ($self) = @_;
return $self->{"name"};
}
sub setName {
my ($self, $name) = @_;
$self->{"name"} = $name;
}
sub getSex {
my ($self) = @_;
return $self->{"sex"};
}
sub setSex {
my ($self, $sex) = @_;
$self->{"sex"} = $sex;
}
3. Content
#!/usr/bin/perl -w
use strict;
use person;
sub main()
{
my $tom = person->new("Tom", "male");
my $kiss = person->new("Kiss", "female");
my @persons = ($tom, $kiss);
for my $p (@persons) {
printf("NAME: %s\tSEX: %s\n", $p->getName(), $p->getSex());
}
}
&main();
4. Program operation results
CLASS = person
CLASS = person
NAME: Tom SEX: male
NAME: Kiss SEX: female