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. WampServer PHP服务配置方法(允许外部访问、phpmyadmin设置为输入用户名密码才可登录等)

    WampSever 指的是apache + mySQL + PHP 三合一套装,第一字母W,是指用于windows系统,我用的是2.0f版.用于Linux系统的,是LampSever,第一字母是L.请 ...

  2. Flink资料(8) -- Flink代码贡献的指导及准则

    本文翻译自Contributing Code ----------------------------------------- Apache Flink是由自愿的代码贡献者维护.优化及扩展的.Apa ...

  3. The number of positions

    Description The number of positions time limit per test: 0.5 second memory limit per test: 256 megab ...

  4. 查看ORACLE中正在运行的存储过程 kill

    1:登陆PLSQL Developer,写一个存储过程,向一个表中插入值,并运行存储过程 2:打开PLSQL Developer的命令窗口 .--终止procedure   11.select * f ...

  5. linux搭建php

    http://www.cnblogs.com/wubaiqing/archive/2011/11/08/2241195.html 四,启动php-fpm & Nginx /usr/local/ ...

  6. 九度OnlineJudge之1021:统计字符

    题目描述:     统计一个给定字符串中指定的字符出现的次数. 输入:     测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串.注 ...

  7. uestc 10 In Galgame We Trust

    题意:求最长的合法括号序列 解:栈+分类讨论 now表示已经算出的序列,且此序列与现在扫描的序列可能能够连接,tmp表示现在扫描到的序列长度 左括号入栈 右括号:1.栈空时:统计当前总长 并且将栈,n ...

  8. python-认识Socket[入门篇]

    什么是socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket.socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链 ...

  9. NSJSONSerialization(category)的一个扩展类

    .h文件 // // NSJSONSerialization+Manage.h // SVPullToRefreshDemo // // Created by Fuer on 14-7-4. // C ...

  10. 假设说这个世界不是真实存在的,仅仅是一段代码,迄今为止你发现了哪些bug?

    给这个世界写代码的不是一个人,而是一个团队(这么大的项目,一个人开发不了).并且严重怀疑这个一个开源项目.开发人员被我们觉得是神,所以一神论是不正确的,众神论才是真理,且凡人是有机会成为神的(參悟神道 ...