Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1003533
  • 博文数量: 150
  • 博客积分: 3017
  • 博客等级: 少校
  • 技术积分: 3829
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-19 14:40
个人简介

Now in Baidu WISE team

文章分类

全部博文(150)

文章存档

2014年(8)

2013年(31)

2012年(111)

分类: Python/Ruby

2012-02-06 15:05:01

When creating module in Perl, use anonymous hash table is recommend.
See the different ways of implementing a module

package A;
require Exporter;
@ISA = qw(Exporter);
@Exporter = qw(test);

my $field;
sub new{
      shift;
      my $this = {};
      $field = shift;
      bless $this;
      return $this;
}
sub test{
      print $field;
}
1;
======================================
package A;
require Exporter;
@ISA = qw(Exporter);
@Exporter = qw(test);


sub new{
      shift;
      my $this = {};
      $this->{field} = shift;
      bless $this;
      return $this;
}
sub test{
      my $this = shift;
      print $this->{field};
}
1;
========================================
In the first implementation, if you create more than 1 objects of A, you may found that all these object's 'field' value is the same --- the value is which you created with in last object.
That is because in perl, only blessed fields are packaged into the object. 'field' has not been blessed.
When you creating many A objects, there is only one instance of 'field' in memory.  So when you called the objects you created,  you will find every objects' field have only 1 value. --------- All these objects share 1 'field.




阅读(891) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:'caller' in perl

给主人留下些什么吧!~~