Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6036859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:59:13+00:00 2026-05-23T05:59:13+00:00

I have a card game – written in Perl and with few objects, like

  • 0

I have a card game – written in Perl and with few objects, like this one:

package PlayingTable;

our (%Games, $Num);

sub new {
        my $pkg   = shift;

        my $game = {
                ID        => ++$Num,
                PHASE     => WAITING,
                KIBITZERS => [],
                PLAYERS   => [],
                INFO      => '',
                RED5      => '',
                TALON     => [],
                TABLE     => {},
                ROUND     => 0,
                PASS_ROUND => 0,
                START     => undef,
                TURN      => undef,
                NPASSED   => 0,
                HOLDER    => undef,
                WHISTER1  => undef,
                WHISTER2  => undef,
                ACTIVE    => undef,
                PASSIVE   => undef,
                SHOW      => undef,
                BEFORE    => undef,
                SUIT1     => undef,
                TRUMP     => undef,
                WINNER    => undef,
        };

        $Games{$Num} = $game;
        bless($game, $pkg);
}

and in the objects I have many hash and list references, which I often have to reset. For example, when a game round is over (one case: when all players passed), I will just call $player->{CARDS} = {}; to reduce the number of cards in the player hand to 0.

My question is if assigning [] and {} is a good enough practice or if it is too expensive, because the perl interpreter will malloc (or whatever it does to allocate memory) those new hash and array objects internally (will it really? or is the interpreter clever enough?).

I’m using (and do not want to upgrade) the stock CentOS perl package:

This is perl, v5.8.8 built for x86_64-linux-thread-multi

with CentOS 5.6 / 64 bit, on a 4GB machine and have max. simultaneous 500 players at evenings. My perl process (a non-forking daemon, polling TCP-sockets) uses right now:

top - 13:50:07 up 13 days,  3:25,  1 user,  load average: 2.64, 3.36, 3.46
Tasks: 179 total,   2 running, 177 sleeping,   0 stopped,   0 zombie
Cpu0  :  3.6%us,  0.3%sy,  0.0%ni, 96.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu1  :  6.0%us,  1.3%sy,  0.0%ni, 92.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu2  : 13.7%us,  0.3%sy,  0.0%ni, 85.3%id,  0.7%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu3  : 42.7%us,  1.7%sy,  0.0%ni, 54.6%id,  0.0%wa,  0.3%hi,  0.7%si,  0.0%st
Mem:   4018280k total,  2831016k used,  1187264k free,   313128k buffers
Swap:  7999472k total,    13612k used,  7985860k free,  1775196k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
13685 afarber   15   0  112m  46m 2704 R 41.8  1.2 176:45.14 pref.pl

Thank you!
Alex

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:59:14+00:00Added an answer on May 23, 2026 at 5:59 am

    The rules of optimization are

    1. Don’t.
    2. Don’t … yet.
    3. Profile before optimizing.

    In another comment, you say you don’t have performance problems, so you’re still at rule 1.

    As for how to clear arrays and hashes, there’s one potential pitfall to avoid. Good practice is to always return copies of objects’ private data. Consider

    #! /usr/bin/env perl
    
    use strict;
    use warnings;
    
    package My::Class;
    
    sub new {
      my($class,@a) = @_;
      bless { a => \@a } => $class;
    }
    
    sub a {
      my($self) = @_;
      $self->{a};
    }
    
    package main;
    
    my $obj = My::Class->new(1, 2, 3);
    
    my $a = $obj->a;
    print "@$a\n";
    
    push @$a, qw/ foo bar /;
    
    my $b = $obj->a;
    print "@$b\n";
    

    Its output is

    1 2 3
    1 2 3 foo bar

    Returning a reference to private data gives a handle for making uncontrolled and likely surprising modifications.

    If your code is sharing references, be sure to clear the same arrays and hashes rather than creating references to new ones. Otherwise, everyone else will go on using the old data without knowing anything has changed. In Perl terms, write

    @{ $game->{PLAYERS} } = ();
    

    rather than

    $game->{PLAYERS} = [];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently wrote a Java playing card game and have all the game logic
I am developing a card game. In this card game the player can set
Ok so I have been trying to fix this for days, and I'm not
This is a derivative question of a question I have posted here: In App
I have a profile card of user that have registration in my forum. Person.update_all({:name
I am trying to generate 5 different random numbers for use in my card
I have an overlay that has a toolbar as a dockedItem and a Ext.List
I have an android application that gets invoked through ADB on a desktop machine.
I work on an audio processing project that needs to do a lot of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.