Perl 5 version 24.3 documentation

Tie::Array

NAME

Tie::Array - base class for tied arrays

SYNOPSIS

  1. package Tie::NewArray;
  2. use Tie::Array;
  3. @ISA = ('Tie::Array');
  4. # mandatory methods
  5. sub TIEARRAY { ... }
  6. sub FETCH { ... }
  7. sub FETCHSIZE { ... }
  8. sub STORE { ... } # mandatory if elements writeable
  9. sub STORESIZE { ... } # mandatory if elements can be added/deleted
  10. sub EXISTS { ... } # mandatory if exists() expected to work
  11. sub DELETE { ... } # mandatory if delete() expected to work
  12. # optional methods - for efficiency
  13. sub CLEAR { ... }
  14. sub PUSH { ... }
  15. sub POP { ... }
  16. sub SHIFT { ... }
  17. sub UNSHIFT { ... }
  18. sub SPLICE { ... }
  19. sub EXTEND { ... }
  20. sub DESTROY { ... }
  21. package Tie::NewStdArray;
  22. use Tie::Array;
  23. @ISA = ('Tie::StdArray');
  24. # all methods provided by default
  25. package main;
  26. $object = tie @somearray,'Tie::NewArray';
  27. $object = tie @somearray,'Tie::StdArray';
  28. $object = tie @somearray,'Tie::NewStdArray';

DESCRIPTION

This module provides methods for array-tying classes. See perltie for a list of the functions required in order to tie an array to a package. The basic Tie::Array package provides stub DESTROY, and EXTEND methods that do nothing, stub DELETE and EXISTS methods that croak() if the delete() or exists() builtins are ever called on the tied array, and implementations of PUSH , POP , SHIFT , UNSHIFT , SPLICE and CLEAR in terms of basic FETCH , STORE , FETCHSIZE , STORESIZE .

The Tie::StdArray package provides efficient methods required for tied arrays which are implemented as blessed references to an "inner" perl array. It inherits from Tie::Array, and should cause tied arrays to behave exactly like standard arrays, allowing for selective overloading of methods.

For developers wishing to write their own tied arrays, the required methods are briefly defined below. See the perltie section for more detailed descriptive, as well as example code:

  • TIEARRAY classname, LIST

    The class method is invoked by the command tie @array, classname . Associates an array instance with the specified class. LIST would represent additional arguments (along the lines of AnyDBM_File and compatriots) needed to complete the association. The method should return an object of a class which 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 class's mapping of undef should be returned for new positions.

  • FETCH this, index

    Retrieve the datum in index for the tied array associated with object this.

  • FETCHSIZE this

    Returns the total number of items in the tied array associated with object this. (Equivalent to scalar(@array)).

  • 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 class's mapping of undef should be returned for new positions. If the array becomes smaller then entries beyond count should be deleted.

  • EXTEND this, count

    Informative call that array is likely to grow to have count entries. Can be used to optimize allocation. This method need do nothing.

  • EXISTS this, key

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

    The Tie::Array implementation is a stub that simply croaks.

  • DELETE this, key

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

    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.

  • DESTROY this

    Normal object destructor method.

  • PUSH this, LIST

    Append elements of LIST to the array.

  • POP this

    Remove 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.

  • SPLICE this, offset, length, LIST

    Perform the equivalent of splice on the array.

    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. There is a potential conflict between magic entries needed to notice setting of @ISA, and those needed to implement 'tie'.

AUTHOR

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