Tie::Array(3pm)

πŸ’… Tie::Array β€” the base class for tied arrays, bestie

okay so real talk, this page is from Perl Programmers Reference Guide and it's literally the blueprint for making your own array-tying classes. it's giving "build your own array, but make it custom." let's get into it πŸŽ€


✨ SYNOPSIS ✨

like, here's the whole vibe in code form β€” copy it exactly, don't @ me:

    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';

see those # mandatory comments? yeah, those are not suggestions, bestie. read them. πŸ”₯


🌸 DESCRIPTION 🌸

so this module hands you the methods you need for array-tying classes. if you wanna know the full guest list of functions required to tie an array to a package, go check out perltie β€” it's the group chat for all things tie.

the basic Tie::Array package is out here providing:

  • stub DESTROY and EXTEND methods that literally do nothing (iconic laziness, honestly)
  • stub DELETE and EXISTS methods that croak() at you if the delete() or exists() builtins ever get called on the tied array β€” so yeah, they yell. loud. 😭
  • actual working implementations of PUSH, POP, SHIFT, UNSHIFT, SPLICE and CLEAR, all written in terms of the basic FETCH, STORE, FETCHSIZE, STORESIZE

meanwhile Tie::StdArray is the overachiever πŸ’… β€” it provides efficient methods for tied arrays that are implemented as blessed references to an "inner" perl array. it inherits from Tie::Array, and it should make your tied arrays behave exactly like standard arrays, which means you can selectively overload methods. no cap, that's the whole point.


for the devs out there who wanna write their own tied arrays, here's the required methods, defined briefly. for the fuller description and example code, head to the perltie section β€” it's the director's cut.

TIEARRAY classname, LIST

this class method gets invoked by the command tie @array, classname. it associates an array instance with the specified class. LIST would represent the additional arguments (along the lines of AnyDBM_File and compatriots) that you need to complete the association. the method should return an object of a class that provides the methods below. 🫢

STORE this, index, value

store datum value into index for the tied array associated with object this. if this makes the array larger, then the class's mapping of undef should be returned for the new positions. ✨

FETCH this, index

retrieve the datum in index for the tied array associated with object this. simple as that, bestie.

FETCHSIZE this

returns the total number of items in the tied array associated with object this. (that's equivalent to scalar(@array), iykyk.)

STORESIZE this, count

sets the total number of items in the tied array associated with object this to be count. if this makes the array larger, then the class's mapping of undef should be returned for the new positions. if the array becomes smaller, then entries beyond count should be deleted. πŸ’€

EXTEND this, count

this one's just an informative call β€” a heads up that the array is likely to grow to have count entries. you can use it to optimize allocation. this method need do nothing. literally nothing. nap time. 😴

EXISTS this, key

verify that the element at index key exists in the tied array this.

heads up: the Tie::Array implementation is a stub that simply croaks. so it will not help you. it will only yell. πŸ™ˆ

DELETE this, key

delete the element at index key from the tied array this.

same situation as above: the Tie::Array implementation is a stub that simply croaks. 😭

CLEAR this

clear (remove, delete, ...) all values from the tied array associated with object this. whatever your heart desires, it's all going.

DESTROY this

normal object destructor method. the end. goodbye. πŸ‘‹

PUSH this, LIST

append elements of LIST to the array. πŸ“₯

POP this

remove the last element of the array and return it. πŸ“€

SHIFT this

remove the first element of the array (shifting other elements down) and return it.

UNSHIFT this, LIST

insert LIST elements at the beginning of the array, moving existing elements up to make room. make space, bestie, they're coming in. πŸ¦‹

SPLICE this, offset, length, LIST

perform the equivalent of splice on the array. here's the deets:

  • offset is optional and defaults to zero β€” negative values count back from the end of the array.
  • length is optional and defaults to rest of the array.
  • LIST may be empty.
  • returns a list of the original length elements at offset.

⚠️ CAVEATS ⚠️

there is no support at present for tied @ISA. and real talk β€” there's a potential conflict between the magic entries needed to notice setting of @ISA, and those needed to implement 'tie'. so like, be aware. πŸ‘‘


πŸ‘‘ AUTHOR πŸ‘‘

Nick Ing-Simmons <nik@tiuk.ti.com>


that's the whole page, bestie β€” go forth and tie some arrays. slay. πŸ’–

🌸 sign-off szn: this was perl v5.42.3, dated 2026-11-03, and the page is Tie::Array(3pm). periodt. πŸŽ€

πŸ“¦ where this page comes from