The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

Name

Tie::Subset::Array - Tie an array to a subset of another array

Synopsis

use Tie::Subset::Array;
my @array = (55,66,77,88,99);
tie my @subset, 'Tie::Subset::Array', \@array, [1,2,3];
print "$subset[1]\n";  # prints "77"
$subset[2]++;          # modifies $array[3]

Description

This class for tied arrays provides a "view" of a subset of an array.

tieing
tie my @subset, 'Tie::Subset::Array', \@array, \@indices;

You must specify which subset of indices from the original array should be part of the new array. (Indices that do not yet exist in the original array may be specified.) The subset (tied array) will be the same size as @indices, and is indexed by the usual 0 to $#subset.

Fetching

If the index is within the bounds of the tied array, the value from the underlying array is returned, otherwise returns nothing (undef).

Storing

If the index is within the bounds of the tied array, the new value will be stored in the underlying array, otherwise the operation is ignored and a warning issued.

exists

Note: The Perl documentation strongly discourages from calling exists on array values.

Will return true only if the index exists in the subset and the corresponding index in the underlying array exists.

Not Supported

Any operations that modify the size of the tied array are not (yet) supported (because it is ambiguous how such operations should affect the underlying array). Attempting to change the tied array's size, including using push, pop, shift, unshift, splice, assigning to the $#array notation, clearing the array, etc. will currently do nothing and cause a warning to be issued, and operations that normally return a value will return nothing.

The above is also true for attempting to delete array elements, which the Perl documentation strongly discourages anyway.

A future version of this module may lift these limitations (if a useful default behavior exists).

See Also

"See Also" in Tie::Subset

Author, Copyright, and License

Copyright (c) 2018-2023 Hauke Daempfling (haukex@zero-g.net).

This library is free software; you can redistribute it and/or modify it under the same terms as Perl 5 itself.

For more information see the Perl Artistic License, which should have been distributed with your copy of Perl. Try the command perldoc perlartistic or see http://perldoc.perl.org/perlartistic.html.