in reply to How would you implement a FIFO non-blocking queue?
The following may help. Note that this hasn't been tested beyond the sample code given so the handling for data in addition to the ID had not been checked.
use warnings; use strict; package Fifo; sub new { my $self = []; return bless $self; } sub push { my $self = shift; my $id = shift; if (@_) { push @$self, [$id, \@_]; } else { push @$self, $id; } return scalar @$self; } sub pull { my $self = shift; return shift @$self; } sub remove { my $self = shift; my $id = shift; for (reverse 0..$#$self) { my $item = $self->[$_]; my $itemId = ref $item ? $item->[0] : $item; splice @$self, $_, 1 if $itemId eq $id } } 1; package main; my $fifo = Fifo->new; $fifo->push ('First person'); $fifo->push ('Second person'); $fifo->push ('Third person'); $fifo->remove ('Second person'); print "$_\n" while defined ($_ = $fifo->pull);
Prints:
First person Third person
|
|---|