我做了一个Perl模块和我仍然得到认真处理的Perl如何与对象交易中的访问Perl的对象特征。 这是new
子,我写了创建一个对象,我没有问题,更新内容:如何从同一个包
sub new {
my $class = shift;
my ($self) = {
name => undef
};
bless($self, $class);
return $self;
}
sub get_name {
my $self = shift;
$self->{name} = 'Eve';
return $self->{name};
}
我可以使用对象很好,当我调用模块,并从其他文件访问,但我想使用模块代码中其他区域的对象中的数据。
所以我没有问题,这样做:
my $new_object = new ProgramTest; # ProgramTest being the module/package
my $name = get_name();
但我想用$self
元素中从未被外部脚本访问的“模块内部”的方法。所以我想有这样的事情:
sub get_variables {
return (name); # I don't know how to get the name here
# (I plan to have other variables, too)
}
我可能失去了一些东西明显(我敢肯定,我会踢自己,当我看到的解决方案),所以任何帮助表示赞赏! 我想要这样的模块的其余部分可以使用变量(不改变),因为有条件依赖于它们的值。
参见[如何使Perl模块中的私有函数?(http://stackoverflow.com/questions/451521/how-do-i-make-private-functions-in-a-perl -module)。 – qqx
这实际上是非常有益的,解释了为什么我总是看到'在一些地方_'使用的,而不是别人+1 – dgBP
另一件事学习Perl中的“新东西”是坏的:http://shadow.cat/blog/matt- s-trout /间接但还是致命的/还有很多其他好文章 –