#!/usr/bin/perl use strict; use warnings; use Data::Dumper; package Message; use overload '""' => \&to_s; sub new { my $class = shift; my $msg = shift; my $t = time(); my $self = { message => $msg, t => $t }; return bless $self, $class; } sub to_s { my $self = shift; return $self->{'message'}; } 1; package main; my @msg_queue = (); push @msg_queue, new Message("Hello"); sleep(1); push @msg_queue, new Message("World"); print Dumper [grep { time - $_->{'t'} <= 2 } @msg_queue]; sleep(2); print Dumper [grep { time - $_->{'t'} <= 2 } @msg_queue];