PERLTIE

NAME

perltie - how to hide an object class in a simple variable

SYNOPSIS

tie VARIABLE, CLASSNAME, LIST
$object = tied VARIABLE
untie VARIABLE

DESCRIPTION

Prior to release 5.0 of Perl, a programmer could use dbmopen()
to connect an on-disk database in the standard Unix dbm(3x)
format magically to a %HASH in their program. However, their Perl was either
built with one particular dbm library or another, but not both, and
you couldn't extend this mechanism to other packages or types of variables.

Now you can.

The tie() function binds a variable to a class (package) that will provide
the implementation for access methods for that variable. Once this magic
has been performed, accessing a tied variable automatically triggers
method calls in the proper class. The complexity of the class is
hidden behind magic methods calls. The method names are in ALL CAPS,
which is a convention that Perl uses to indicate that they're called
implicitly rather than explicitly---just like the BEGIN() and END()
functions.

In the tie() call, "VARIABLE" is the name of the variable to be
enchanted. "CLASSNAME" is the name of a class implementing objects of
the correct type. Any additional arguments in the "LIST" are passed to
the appropriate constructor method for that class---meaning TIESCALAR(),
TIEARRAY(), TIEHASH(), or TIEHANDLE(). (Typically these are arguments
such as might be passed to the dbminit() function of C.) The object
returned by the ``new'' method is also returned by the tie() function,
which would be useful if you wanted to access other methods in
"CLASSNAME". (You don't actually have to return a reference to a right
``type'' (e.g., HASH or "CLASSNAME") so long as it's a properly blessed
object.) You can also retrieve a reference to the underlying object
using the tied() function.

Unlike dbmopen(), the tie() function will not "use" or "require" a module
for you---you need to do that explicitly yourself.

Tying Scalars

A class implementing a tied scalar should define the following methods:
TIESCALAR, FETCH, STORE, and possibly UNTIE and/or DESTROY.

Let's look at each in turn, using as an example a tie class for
scalars that allows the user to do something like:

tie $his_speed, 'Nice', getppid();
tie $my_speed, 'Nice', $$;

And now whenever either of those variables is accessed, its current
system priority is retrieved and returned. If those variables are set,
then the process's priority is changed!

We'll use Jarkko Hietaniemi <jhi@iki.fi>'s BSD::Resource class (not
included) to access the PRIO_PROCESS, PRIO_MIN, and PRIO_MAX constants
from your system, as well as the getpriority() and setpriority() system
calls. Here's the preamble of the class.

This tie class has chosen to return an error rather than raising an
exception if its constructor should fail. While this is how dbmopen() works,
other classes may well not wish to be so forgiving. It checks the global
variable $^W to see whether to emit a bit of noise anyway.

FETCH this

This method will be triggered every time the tied variable is accessed
(read). It takes no arguments beyond its self reference, which is the
object representing the scalar we're dealing with. Because in this case
we're using just a SCALAR ref for the tied scalar object, a simple $$self
allows the method to get at the real value stored there. In our example
below, that real value is the process ID to which we've tied our variable.

This time we've decided to blow up (raise an exception) if the renice
fails---there's no place for us to return an error otherwise, and it's
probably the right thing to do.

STORE this, value

This method will be triggered every time the tied variable is set
(assigned). Beyond its self reference, it also expects one (and only one)
argument: the new value the user is trying to assign. Don't worry about
returning a value from STORE; the semantic of assignment returning the
assigned value is implemented with FETCH.

This method will be triggered when the "untie" occurs. This can be useful
if the class needs to know when no further calls will be made. (Except DESTROY
of course.) See "The "untie" Gotcha" below for more details.

DESTROY this

This method will be triggered when the tied variable needs to be destructed.
As with other object classes, such a method is seldom necessary, because Perl
deallocates its moribund object's memory for you automatically---this isn't
C++, you know. We'll use a DESTROY method here for debugging purposes only.

That's about all there is to it. Actually, it's more than all there
is to it, because we've done a few nice things here for the sake
of completeness, robustness, and general aesthetics. Simpler
TIESCALAR classes are certainly possible.

Tying Arrays

FETCHSIZE and STORESIZE are used to provide $#array and
equivalent "scalar(@array)" access.

The methods POP, PUSH, SHIFT, UNSHIFT, SPLICE, DELETE, and EXISTS are
required if the perl operator with the corresponding (but lowercase) name
is to operate on the tied array. The Tie::Array class can be used as a
base class to implement the first five of these in terms of the basic
methods above. The default implementations of DELETE and EXISTS in
Tie::Array simply "croak".

In addition EXTEND will be called when perl would have pre-extended
allocation in a real array.

For this discussion, we'll implement an array whose elements are a fixed
size at creation. If you try to create an element larger than the fixed
size, you'll take an exception. For example:

This is the constructor for the class. That means it is expected to
return a blessed reference through which the new array (probably an
anonymous ARRAY ref) will be accessed.

In our example, just to show you that you don't really have to return an
ARRAY reference, we'll choose a HASH reference to represent our object.
A HASH works out well as a generic record type: the "{ELEMSIZE}" field will
store the maximum element size allowed, and the "{ARRAY}" field will hold the
true ARRAY ref. If someone outside the class tries to dereference the
object returned (doubtless thinking it an ARRAY ref), they'll blow up.
This just goes to show you that you should respect an object's privacy.

If a negative array index is used to read from an array, the index
will be translated to a positive one internally by calling FETCHSIZE
before being passed to FETCH. You may disable this feature by
assigning a true value to the variable $NEGATIVE_INDICES in the
tied array class.

As you may have noticed, the name of the FETCH method (et al.) is the same
for all accesses, even though the constructors differ in names (TIESCALAR
vs TIEARRAY). While in theory you could have the same class servicing
several tied types, in practice this becomes cumbersome, and it's easiest
to keep them at simply one tie type per class.

STORE this, index, value

This method will be triggered every time an element in the tied array is set
(written). It takes two arguments beyond its self reference: the index at
which we're trying to store something and the value we're trying to put
there.

In our example, "undef" is really "$self->{ELEMSIZE}" number of
spaces so we have a little more work to do here:

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

sub FETCHSIZE {
my $self = shift;
return scalar @{$self->{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.

In our example, 'undef' is really an element containing
"$self->{ELEMSIZE}" number of spaces. Observe:

Will be called when "untie" happens. (See "The "untie" Gotcha" below.)

DESTROY this

This method will be triggered when the tied variable needs to be destructed.
As with the scalar tie class, this is almost never needed in a
language that does its own garbage collection, so this time we'll
just leave it out.

Tying Hashes

Hashes were the first Perl data type to be tied (see dbmopen()). A class
implementing a tied hash should define the following methods: TIEHASH is
the constructor. FETCH and STORE access the key and value pairs. EXISTS
reports whether a key is present in the hash, and DELETE deletes one.
CLEAR empties the hash by deleting all the key and value pairs. FIRSTKEY
and NEXTKEY implement the keys() and each() functions to iterate over all
the keys. SCALAR is triggered when the tied hash is evaluated in scalar
context. UNTIE is called when "untie" happens, and DESTROY is called when
the tied variable is garbage collected.

If this seems like a lot, then feel free to inherit from merely the
standard Tie::StdHash module for most of your methods, redefining only the
interesting ones. See Tie::Hash for details.

Remember that Perl distinguishes between a key not existing in the hash,
and the key existing in the hash but having a corresponding value of
"undef". The two possibilities can be tested with the "exists()" and
"defined()" functions.

Here's an example of a somewhat interesting tied hash class: it gives you
a hash representing a particular user's dot files. You index into the hash
with the name of the file (minus the dot) and you get back that dot file's
contents. For example:

For our example, we want to be able to emit debugging info to help in tracing
during development. We keep also one convenience function around
internally to help print out warnings; whowasi() returns the function name
that calls it.

Here are the methods for the DotFiles tied hash.

TIEHASH classname, LIST

This is the constructor for the class. That means it is expected to
return a blessed reference through which the new object (probably but not
necessarily an anonymous hash) will be accessed.

It's probably worth mentioning that if you're going to filetest the
return values out of a readdir, you'd better prepend the directory
in question. Otherwise, because we didn't chdir() there, it would
have been testing the wrong file.

FETCH this, key

This method will be triggered every time an element in the tied hash is
accessed (read). It takes one argument beyond its self reference: the key
whose value we're trying to fetch.

It was easy to write by having it call the Unix cat(1) command, but it
would probably be more portable to open the file manually (and somewhat
more efficient). Of course, because dot files are a Unixy concept, we're
not that concerned.

STORE this, key, value

This method will be triggered every time an element in the tied hash is set
(written). It takes two arguments beyond its self reference: the index at
which we're trying to store something, and the value we're trying to put
there.

Here in our DotFiles example, we'll be careful not to let
them try to overwrite the file unless they've called the clobber()
method on the original object reference returned by tie().

The value returned by DELETE becomes the return value of the call
to delete(). If you want to emulate the normal behavior of delete(),
you should return whatever FETCH would have returned for this key.
In this example, we have chosen instead to return a value which tells
the caller whether the file was successfully deleted.

CLEAR this

This method is triggered when the whole hash is to be cleared, usually by
assigning the empty list to it.

In our example, that would remove all the user's dot files! It's such a
dangerous thing that they'll have to set CLOBBER to something higher than
1 to make it happen.

FIRSTKEY is always called in scalar context and it should just
return the first key. values(), and each() in list context,
will call FETCH for the returned keys.

NEXTKEY this, lastkey

This method gets triggered during a keys(), values(), or each() iteration. It has a
second argument which is the last key that had been accessed. This is
useful if you're caring about ordering or calling the iterator from more
than one sequence, or not really storing things in a hash anywhere.

NEXTKEY is always called in scalar context and it should just
return the next key. values(), and each() in list context,
will call FETCH for the returned keys.

For our example, we're using a real hash so we'll do just the simple
thing, but we'll have to go through the LIST field indirectly.

This is called when the hash is evaluated in scalar context. In order
to mimic the behaviour of untied hashes, this method should return a
false value when the tied hash is considered empty. If this method does
not exist, perl will make some educated guesses and return true when
the hash is inside an iteration. If this isn't the case, FIRSTKEY is
called, and the result will be a false value if FIRSTKEY returns the empty
list, true otherwise.

However, you should not blindly rely on perl always doing the right
thing. Particularly, perl will mistakenly return true when you clear the
hash by repeatedly calling DELETE until it is empty. You are therefore
advised to supply your own SCALAR method when you want to be absolutely
sure that your hash behaves nicely in scalar context.

In our example we can just call "scalar" on the underlying hash
referenced by "$self->{LIST}":

This method is triggered when a tied hash is about to go out of
scope. You don't really need it unless you're trying to add debugging
or have auxiliary state to clean up. Here's a very simple function:

sub DESTROY {
carp &whowasi if $DEBUG;
}

Note that functions such as keys() and values() may return huge lists
when used on large objects, like DBM files. You may prefer to use the
each() function to iterate over such. Example:

Tying FileHandles

This is partially implemented now.

A class implementing a tied filehandle should define the following
methods: TIEHANDLE, at least one of PRINT, PRINTF, WRITE, READLINE, GETC,
READ, and possibly CLOSE, UNTIE and DESTROY. The class can also provide: BINMODE,
OPEN, EOF, FILENO, SEEK, TELL - if the corresponding perl operators are
used on the handle.

When STDERR is tied, its PRINT method will be called to issue warnings
and error messages. This feature is temporarily disabled during the call,
which means you can use "warn()" inside PRINT without starting a recursive
loop. And just like "__WARN__" and "__DIE__" handlers, STDERR's PRINT
method may be called to report parser errors, so the caveats mentioned under
``%SIG'' in perlvar apply.

All of this is especially useful when perl is embedded in some other
program, where output to STDOUT and STDERR may have to be redirected
in some special way. See nvi and the Apache module for examples.

When tying a handle, the first argument to "tie" should begin with an
asterisk. So, if you are tying STDOUT, use *STDOUT. If you have
assigned it to a scalar variable, say $handle, use *$handle.
"tie $handle" ties the scalar variable $handle, not the handle inside
it.

In our example we're going to create a shouting handle.

package Shout;

TIEHANDLE classname, LIST

This is the constructor for the class. That means it is expected to
return a blessed reference of some sort. The reference can be used to
hold some internal information.

sub TIEHANDLE { print "<shout>\n"; my $i; bless \$i, shift }

WRITE this, LIST

This method will be called when the handle is written to via the
"syswrite" function.

This method will be triggered every time the tied handle is printed to
with the "print()" or "say()" functions. Beyond its self reference
it also expects the list that was passed to the print function.

sub PRINT { $r = shift; $$r++; print join($,,map(uc($_),@_)),$\ }

"say()" acts just like "print()" except $\ will be localized to "\n" so
you need do nothing special to handle "say()" in "PRINT()".

PRINTF this, LIST

This method will be triggered every time the tied handle is printed to
with the "printf()" function.
Beyond its self reference it also expects the format and list that was
passed to the printf function.

sub PRINTF {
shift;
my $fmt = shift;
print sprintf($fmt, @_);
}

READ this, LIST

This method will be called when the handle is read from via the "read"
or "sysread" functions.

This method is called when the handle is read via "<HANDLE>"
or "readline HANDLE".

As per "readline", in scalar context it should return
the next line, or "undef" for no more data. In list context it should
return all remaining lines, or an empty list for no more data. The strings
returned should include the input record separator $/ (see perlvar),
unless it is "undef" (which means ``slurp'' mode).

Starting with Perl 5.12, an additional integer parameter will be passed. It
will be zero if "eof" is called without parameter; 1 if "eof" is given
a filehandle as a parameter, e.g. "eof(FH)"; and 2 in the very special
case that the tied filehandle is "ARGV" and "eof" is called with an empty
parameter list, e.g. "eof()".

sub EOF { not length $stringbuf }

CLOSE this

This method will be called when the handle is closed via the "close"
function.

sub CLOSE { print "CLOSE called.\n" }

UNTIE this

As with the other types of ties, this method will be called when "untie" happens.
It may be appropriate to ``auto CLOSE'' when this occurs. See
"The "untie" Gotcha" below.

DESTROY this

As with the other types of ties, this method will be called when the
tied handle is about to be destroyed. This is useful for debugging and
possibly cleaning up.

So far so good. Those of you who have been paying attention will have
spotted that the tied object hasn't been used so far. So lets add an
extra method to the Remember class to allow comments to be included in
the file; say, something like this:

When a variable is tied, it is associated with the object which is the
return value of the TIESCALAR, TIEARRAY, or TIEHASH function. This
object normally has only one reference, namely, the implicit reference
from the tied variable. When untie() is called, that reference is
destroyed. Then, as in the first example above, the object's
destructor (DESTROY) is called, which is normal for objects that have
no more valid references; and thus the file is closed.

In the second example, however, we have stored another reference to
the tied object in $x. That means that when untie() gets called
there will still be a valid reference to the object in existence, so
the destructor is not called at that time, and thus the file is not
closed. The reason there is no output is because the file buffers
have not been flushed to disk.

Now that you know what the problem is, what can you do to avoid it?
Prior to the introduction of the optional UNTIE method the only way
was the good old "-w" flag. Which will spot any instances where you call
untie() and there are still valid references to the tied object. If
the second script above this near the top "use warnings 'untie'"
or was run with the "-w" flag, Perl prints this
warning message:

untie attempted while 1 inner references still exist

To get the script to work properly and silence the warning make sure
there are no valid references to the tied object beforeuntie() is
called:

undef $x;
untie $fred;

Now that UNTIE exists the class designer can decide which parts of the
class functionality are really associated with "untie" and which with
the object being destroyed. What makes sense for a given class depends
on whether the inner references are being kept so that non-tie-related
methods can be called on the object. But in most cases it probably makes
sense to move the functionality that would have been in DESTROY to the UNTIE
method.

If the UNTIE method exists then the warning above does not occur. Instead the
UNTIE method is passed the count of ``extra'' references and can issue its own
warning if appropriate. e.g. to replicate the no UNTIE case this method can
be used:

SEE ALSO

See DB_File or Config for some interesting tie() implementations.
A good starting point for many tie() implementations is with one of the
modules Tie::Scalar, Tie::Array, Tie::Hash, or Tie::Handle.

BUGS

The bucket usage information provided by "scalar(%hash)" is not
available. What this means is that using %tied_hash in boolean
context doesn't work right (currently this always tests false,
regardless of whether the hash is empty or hash elements).

Localizing tied arrays or hashes does not work. After exiting the
scope the arrays or the hashes are not restored.

Counting the number of entries in a hash via "scalar(keys(%hash))"
or "scalar(values(%hash)") is inefficient since it needs to iterate
through all the entries with FIRSTKEY/NEXTKEY.

You cannot easily tie a multilevel data structure (such as a hash of
hashes) to a dbm file. The first problem is that all but GDBM and
Berkeley DB have size limitations, but beyond that, you also have problems
with how references are to be represented on disk. One
module that does attempt to address this need is DBM::Deep. Check your
nearest CPAN site as described in perlmodlib for source code. Note
that despite its name, DBM::Deep does not use dbm. Another earlier attempt
at solving the problem is MLDBM, which is also available on the CPAN, but
which has some fairly serious limitations.

Tied filehandles are still incomplete. sysopen(), truncate(),
flock(), fcntl(), stat() and -X can't currently be trapped.