Sure, easy; like the man said, just use tie:
package Tie::FixedArray; use Carp; sub TIEARRAY { my @new = (undef) x $_[1]; bless \@new, $_[0] } sub FETCHSIZE { scalar @{$_[0]} } sub STORE { croak "Element out of bounds$!" if (abs($_[1]) > @{$_[0]}); $_[0]->[$_[1]] = $_[2] } sub FETCH { croak "Element out of bounds$!" if (abs($_[1]) > @{$_[0]}); $_[0]->[$_[1]] } sub EXISTS { exists $_[0]->[$_[1]] } sub CLEAR { @{$_[0]} = (undef) x @{$_[0]} } sub DELETE { delete $_[0]->[$_[1]] } sub SPLICE { my $self = shift; croak "Elements can't be removed from fixed length + arrays$!" if (@_ < 2); if (@_ > 2) { my ($off,$len) = (shift,shift); croak "Element out of bounds$!" if ( (abs($off)+$len) > @{$self}); croak "Elements can't be removed from fixed le +ngth arrays$!" if ($len > @_); croak "Elements can't be removed from fixed le +ngth arrays$!" if ($len < @_); return splice (@{$self},$off,$len,@_); } elsif (($_[0] == 0) && ($_[1] == 0)) {} else { croak "Elements can't be removed from fixed le +ngth arrays$!" } } sub STORESIZE { croak "Elements can't be added/removed to fixed le +ngth arrays$!" } sub PUSH { croak "Elements can't be added to fixed length arr +ays$!" } sub POP { croak "Elements can't be removed from fixed length + arrays$!" } sub SHIFT { croak "Elements can't be removed from fixed length + arrays$!" } sub UNSHIFT { croak "Elements can't be added to fixed length arr +ays$!" } sub EXTEND { croak "Fixed length arrays can't be extended$!" } package main; use strict; use warnings 'all'; # 4 element fixed array my $object = tie my @somearray,'Tie::FixedArray', 4; $somearray[3] = 'hi'; print $somearray[3]; $somearray[5] = 'hello!' # watch it crash and burn!

Update: Accidentally mixed up carp and croak. Fixed.
Update: Fixed splice mistake mentioned by theorbtwo.


In reply to Re: Fixed Array size by jryan
in thread Fixed Array size by Anonymous Monk

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.