perl bless

程序说明

网上的很多教程都没有把bless讲清楚,我通过摸索和实验,终于明白bless是什么意思了,简单的讲:

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

程序包括5个文件:

person.pm :实现了person类

dog.pm :实现了dog类

bless.pl : 正确的使用bless

bless.wrong.pl : 错误的使用bless

bless.cc : 使用C++语言实现了与bless.pl相同功能的代码

person.pm

  1. #!/usr/bin/perl -w
  2. package person;
  3. use strict;
  4. sub sleep() {
  5. my ($self) = @_;
  6. my $name = $self->{"name"};
  7. print("$name is person, he is sleeping\n");
  8. }
  9. sub study() {
  10. my ($self) = @_;
  11. my $name = $self->{"name"};
  12. print("$name is person, he is studying\n");
  13. }
  14. return 1;
复制代码

dog.pm

  1. #!/usr/bin/perl -w
  2. package dog;
  3. use strict;
  4. sub sleep() {
  5. my ($self) = @_;
  6. my $name = $self->{"name"};
  7. print("$name is dog, he is sleeping\n");
  8. }
  9. sub bark() {
  10. my ($self) = @_;
  11. my $name = $self->{"name"};
  12. print("$name is dog, he is barking\n");
  13. }
  14. return 1;
复制代码

bless.pl

  1. #!/usr/bin/perl =w
  2. use strict;
  3. use person;
  4. use dog;
  5. sub main()
  6. {
  7. my $object = {"name" => "tom"};
  8. # 先把"tom"变为人
  9. bless($object, "person");
  10. $object->sleep();
  11. $object->study();
  12. # 再把"tom"变为狗
  13. bless($object, "dog");
  14. $object->sleep();
  15. $object->bark();
  16. # 最后,再把"tom"变回人
  17. bless($object, "person");
  18. $object->sleep();
  19. $object->study();
  20. }
  21. &main();
  22. # 程序运行时输出:
  23. # tom is person, he is sleeping
  24. # tom is person, he is studying
  25. # tom is dog, he is sleeping
  26. # tom is dog, he is barking
  27. # tom is person, he is sleeping
  28. # tom is person, he is studying
复制代码

bless.wrong.pl

  1. #!/usr/bin/perl =w
  2. use strict;
  3. use person;
  4. use dog;
  5. sub main()
  6. {
  7. my $object = {"name" => "tom"};
  8. # 没有把类型信息和$object绑定,因此无法获知$object有sleep方法
  9. $object->sleep();
  10. $object->study();
  11. }
  12. &main();
  13. # 程序运行输出为:
  14. # Can't call method "sleep" on unblessed reference at bless.wrong.pl line 10.
复制代码

使用c++实现bless的功能

c中的代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct object {
  5. char name[16];
  6. };
  7. struct person {
  8. char name[16];
  9. void sleep() { printf("%s is person, he is sleeping\n", this->name); }
  10. void study() { printf("%s is person, he is studying\n", this->name); }
  11. };
  12. struct dog {
  13. char name[16];
  14. void sleep() { printf("%s is dog, he is sleeping\n", this->name); }
  15. void bark() { printf("%s is dog, he is barking\n", this->name); }
  16. };
  17. #define bless(object, type) ((type*) object)
  18. int main()
  19. {
  20. struct object * o = (struct object *) malloc(sizeof(struct object));
  21. strcpy(o->name, "tom");
  22. // 先把"tom"变为人
  23. bless(o, person)->sleep();
  24. bless(o, person)->study();
  25. // 再把"tom"变为狗
  26. bless(o, dog)->sleep();
  27. bless(o, dog)->bark();
  28. // 最后,再把"tom"变回人
  29. bless(o, person)->sleep();
  30. bless(o, person)->study();
  31. return 0;
  32. }
  33. // 程序运行时输出:
  34. // tom is person, he is sleeping
  35. // tom is person, he is studying
  36. // tom is dog, he is sleeping
  37. // tom is dog, he is barking
  38. // tom is person, he is sleeping
  39. // tom is person, he is studying
复制代码

关键的地方就是把对象o的类型转变为person类型和dog类型