Introduction
------------
Have you ever wanted to...
- have a hash that is automatically stored on disk?
- have a hash with a fixed set of keys?
- have a scalar that can get and set the priority of a process?
- have a hash which maintains insertion order?
- have a fixed array?
- etc?
Basically, have you ever wanted to have the interface of a standard Perl type
but with modified characteristics?
If so, then what you want is a tied object.
Tied Objects
------------
What we are talking about here is using Perl variable access to actually
access an object which you (or someone in CPAN) have created that responds to
the predefined methods of the variable.
In fact, the Perl distribution comes with implementations of tie classes which
completely mirror the functions of Perl variables. So, all you have to do is
to inherit one of these and override the methods that you are interested in.
The class for the hash is Tie::StdHash.
There are many tie classes to in CPAN, and hopefully, Mark will show us
several good ones. However, you can also create your own.
Using Tied Objects
------------------
The tie function:
- Two mandatory params
- variable being tied
- name of class being tied to
- Optional params are passed to the class' "new" method
Example:
# Prints this without tie:
# apples
# oranges
#
# Prints this with tie:
# oranges
# apples
use Tie::IxHash;
tie %menu, 'Tie::IxHash';
$menu{oranges} = 1;
$menu{apples} = 2;
foreach (keys %menu)
{
print;
print "\n";
}
To access object that the variable is tied to, do either:
- Save the reference returned by the tie function:
$ref = tie %menu, 'Tie::IxHash';
$ref->SortByKey;
- Retrieve a reference to the object using the tied function:
$ref = tied(%menu);
$ref->SortByValue;
To return a variable to its pre-tied state, use the untie function:
untie %menu;
Methods for a tie'd Scalar
--------------------------
TIESCALAR classname, LIST
The "new" method. Called when tie function called. LIST is list of
optional parameters from the tie function. Should return a reference to
new object.
FETCH this
Called when value is read. Should return value (or undef).
STORE this, value
Called when a value is written.
UNTIE this
Called when the untie function is called.
DESTROY this
Called when the tied variable goes out of scope.
Methods for a tie'd Hash
---------------------------------------
TIEHASH classname, LIST
Same as TIESCALAR.
FETCH this, key
Called when a value is read using a hash key. Should return value (or
undef).
STORE this, key, value
Called when a value is written using a hash key.
DELETE this, key
Called when a key-value pair is deleted from the hash with the delete
function. Should return value being deleted.
CLEAR this
Called when user clears the whole hash, typically by assigning the empty
list.
EXISTS this, key
Called when existence of a key is checked with the exists function. Should
return true or false.
FIRSTKEY this
Called when one of the hash iterator functions (each or keys) is called
for the first time. Should return 1st key.
NEXTKEY this, lastkey
Called when one of the hash iterator functions is called. Argument lastkey
is the key from the last iterator call. Should return name of next key, or
undef if no more keys.
UNTIE this
Same as scalar
DESTROY this
Same as scalar
Methods for a tie'd Array
-------------------------
TIEARRAY classname, LIST
Same as TIESCALAR.
FETCH this, index
Called when an element's value is read. Should return value (or undef).
STORE this, index, value
Called when a new or existing element is written.
FETCHSIZE this
Called when number of elements in array is retrieved, e.g. @#array.
Should return number of elements.
STORESIZE this, count
Called when number of elements in array is changed. If count is larger
than current count, new elements with the object's equivalent value of
undef should be created. If count is smaller, then elements 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.
EXISTS this, index
Same as hash, except index vs. key
DELETE this, index
Same as hash, except index vs. key
CLEAR this
Same as hash, except array vs. hash
PUSH this, LIST
Appends elements of list to array.
POP this
Remove and return last element.
SHIFT this
Remove and return first element.
UNSHIFT this, LIST
Inserts elements of list at the beginning of the array.
SPLICE this, offset, length, LIST
Performs 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 the rest of the array. LIST may be
empty. Returns a list of the original length elements at offset.
UNTIE this
Same as scalar
DESTROY this
Same as scalar
Methods for a tie'd File Handle
-------------------------------
TIEHANDLE classname, LIST
Same as TIESCALAR.
WRITE this, LIST
Called in response to syswrite.
PRINT this, LIST
Called in response to print.
PRINTF this, LIST
Called in response to printf.
READ this, LIST
Called in response to read or sysread.
READLINE this
Called in response to <HANDLE>. Should return undef when no more data.
GETC this
Called in response to getc.
CLOSE this
Called in response to close.
UNTIE this
Same as scalar
DESTROY this
Same as scalar
References
----------
[1] "Changing Hash Behaviour with tie", Dave Cross, September 4, 2001,
http://www.perl.com/pub/a/2001/09/04/tiedhash.html.
[2] perldoc perltie or http://www.perldoc.com/perl5.6/pod/perltie.html, Tom
Christiansen, et. al.