That's a great question and that’s graceful code from Ruby. We can do similar several different ways in Perl including tying an array, using an array as the object, minimal OO harnessing like Class::Accessor, or even a code ref. This kind of problem strikes me as a good candidate for a code ref. But that's not the topic :P so here are three different ways to do it. All are more verbose than the Ruby; and the least verbose of them (array object) is probably confusingly terse/unusual to most Perl hackers. The last two are FIFO, not FIFI in the example and the third is a hard fixed queue in that its length never changes. It hold undef in “empty” slots. Putting packages in <readmore/> since it’s longish–
use 5.16.2;
package fQ_moonimal {
use Moo;
use overload '""' => sub { join $", @{+shift->items} };
has size =>
is => "ro",
default => sub { 10 },
isa => sub { die "size must be a positive integer"
unless $_[0] > 0 };
has items =>
is => "ro",
init_arg => undef,
default => sub { [] };
sub push {
my ( $self, $item ) = @_;
push @{$self->items}, $item;
pop @{$self->items} if @{$self->items} > $self->size;
}
sub pop {
pop @{+shift->items};
}
};
package fQ_moofancy {
use Moo;
use MooX::HandlesVia;
use MooX::late;
use overload '""' => sub { join $", +shift->all };
# More are avaialbe.
my @methods = qw/ push pop count all shift unshift natatime /;
has size =>
is => "ro",
required => 1,
isa => sub { die "size must be a positive integer"
unless $_[0] and $_[0] > 0 };
has items =>
is => "ro",
init_args => undef,
traits => ["Array"],
handles => { map { $_ => $_ } @methods },
default => sub { [] };
after "push" => sub {
my $self = shift;
$self->shift until $self->size >= $self->count;
};
after "unshift" => sub {
my $self = shift;
$self->pop until $self->size >= $self->count;
};
sub BUILDARGS {
my $class = shift;
my $size = shift;
die "Only one argument allowed: size" if @_;
{ size => $size };
}
};
package fQ_FIXED {
use overload '""' => sub { join $", map $_ || "_", @{+shift} };
sub new { bless [ (undef) x $_[1] ], __PACKAGE__ }
sub push {
my $self = shift;
push @{$self}, shift;
shift @{$self};
}
sub pop {
my $self = shift;
unshift @{$self}, undef;
pop @{$self};
}
};
Some sample code with them and output.
eval { fQ_moofancy->new } or say $@;
my $foo = fQ_moonimal->new(size => 3);
my $fooncy = fQ_moofancy->new(3);
my $fixed = fQ_FIXED->new(3);
for ( 1 .. 5 )
{
say " foo -> $foo";
say "fooncy -> $fooncy";
say " fixed -> $fixed";
$foo->push($_);
$fooncy->push($_);
$fixed->push($_);
}
__END__
isa check for "size" failed: size must be a positive integer at…
foo ->
fooncy ->
fixed -> _ _ _
foo -> 1
fooncy -> 1
fixed -> _ _ 1
foo -> 1 2
fooncy -> 1 2
fixed -> _ 1 2
foo -> 1 2 3
fooncy -> 1 2 3
fixed -> 1 2 3
foo -> 1 2 3
fooncy -> 2 3 4
fixed -> 2 3 4
I have no doubt there are other, likely cleaner approaches. I was winging it, first drafts, all that sort of caveat.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.