# perldoc > Tie::Array

---
type: CommandReference
command: Tie::Array
mode: perldoc
section: ""
source: perldoc
---

## Quick Reference

- `tie @array, 'Tie::StdArray';` — tie array to behave like a standard Perl array
- `tie @array, 'Tie::NewArray';` — tie array with custom mandatory methods
- `sub TIEARRAY { ... }` — mandatory: associate array with class
- `sub FETCH { ... }` — mandatory: retrieve element at index
- `sub STORE { ... }` — mandatory if elements writable
- `sub FETCHSIZE { ... }` — mandatory: return array size
- `sub STORESIZE { ... }` — mandatory if size can change
- `sub EXISTS { ... }` — mandatory if `exists()` used
- `sub DELETE { ... }` — mandatory if `delete()` used

## Name

Tie::Array - base class for tied arrays

## Synopsis

perl
package Tie::NewArray;
use Tie::Array;
@ISA = ('Tie::Array');

# mandatory methods
sub TIEARRAY { ... }
sub FETCH { ... }
sub FETCHSIZE { ... }
sub STORE { ... }       # mandatory if elements writeable
sub STORESIZE { ... }   # mandatory if elements can be added/deleted
sub EXISTS { ... }      # mandatory if exists() expected to work
sub DELETE { ... }      # mandatory if delete() expected to work

# optional methods (for efficiency)
sub CLEAR { ... }
sub PUSH { ... }
sub POP { ... }
sub SHIFT { ... }
sub UNSHIFT { ... }
sub SPLICE { ... }
sub EXTEND { ... }
sub DESTROY { ... }

package Tie::NewStdArray;
use Tie::Array;
@ISA = ('Tie::StdArray');
# all methods provided by default

package main;
$object = tie @somearray, 'Tie::NewArray';
$object = tie @somearray, 'Tie::StdArray';
$object = tie @somearray, 'Tie::NewStdArray';
## Methods

### Mandatory (must be implemented in custom classes)

- `TIEARRAY classname, LIST` — invoked by `tie @array, classname`; returns an object providing the methods below
- `STORE this, index, value` — store `value` at `index`; if array grows larger, new positions return class's mapping of `undef`
- `FETCH this, index` — retrieve the datum at `index`
- `FETCHSIZE this` — return total number of items (equivalent to `scalar(@array)`)
- `STORESIZE this, count` — set total number of items to `count`; if larger, new positions return `undef`; if smaller, delete entries beyond `count`
- `EXISTS this, key` — verify element at `key` exists; `Tie::Array` stub croaks
- `DELETE this, key` — delete element at `key`; `Tie::Array` stub croaks

### Optional (for efficiency; default implementations in Tie::Array)

- `CLEAR this` — clear all values from the tied array
- `PUSH this, LIST` — append elements of `LIST` to the array
- `POP this` — remove and return last element
- `SHIFT this` — remove and return first element, shifting others down
- `UNSHIFT this, LIST` — insert `LIST` at beginning, moving existing elements up
- `SPLICE this, offset, length, LIST` — perform equivalent of `splice`; `offset` defaults to 0 (negative values count from end), `length` defaults to rest of array, `LIST` may be empty; returns list of original `length` elements at `offset`
- `EXTEND this, count` — informative call that array is likely to grow to `count` entries; can optimize allocation; default does nothing
- `DESTROY this` — normal object destructor; default does nothing

### Tie::StdArray

`Tie::StdArray` inherits from `Tie::Array` and provides efficient default implementations for all methods. Tied arrays using `Tie::StdArray` behave exactly like standard arrays, allowing selective overloading.

## Caveats

- No support for tied `@ISA` (conflict between magic needed for `@ISA` setting and for `tie`).

## See Also

- [perltie](http://localhost/phpMan.php/perldoc/perltie) — detailed description and example code for tied arrays
- `Tie::StdArray` — efficient default implementation
- Other `Tie::*` modules (e.g., `Tie::Hash`, `Tie::Scalar`)