Make your own array. This one specifically croaks when the size limitation would be exceeded. You can change it so that pushing to it would remove elements from the beginning if needed.
package Tie::Array::MaxLen; use Carp; sub TIEARRAY { my ($class, $size) = @_; croak "size must be positive" if $size <= 0; return bless [ $size, [] ], $class; } sub check ($$) { my ($size, $idx) = @_; croak "index $idx out of bounds" if $idx >= $size or ($idx < 0 and -$idx > $size); } sub FETCH { my ($self, $idx) = @_; check($self->[0], $idx); return $self->[1][$idx]; } sub STORE { my ($self, $idx, $value) = @_; check($self->[0], $idx); return $self->[1][$idx] = $value; } sub FETCHSIZE { my ($self) = @_; return scalar @{ $self->[1] }; } sub STORESIZE { # maybe you want this, maybe not my ($self, $size) = @_; croak "size must be positive" if $size <= 0; $self->[0] = $size; $#{ $self->[1] } = $size-1; } sub CLEAR { my ($self) = @_; $self->[1] = []; } sub POP { my ($self) = @_; return pop @{ $self->[1] }; } sub PUSH { my $self = shift; check($self->[0], @_ + @{ $self->[1] }); push @{ $self->[1] }, @_; } sub SHIFT { my ($self) = @_; return shift @{ $self->[1] }; } sub UNSHIFT { my $self = shift; check($self->[0], @_ + @{ $self->[1] }); unshift @{ $self->[1] }, @_; } sub EXISTS { # remove if not 5.6+ my ($self, $idx) = @_; check($self->[0], $idx); exists $self->[1][$idx]; } sub DEFINED { my ($self, $idx) = @_; check($self->[0], $idx); defined $self->[1][$idx]; } sub SPLICE { my $self = shift; my $off = @_ ? shift : 0; $off += $self->[0] if $off < 0; my $len = @_ ? shift : $self->[0] - $off; $len += $self->[0] if $len < 0; check($self->[0], $off + $len); splice(@{ $self->[1] }, $off, $len, @_); }
As "complete" as that looks, I've not tested it. It seems like it should work though. Sample use:
tie my(@five), 'Tie::Array::MaxLen', 5; push @five, 10, 20, 30, 40; push @five, 50; push @five, 60; # QUACK


japhy -- Perl and Regex Hacker

In reply to Re: Limiting array size? by japhy
in thread Limiting array size? by ned

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.