作者windincloud (爱情已死枯如槁灰)
看板Perl
标题[问题] perl OO 中有无 private?
时间Thu Feb 2 16:43:40 2012
我最近想玩perl的OO
不过发现外部存取可以直接存取物件变数
不知道perl是否有啥方是可以避开这样的问题
======== code ========
package Foo
use strict;
use warnings;
sub new
{
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $this =
{
mem1 => "",
mem2 => "",
};
bless $this, $class;
$this->_init(@_);
return $this;
}
......以下略
======= code =======
#!/usr/bin/perl
my Foo $_foo = new();
$_foo->{'mem1'} = "set";
print $_foo->{'mem1'}."\n";
我希望让我的data 封装&不让外界任意存取
不知有人有解吗?
感谢
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.109.183.102
1F:→ AlanSung:try Moose 02/02 17:32