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}; } }; #### 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