你好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 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);
}
Run Code Online (Sandbox Code Playgroud)
问题1:我的新收到2个参数 - $ id和$ auth - 它们用于启动几个成员.我可能知道如何通过查看perldoc类:Struct中的示例3来解决它.
问题2:我使用全局变量$ LobbyPos来设置另一个成员,并且还将一个成员设置为epoch秒time()
问题3:我的对象中有4个数字成员MOUNT,POOL,WHIST1,WHIST2.可能没有办法继续使用它们 - 一旦我开始使用Class :: Struct?
任何评论(尤其是问题2 - 因为我有更多的类使用全局变量来在其构造函数中设置成员)是受欢迎的,
谢谢!亚历克斯