Perl Object-oriented instance _perl

Source: Internet
Author: User

First, let's look at three basic definitions of Perl object-oriented programming:

1. An "object" refers to a simple reference that "there is a way to know which class it belongs to". (object is reference variable)
2. A "class" refers to a simple package that "has the means to provide some method for the object belonging to it". (class is a package)
3. A "method" refers to a simple subroutine that "accepts an object or class name as the first argument." (The method of the class is the first parameter is the method of the class name or object)

A class is just a simple package

Unlike C + +, Perl does not provide any special syntax for class definitions. Actually, the class is just a package. You can use a package as a class and use the function in the package as a class method. However, there is a special array called @ISA, which shows "where Perl should continue to look when it cannot find the method it wants in the current package." This is the key to Perl's "inheritance". Each element in the @ISA is the name of a different package. When a class cannot find a method, it looks in sequence from the @ISA array (depth first). class to know which classes are its base classes by accessing the @ISA.

All classes have an implied base class (ancestor Class): "Universal". The "Universal" class provides several common class methods for its subclasses. It provides the following methods: Isa,can. Where Isa is used to determine whether a variable inherits from a class, the parameter behind can is a method of determining whether the method is defined in this class or base class. In addition, you can add new methods to universal. Once you have added a new method to it, all classes can be invoked.

Example of adding a new method to Unibersal:

Copy Code code as follows:

Sub Universal::log ()
{
My ($self, $msg) =@_;
Print "$self: $msg \ n";
}

This allows the function to be called in each class, and the class name is printed before log.

object is simply a reference

The constructor in Perl is just a subroutine that returns a bless-processed reference, which is referred to as the "object" by the bless, and bless is used to indicate which "class" the object belongs to.

The simplest builder:

Copy Code code as follows:

Package critter;
Sub New {bless {}}

If you want users to be able to call your constructor not only in the form of "class->new ()", but also in the form of "$obj->new ()", then do it:

Copy Code code as follows:

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.

method to take the object or class name when it is invoked as its first argument. There are two different ways to invoke methods, respectively, to invoke class methods and invoke instance methods. class method treats the class name as the first argument. It provides functionality for the class, not for a specific object. Constructors are usually a class method. Most class methods simply ignore the first argument because the method knows what class it is in and does not care what class it is called.
Perl offers two different forms to invoke a method. The simplest form is to use the arrow symbol:

Copy Code code as follows:

My $fred = Critter->find ("Fred");
$fred->display ("Height", "Weight");

You can already be familiar with the quoted "->" operator. In fact, because the $fred above is a reference to an object, you can also interpret the arrow operator as another form of dereference.

The reference or class name that appears to the left of the arrow and is passed as the first argument to the method to the right of the arrow. So the code above is equivalent to this:

Copy Code code as follows:

My $fred = Critter::find ("Critter", "Fred");
Critter::d isplay ($fred, "Height", "Weight");

Simple example

1, the description

This program demonstrates how to do object-oriented programming in Perl: The demo program contains two files: person.pl and person.pm, placing person.pl and person.pm in the current directory, and running "Perl person.pl" to see the results. PERSON.PM defines a class named person. An instance of the person class was created in person.pl, and the member method of the person was tested.

2, the content of person.pm

Copy Code code as follows:

#!/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, the content of person.pl

Copy Code code as follows:

#!/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. Results of program operation

Copy Code code as follows:

CLASS = person
CLASS = person
Name:tom Sex:male
Name:kiss Sex:female

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.