Perl:转到Class:Struct-如何定制新的(例如,ctor)?
各位Perl用户好 我想把几个Perl类从类转移到Class::Struct,因为1)我想在我的类中进行一些类型检查,2)Damian Conway是模块作者之一,他很棒(10年前在Perl会议上看到过他讲话)。但是我想知道,如何在下面的类中解决构造函数的3个问题:Perl:转到Class:Struct-如何定制新的(例如,ctor)?,perl,oop,Perl,Oop,各位Perl用户好 我想把几个Perl类从类转移到Class::Struct,因为1)我想在我的类中进行一些类型检查,2)Damian Conway是模块作者之一,他很棒(10年前在Perl会议上看到过他讲话)。但是我想知道,如何在下面的类中解决构造函数的3个问题: package User; our %Users; our $LobbyPos = 0; # the bit positions for the recently changed fields use constant MOUN
package User;
our %Users;
our $LobbyPos = 0;
# the bit positions for the recently changed fields
use constant MOUNT => 1 << 0;
use constant POOL => 1 << 1;
use constant WHIST1 => 1 << 2;
use constant WHIST2 => 1 << 3;
sub new {
my $pkg = shift;
my $id = shift;
my $auth = shift;
my $user = {
ID => $id,
AUTH => $auth,
LOBBY_POS => (++$LobbyPos % 3),
# NAME, NICK, INFO will be updated later by login()
NAME => $id,
NICK => $id,
INFO => sprintf('id="%s"', $id),
CHAT => [],
KIB => undef,
GAME => undef,
CHILD => undef,
SEAT => 0,
HAND => {},
HAND_STR => '',
NTRIX => 0,
ALLOWED => [],
BID => 0,
BID1 => 0,
CARD => undef,
LAST_READ => time(),
TIMER => undef,
PREV => [],
BACK => 0,
AGREE => 0,
REVEAL => 0,
ONEMORE => 0,
MONEY => 0,
OLD_MONEY => 0,
CHANGED => 0,
MOUNT , [],
POOL , [],
WHIST1 , [],
WHIST2 , [],
STATS_PASS => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
STATS_MISERE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
STATS_LUCK => [0, 0, 0, 0, 0, 0, 0],
STATS_GAME => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
STATS_MATCH => [0, 0, 0, 0],
};
$Users{$id} = $user;
bless($user, $pkg);
}
包用户;
我们的%用户;
我们的$LobbyPos=0;
#最近更改的字段的位位置
使用常量MOUNT=>1$id,
AUTH=>$AUTH,
大厅位置=>(++$LobbyPos%3),
#姓名,NICK,信息稍后将通过登录()更新
NAME=>$id,
尼克=>$id,
INFO=>sprintf('id=“%s”,$id),
聊天=>[],
KIB=>undf,
游戏=>undef,
CHILD=>undf,
座位=>0,
HAND=>{},
手部长度=>“”,
NTRIX=>0,
允许=>[],
出价=>0,
BID1=>0,
卡片=>未定义,
上次读取=>time(),
计时器=>未定义,
上一页=>[],
后退=>0,
同意=>0,
显示=>0,
ONEMORE=>0,
金钱=>0,
旧钱=>0,
已更改=>0,
挂载,[],
池,[],
WHIST1,[],
WHIST2,[],
统计数据通过=>[0,0,0,0,0,0,0,0,0,0,0,0],
统计数据=>[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
统计数据_LUCK=>[0,0,0,0,0,0,0],
游戏统计=>[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
统计数据匹配=>[0,0,0,0],
};
$Users{$id}=$user;
祝福($user,$pkg);
}
问题1:my new接收两个参数-$id和$auth-它们用于启动多个成员。通过查看perldoc类:Struct中的示例3,我可能知道如何解决这个问题
问题2:我使用全局变量$LobbyPos设置另一个成员,并将另一个成员设置为epoch seconds time()
问题3:我的对象中有4个数字成员MOUNT、POOL、WHIST1、WHIST2。可能没有办法继续使用它们,所以-一旦我开始使用Class::Struct
欢迎任何评论(特别是关于问题2的评论-因为我有更多的类使用全局变量在其构造函数中设置成员)
谢谢大家!!
AlexClass::Struct已经十年没有更新了。我很确定,为了向后兼容,它仍然包含在Perl内核中。我真的不建议现在有人将代码移植到Class::Struct 相反,我强烈建议您研究将代码移动到。Moose是Perl面向对象编程的未来