package Chat::Pool; # 16 should suffice. use constant POOL_SIZE => 16; sub new { bless [ # $self->pool: Arrayref of ["#fg","#bg"]-pairs. # Fixed size. [ $_[0]->precalc_colors(POOL_SIZE) ], # $self->col->[$id] contains the ["#fg","#bg"]-pair of a user. [], ] => shift } sub tick { my ($self, $id, $time) = @_; # If we haven't allocated a color for $id... unless(defined $self->col->[$id]) { # Take one from the pool (pop), assign in to $id, and unshift it. # All in one line :) unshift @{ $self->pool }, $self->col->[$id] = pop @{ $self->pool }; } } # Return the allocated color for $_[1]. sub color {@{ $_[0]->col->[$_[1]] }} # Precalculate the pool. sub precalc_colors { my ($self, $num) = @_; local $_; return map {[ $self->calc_color($_, $num) ]} 0..$num-1; } # calc_color as in OP sub pool : lvalue { $_[0]->[0] } sub col : lvalue { $_[0]->[1] }