Perl: class transition: Struct - how to configure a new one (i.e. Ctor)?

Greetings to Perl users,

I would like to move several Perl classes from the card game to the :: Struct class, because 1) I would like some type of verification in my classes and 2) Damian Conway is one of the authors of the module and the person is great (saw him talking on Perl conferences 10 years ago). But I am wondering how to solve 3 problems with the constructor in the following class:

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 gets 2 arguments - $ id and $ auth - and they are used to run multiple members. I probably know how to solve it by looking at example 3 in perldoc Class: Struct.

2: $LobbyPos , epoch seconds time()

3: 4 MOUNT, POOL, WHIST1, WHIST2 . , - Class:: Struct?

(, 2 - , ), ,

! Alex

+3
1

:: Struct . , ​​Perl . , Class:: Struct.

Moose. Moose - OO Perl.

+8

All Articles