bless有两个参数:对象的引用、类的名称。 
类的名称是一个字符串,代表了类的类型信息,这是理解bless的关键。 
所谓bless就是把 类型信息 赋予 实例变量。 
程序包括5个文件:
person.pm :实现了person类 
dog.pm :实现了dog类 
bless.pl : 正确的使用bless
bless.wrong.pl : 错误的使用bless
bless.cc : 使用C++语言实现了与bless.pl相同功能的代码

person.pm 
CODE:
#!/usr/bin/perl -w
package person;
use strict;

sub sleep() {
       my ($self) = @_;
       my $name = $self->{"name"};

print("$name is person, he is sleeping/n");
}

sub study() {
       my ($self) = @_;
       my $name = $self->{"name"};

print("$name is person, he is studying/n");
}
return 1;

dog.pm 
CODE:
#!/usr/bin/perl -w
package dog;
use strict;

sub sleep() {
       my ($self) = @_;
       my $name = $self->{"name"};

print("$name is dog, he is sleeping/n");
}

sub bark() {
       my ($self) = @_;
       my $name = $self->{"name"};

print("$name is dog, he is barking/n");
}

return 1;

bless.pl 
CODE:
#!/usr/bin/perl =w
use strict;
use person;
use dog;

sub main()
{
       my $object = {"name" => "tom"};

# 先把"tom"变为人
       bless($object, "person");
       $object->sleep();
       $object->study();

# 再把"tom"变为狗
       bless($object, "dog");
       $object->sleep();
       $object->bark();

# 最后,再把"tom"变回人
       bless($object, "person");
       $object->sleep();
       $object->study();
}

&main();

# 程序运行时输出:
# tom is person, he is sleeping
# tom is person, he is studying
# tom is dog, he is sleeping
# tom is dog, he is barking
# tom is person, he is sleeping
# tom is person, he is studying

bless.wrong.pl 
CODE:
#!/usr/bin/perl =w
use strict;
use person;
use dog;

sub main()
{
       my $object = {"name" => "tom"};

# 没有把类型信息和$object绑定,因此无法获知$object有sleep方法
       $object->sleep();
       $object->study();
}

&main();

# 程序运行输出为:
# Can't call method "sleep" on unblessed reference at bless.wrong.pl line 10.

使用c++实现bless的功能

c中的代码 
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct object {
       char name[16];
};

struct person {
       char name[16];

void sleep() { printf("%s is person, he is sleeping/n", this->name); }
       void study() { printf("%s is person, he is studying/n", this->name); }
};

struct dog {
       char name[16];

void sleep() { printf("%s is dog, he is sleeping/n", this->name); }
       void bark() { printf("%s is dog, he is barking/n", this->name); }
};

#define bless(object, type) ((type*) object)

int main()
{
       struct object * o = (struct object *) malloc(sizeof(struct object));
       strcpy(o->name, "tom");

// 先把"tom"变为人
       bless(o, person)->sleep();
       bless(o, person)->study();

// 再把"tom"变为狗
       bless(o, dog)->sleep();
       bless(o, dog)->bark();

// 最后,再把"tom"变回人
       bless(o, person)->sleep();
       bless(o, person)->study();
       return 0;
}

// 程序运行时输出:
// tom is person, he is sleeping
// tom is person, he is studying
// tom is dog, he is sleeping
// tom is dog, he is barking
// tom is person, he is sleeping
// tom is person, he is studying

【Perl学习笔记】2. perl中的bless理解的更多相关文章

  1. 并发编程学习笔记(4)----jdk5中提供的原子类及Lock使用及原理

    (1)jdk中原子类的使用: jdk5中提供了很多原子类,它会使变量的操作变成原子性的. 原子性:原子性指的是一个操作是不可中断的,即使是在多个线程一起操作的情况下,一个操作一旦开始,就不会被其他线程 ...

  2. [学习笔记] 在Eclipse中导入项目

    参考前文:[学习笔记] 在Eclips 中导出项目 选择已经导出的文件: 导入之后,项目结构如下: 至此,完成.

  3. CockroachDB学习笔记——[译]CockroachDB中的SQL:映射表中数据到键值存储

    CockroachDB学习笔记--[译]CockroachDB中的SQL:映射表中数据到键值存储 原文标题:SQL in CockroachDB: Mapping Table Data to Key- ...

  4. [学习笔记] 在Eclipse中导出可以直接运行的jar,依赖的jar中的类解压后放在运行jar中

    前文: [学习笔记] 在Eclipse中导出可以直接运行的jar,依赖的jar打在jar包中 使用7z打开压缩包,查看所有依赖的jar都被解压以包名及class的方式存储在了运行jar中,此时jar的 ...

  5. [学习笔记] 在Eclipse中导出可以直接运行的jar,依赖的jar打在jar包中

    本文需要参考前文: [学习笔记] 在Eclipse中导出可以直接运行的jar,依赖的jar在子目录中 上文是导出的运行的依赖jar被放在了子目录中,本文是将依赖jar放在可运行jar的本身,这样发布的 ...

  6. [学习笔记] 在Eclipse中导出可以直接运行的jar,依赖的jar在子目录中

    工程创建可参考前文: [学习笔记] 在Eclipse中使用Hibernate,并创建第一个工程,数据库为Oracle XE 在工程上鼠标右键: 找到java 选择 Runable JAR file N ...

  7. Web安全学习笔记 SQL注入中

    Web安全学习笔记 SQL注入中 繁枝插云欣 --ICML8 权限提升 数据库检测 绕过技巧 一.权限提升 1. UDF提权 UDF User Defined Function,用户自定义函数 是My ...

  8. Perl 学习笔记-标量数据

    最近学习Perl, 准备看一遍入门指南,关键的东西还是记录下来,以便以后复习和查看参考. 笔记来自<<Perl语言入门第5版>> 1. 在Perl内部,不区分整数值和浮点数值, ...

  9. Perl 学习笔记-高级控制结构

    1.unless控制结构 类似于独立的else语句; 要么条件为真, 要么执行语句块内的代码;  unless(<condition>){code...;} 等价于  if(<con ...

  10. Perl 学习笔记-列表和数组

    笔记来自<<Perl语言入门第5版>> 1. Perl中列表指标量的有序集合,数组则是存储列表的变量, 这两个术语经常混用,不过更精确地说,列表指数据,而数组指变量.数组的表示 ...

随机推荐

  1. 管理node_modules

    http://stackoverflow.com/questions/15225865/centralise-node-modules-in-project-with-subproject

  2. hdu 2685 I won't tell you this is about number theory 数论

    题目链接 根据公式 \[ gcd(a^m-1, a^n-1) = a^{gcd(m, n)}-1 \] 就可以很容易的做出来了. #include <iostream> #include ...

  3. VHDL设计时参数定义的方法 例子

    -- SPtb LIBRARY ieee; USE ieee.std_logic_1164.ALL; USE IEEE.STD_LOGIC_ARITH.ALL; USE IEEE.STD_LOGIC_ ...

  4. 迁移到gitbook

    现在要迁移到gitbook啦, 一些note类分享就只在gitbook发了, 其他一些比较长的分享会第一时间发到gitbook,但也会在这边同步 我的gitbook

  5. 简易对象垃圾回收框架 for Delphi

    (一).缘起 1.1 我的一个出错程序 程序名称:呼叫处理模块的压力测试工具,分为客户端和服务端. 开发工具:Delhpi 5 相关技术:客户端通过与服务端建立Socket连接来模拟一组电话机的拨入. ...

  6. Android updater-scripts(Edify Script)各函数详细说明(转)

    这是Android系统来运行updater-scripts的Edify语言的基本介绍. 大部分的Edify命名都是函数,当调用这些函数结束的时候,会返回数据给脚本.当然,你也可以使用这些函数的返回值来 ...

  7. 清风注解-Swift程序设计语言:Point1~5

    目录索引 清风注解-Swift程序设计语言 Point 1. Swift 风格的"Hello, world" 代码事例: println("Hello, world&qu ...

  8. 【POJ】2492 A bug's life ——种类并查集

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 De ...

  9. Mongodb入门并使用java操作Mongodb

    转载请注意出处:http://blog.csdn.net/zcm101 最近在学习NoSql,先从Mongodb入手,把最近学习的总结下. Mongodb下载安装 Mongodb的下载安装就不详细说了 ...

  10. JQuery打造下拉框联动效果

    做联动效果,若是用纯JavaScript来做,往往须要辅助页面保存须要刷新的结果集,然后渲染到原页面.考虑将须要动态刷新的内容自己主动拼接到前一个下拉框之后,当前一个下拉框onchange后,同级的后 ...