OO in Perl5

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

总结:

  • 万物皆对象,对象既是类(包)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值