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
DESTROYandEXTENDmethods that literally do nothing (iconic laziness, honestly) - stub
DELETEandEXISTSmethods thatcroak()at you if thedelete()orexists()builtins ever get called on the tied array β so yeah, they yell. loud. π - actual working implementations of
PUSH,POP,SHIFT,UNSHIFT,SPLICEandCLEAR, all written in terms of the basicFETCH,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.
LISTmay 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
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms