bless有两个参数:对象的引用、类的名称。 
类的名称是一个字符串,代表了类的类型信息,这是理解bless的关键。 
所谓bless就是把 类型信息 赋予 实例变量。

[xywang@mnsdev13:~]$ cat Person.pm
#!/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; [xywang@mnsdev13:~]$ cat Dog.pm
#!/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; [xywang@mnsdev13:~]$ cat bless.pl
#!/usr/bin/perl
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(); [xywang@mnsdev13:~]$ ./bless.pl
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

  以下为错误的使用:

[xywang@mnsdev13:~]$ cat wrong_bless.pl
#!/usr/bin/perl
use strict;
use Person;
use Dog; sub main()
{
my $object = {"name" => "tom"}; #没有把类型信息和$object绑定,因此无法获知$object有sleep方法
$object->sleep();
$object->study();
} &main(); [xywang@mnsdev13:~]$ ./wrong_bless.pl
Can't call method "sleep" on unblessed reference at ./wrong_bless.pl line 11.

  

Perl中的bless的理解的更多相关文章

  1. 【Perl学习笔记】2. perl中的bless理解

    bless有两个参数:对象的引用.类的名称. 类的名称是一个字符串,代表了类的类型信息,这是理解bless的关键. 所谓bless就是把 类型信息 赋予 实例变量. 程序包括5个文件:person.p ...

  2. Perl中的特殊内置变量详解

    #!/usr/bin/perl -w @array = qw(a b c d); foreach (@array) { print $_," "; } 例子的作用就是定义一个数组并 ...

  3. perl中my和our的区别分析

    来源: http://www.jb51.net/article/35528.htm perl中our的用法require 5.006当版本号小于 5.006 的时候,会返回失败,从而导致模块加载失败. ...

  4. Perl中的执行上下文

    perl中的上下文 在perl中,很多地方会切换上下文.所谓上下文,它的重点在于同一个表达式出现在不同地方,得到的结果不同.换句话说,同一个表达式,它表达的值不是固定的.这就像是同一个单词,在不同语境 ...

  5. SQL SERVER 2005/2008 中关于架构的理解(二)

    本文上接SQL SERVER 2005/2008 中关于架构的理解(一)      架构的作用与示例 用户与架构(schema)分开,让数据库内各对象不再绑在某个用户账号上,可以解决SQL SERVE ...

  6. SQL SERVER 2005/2008 中关于架构的理解(一)

    SQL SERVER 2005/2008 中关于架构的理解(一) 在一次的实际工作中碰到以下情况,在 SQL SERVER 2008中,新建了一个新用户去访问几张由其他用户创建的表,但是无法进行查询, ...

  7. Perl中的正则表达式

    转自:http://c20031776.blog.163.com/blog/static/684716252013624383887/ Perl 程序中,正则表达式有三种存在形式 分别是 (1 模式匹 ...

  8. Perl中的替换(七)

    在Perl中使用s///进行替换操作,与m//进行查找操作类似. s/with (\w+)/against $1's team/;      ##第一个双斜线,表示被替代的文本.第二个双斜线,表示将替 ...

  9. Perl中的匹配(六)

    在Perl中,匹配的定界符如果是双斜线//,可以直接使用双斜线完成匹配操作. 如果特定条件下需要改变定界符,如改为{},[]等.需要加入m,m{},m[]等. m%^http://% 默认的模式匹配对 ...

随机推荐

  1. SPA是什么?

    认识SPA 最早单页面的应用无从知晓,在2004年,google的Gmail就使用了单页面.到了2010年,随着Backbone的问世之后,此概念才慢慢热了起来. 随着后来React.Angular. ...

  2. oracle表内连接和外连接

    n  概述 表连接分为内连接和外连接 n  内连接 内连接实际上就是利用where子句对两张表形成的笛卡尔集进行筛选,我们前面学习的查询都是内连接,也是在开发过程中用的最多的连接查询. 基本语法: s ...

  3. 如何入门 C++ AMP 教程

    本文告诉大家如何写一个 Helloworld 程序. 首先打开 VisualStudio ,大概现在也没有人还在用 VisualStudio 2013 了,所以我就不需要告诉大家需要用哪个版本的 Vi ...

  4. windows 关闭端口被占用脚本

    cmd 关闭进程java taskkill /F /IM java.exe taskkill /f /im java.exe 如何用dat批处理文件关闭某端口对应程序-Windows自动化命令 如何用 ...

  5. 最强 NLP 预训练模型库 PyTorch-Transformers 正式开源:支持 6 个预训练框架,27 个预训练模型

    先上开源地址: https://github.com/huggingface/pytorch-transformers#quick-tour 官网: https://huggingface.co/py ...

  6. supersockets单个 listener

    在下面的配置中,你可以配置服务器的监听 ip/port: <superSocket> <servers> <server name="TelnetServer& ...

  7. lua在C/C++中使用table生成对应键及值

    int nTop = lua_gettop(L); // 栈内初始数,假设当前为0 lua_newtable(L); // push table lua_pushstring(L,"Line ...

  8. HDU 5974"A Simple Math Problem"(GCD(a,b) = GCD(a+b,ab) = 1)

    传送门 •题意 已知 $a,b$,求满足 $x+y=a\ ,\ LCM(x,y)=b$ 条件的 $x,y$: 其中,$a,b$ 为正整数,$x,y$ 为整数: •题解 关键式子:设 $a,b$ 为正整 ...

  9. Python--day44--navicat使用(知道怎么用就好,要用终端操作,用这个会被人鄙视)

  10. Python--day39--管道和数据共享(面试可能会问到)

    1,管道 上面所述挂起即为阻塞 管道.py from multiprocessing import Pipe, Process def func(conn1,conn2): conn2.close() ...