Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Perl object-oriented programming of Perl

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use Perl Perl object-oriented programming, the article is very detailed, has a certain reference value, interested friends must read it!

A brief introduction to the module

The module is the Perl package (pachage). Objects in Perl are based on references to data items in the package. (for references, see Chapter x references).

See http://www.nease.net/tppmsgs/msgs0.htm#28 's perlmod and perlobj for details.

When using other languages for Perl object-oriented programming, first declare a class and then create an object (instance) of that class. Any object of a particular class behaves in the same way, determined by class methods, and can be created by defining a new class or inheriting from an existing class. People who are already familiar with Perl object-oriented programming can come across many familiar terms here. Perl has always been an Perl object-oriented language, and in Perl5, the syntax has changed slightly to standardize the use of objects.

The following three definitions are critical to understanding how objects, classes, and methods work in Perl.

The ◆ class is a Perl package that contains classes that provide object methods.

The ◆ method is a Perl subroutine, and the class name is its * parameters.

The ◆ object is a reference to a data item in a class. .

2. Classes in Perl

Again, a Perl class is just a package. When you see a reference to a "class" in an Perl document, just think of it as a "package." The syntax of Perl5 can create classes, and if you are already familiar with C #, you already have most of the syntax under control. Unlike Perl4, the concept is to use a double colon (::) to identify base classes and inherited classes (subclasses).

An important object-oriented feature of Perl is inheritance. The inheritance feature in Perl is not exactly the same as in other Perl object-oriented languages. It only inherits methods, and you must use your own mechanism to implement data inheritance.

Because each class is a package, it has its own namespace and its own associative array of symbolic names (see Chapter x associative array for details), so each class can use its own separate set of symbolic names. Combined with package references, you can use single quotation marks (\') operators to locate variables in a class, such as: $class\'$member. In Perl5, you can use double colons instead of single quotation marks to get references, such as: $class\'$member is the same as $class::$member. .

Third, create a class

This section describes the necessary steps to create a new class. The following example is to create a simple class called Cocoa, whose function is to output the necessary part of the source code for a simple Java application. Rest assured, this example does not require your knowledge of Java, but it does not make you an Java expert, and its purpose is to describe the concept of creating classes.

First, create a package document named Cocoa.pm (the extension pm is the default extension for the package, meaning PerlModule). A module is a package, and a package is a class. Add the line "1;" before doing anything else, and when you add other lines, remember to keep "1;" as a line. This is a necessary condition for the Perl package, otherwise the package will not be processed by Perl. .

Fourth, the constructor

In Perl object-oriented, the constructor is a subroutine of the class, and it returns a reference related to the class name. Combining a class name with a reference is called a "blessing" object, because the function that establishes the union is called bless (), and its syntax is:

BlessYeReference [, classname]

YeReference is a reference to the "blessed" object, and classname is optional, specifying the package name of the object's get method, whose default value is the current package name.

The way to create a build function is to return a reference to the internal structure that has been combined with the class. Such as; such as.

Subnew {my$this= {}; # Createananonymoushash,and#selfpointstoit. Bless$this;#ConnectthehashtothepackageCocoa. Return$this;#Returnthereferencetothehash. }

5. Methods

The method of the Perl class is just a Perl subroutine, commonly known as a member function. The method definition of Perl in Perl object orientation does not provide any special syntax, but specifies that the * * parameters of the method are the object or the package to which it is referenced. There are two methods for Perl: static and virtual.

The parameters of the static method are class names and the parameters of the virtual method are references to the object. The way a method handles * parameters determines whether it is static or virtual. Static methods generally ignore * parameters because they already know which class they are in, and the constructor is the static method. Virtual methods usually first shift * * parameters into variables self or this, and then use the value as a normal reference. .

VI. Output of the method

If you want to reference the Cocoa.pm package now, you will get a compilation error saying that the method was not found, because the Cocoa.pm method has not been output yet. The output method requires the Exporter module, adding the following two lines to the beginning of the package:

RequireExporter

@ ISA=qw (Exporter)

The inheritance of the Perl class in Perl object orientation is achieved through the @ Isa array. The @ Isa array does not need to be defined in any package, however, once it is defined, Perl treats it as a special array of directory names. It is similar to the @ Inc array, where @ INC is the path to the containing document. The @ Isa array contains the name of the class (package). When a method is not found in the current package, look for it in the package in @ ISA. The @ ISA also contains the base class name inherited by the current class.

Any method called in a class must belong to the same class or the base class defined by the @ ISA array. If a method is not found in the @ Isa array, Perl looks for the AUTOLOAD () subroutine, an optional subroutine defined with sub in the current package. If you use the AUTOLOAD subroutine, you must call the autoload.pm package with the useAutoload; statement. The AUTOLOAD subroutine attempts to load the called method from the installed Perl library. If AUTOLOAD also fails, Perl makes another attempt to the UNIVERSAL class, and if it still fails, Perl generates an error about the unparsed function. ..

7. Method invocation

There are two ways to call a method of an object, one is through the reference to the image (virtual method), and the other is to directly use the class name (static method).

These are all the contents of the article "how to use Perl's Perl object-oriented programming". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report