package FixedSizeQueue { sub new { my( $class, $size ) = @_; bless { max_size => $size, items => [], }, $class; } sub pop { my $self = shift; pop @{ $self->{ items } }; } sub push { my( $self, $item ) = @_; shift @{ $self->{ items } } if @{ $self->{ items } } == $self->{ max_size }; push @{ $self->{ items } }, $item; } 1; }; __END__ [0] Perl> $Q = FixedSizeQueue->new( 3 );; [0] Perl> $Q->push( 2 );; [0] Perl> $Q->push( 3 );; [0] Perl> $Q->push( 5 );; [0] Perl> pp $Q;; bless({ items => [2, 3, 5], max_size => 3 }, "FixedSizeQueue") [0] Perl> $Q->push( 7 );; [0] Perl> pp $Q;; bless({ items => [3, 5, 7], max_size => 3 }, "FixedSizeQueue") [0] Perl> pp %FixedSizeQueue::;; ( "new", *FixedSizeQueue::new, "push", *FixedSizeQueue::push, "import", *FixedSizeQueue::import, "pop", *FixedSizeQueue::pop, )