Tie::Array::BoundedIndex - Bounded arrays


  use Tie::Array::BoundedIndex;
  tie @array, "Tie::Array::BoundedIndex", upper => 100;
  tie @array, "Tie::Array::BoundedIndex", lower => 10, upper => 20;

  my @array : Bounded(upper => 20);


Tie::Array::BoundedIndex allows you to create arrays which perform bounds checking upon their indices. A fatal exception will be thrown upon an attempt to go outside the specified bounds.


  tie @array, "Tie::Array::BoundedIndex",
              upper => $upper_limit [, lower => $lower_limit]

A mandatory upper limit is specified with the upper keyword. An optional lower limit is specified with the lower keyword; the default is 0. Each specifies the limit of array indices that may be used. Any attempt to exceed them results in the fatal exception "index <index> out of range [<lower>, <upper>]".

The bounds must be integers greater than or equal to zero with the upper bound greater than or equal to the lower bound.

Use with Attribute::Handlers

Damian Conway's Attribute::Handlers module provides a nice alternative declaration syntax. If you have it installed, then you can declare bounded arrays with:

  my @array : Bounded(upper => 20)


  my @array : Bounded(lower => 10, upper => 20)


Slow. But then, what were you expecting? If you want fast bounded arrays, submit an XS version (with tests) and I'll add it.


Peter Scott,

This module is an expanded version of an example developed in the book ``Perl Medic: Transforming Legacy Code''. See


perltie, Tie::Array.