开发者

Perl: moving to Class:Struct - how to customize the new (i.e. ctor)?

Hello fellow Perl users,

I'd like to move few Perl classes from a card game to Class::Struct, because 1) I'd like to have some type checking in my classes and 2) Damian Conway is one of the module authors and that man is great (seen him talking at Perl conferences 10y ago). But I wonder, how to solve 3 problems with the constructor in the following cla开发者_如何学Goss:

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);
}

Problem 1: my new receives 2 arguments - $id and $auth - and they're used to initiate several members. I probably know how to solve it by looking at Example 3 in perldoc Class:Struct.

Problem 2: I use global variable $LobbyPos to set another member, and also set one more member to epoch seconds time()

Problem 3: I have 4 numeric members MOUNT, POOL, WHIST1, WHIST2 in my object. There is probably no way to continue using them so - once I start using Class::Struct?

Any comments (esp. on Problem 2 - because I have more classes which use global variables to set members in their constructors) are welcome,

Thank you! Alex


Class::Struct hasn't been updated for ten years. I'm pretty sure that it's only still included in the Perl core for backwards compatibility. I really wouldn't recommend that anyone ports code to Class::Struct these days.

Instead I'd highly recommend that you investigate moving your code to Moose. Moose is the future of OO Programming in Perl.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜