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.


In reply to Re^6: The future of Perl? by Your Mother
in thread The future of Perl? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.