OO
说起 OO 首先跳出“Object”与“Class”这个两个概念。Perl也不例外,在 Perl5 中对 OO programming进行了设计与支持。如下图所示:
Perl 5 中设计了一个Base class UNIVERSAL, 它是所有Class的最终父类。如下所述:
All classes inherit from the UNIVERSAL class implicitly.The UNIVERSAL class is implemented by the Perl core, and provides several default methods, such as isa() , can() , and VERSION() . The UNIVERSAL class will never appear in a package’s @ISA variable.
Perl 5 中的 OO 术语:
Object: 被实例化的类的取值reference的指向(data structure)。当然,明确指代也可以忽略这个约定。
Class: 在定义时,使用 Package 关键词。
Method:Subroutine,用 sub 定义。
Attribute: 可以为SCALAR, ARRAY,HASH, SCALAR | ARRAY | HASH REFERENCE …
Reference: 可理解为 C 中指针,在 Perl 中广泛使用,使用灵活
Class
- A class is simply a package.
- A class provides methods that expect to operate on objects.
Object
In a sense, everything in Perl is an object, but we usually reserve the word for references to objects that have been officially “blessed” into a class package.被实例化的类的取值reference的指向(data structure), 在 Perl 5 中 通常被称为 “对象(The object)”。
- An object is simply a data structure that knows to which class it belongs.
Reference
When a scalar is holding a reference, it always behaves as a simple scalar. It doesn’t magically start being an array or hash or subroutine; you have to tell it explicitly to do so, by dereferencing it.
- Everything in Perl is an object
Method
- A method is simply a subroutine that expects a reference to an object (or a package name, for
class methods) as the first argument.
综上所述: Everything in Perl –(is)–> Object –(belongs to)–> Class == Package
总结:
- 万物皆对象,对象既是类(包)