Perl Object-oriented

Source: Internet
Author: User



Source:



Http://www.cnblogs.com/itech/archive/2012/08/21/2649580.html





Perl Object Oriented first let's look at three basic definitions of Perl object-oriented Programming: 1. An "object" refers to a simple reference that "has a way of knowing which class it belongs to".    (object is reference variable) 2. A "class" refers to a simple package that "has the means to provide some means for the object belonging to it".  (class is package) 3. A "method" refers to a simple subroutine that "accepts an object or class name as the first parameter." (A method of a class is a method of the first argument to a class name or object) A class is just a simple package that differs from C + +, and Perl does not provide any special syntax for the class definition. The class is actually just a package. You can use a package as a class, and the function in the package as a method of a class. However, there is a special array called @ISA, which explains "Where to look when Perl cannot find the method you want in the current package." This is the key to realizing "inheritance" in Perl. Each element in the @ISA is the name of a different package. When the class cannot find a method, it looks from the @ISA array sequentially (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 argument behind can is a method that determines whether the method is defined in the 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 called. Examples of adding new methods to Unibersal:
 
1 sub UNIVERSAL::log()
2 {
3 my($self,$msg)=@_;
4 print "$self: $msg\n";
5 }
This allows the function to be called in each class, and the name of the class will be printed before the log. The object simply refers to the constructor in Perl just a subroutine, which returns a reference that is handled by bless, which is referred to as the "object", and bless is used to indicate which "class" the object belongs to. The simplest constructor:
 
1 package Critter;
2 sub new { bless {} }


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


 
1 sub new {
2 my $ this = shift;
3 my $ class = ref ($ this) || $ this; #ref supports calls of the form "$ obj-> new ()" (feeling to get the class name)
4 my $ self = ();
5 bless $ self, $ class;
6 $ self-> initialize ();
7 return $ self;
8         }





One method is a simple subroutine.


The method treats the object or class name as its first argument when it is called. There are two different ways to invoke methods, each of which is called class method and Invoke instance method. A class method treats the class name as the first argument. It provides functionality for the class, 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 what class it calls. Perl provides two different forms to invoke a method. The simplest form is the use of arrow symbols:
 
1         my $fred = Critter->find("Fred");
2         $fred->display("Height", "Weight");
You can already familiarize yourself with the reference "-a" 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 is passed as the first argument to the method to the right of the arrow. So the code above is equivalent to this:
 
1         my $fred = Critter::find("Critter", "Fred");
2         Critter::display($fred, "Height", "Weight");





Simple example


 
Simple example
1.Description
This program demonstrates how to perform object-oriented programming in Perl: The demo program contains two files: person.pl and person.pm, put person.pl and person.pm in the current directory, and run "perl person.pl" that You can see the results. person.pm defines a class named person. An instance of the person class is created in person.pl and the member methods of the person are tested.

Contents of person.pm
#! / 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;
}

Content of person.pl
#! / 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 running results
CLASS = person
CLASS = person
NAME: Tom SEX: male
NAME: Kiss SEX: female
From:


Http://bbs.chinaunix.net/thread-980909-1-1.html



http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=770196






Perl Object-oriented


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.