Document UNTIE. Also tweak implementation to suppress the 'inner references'
[p5sagit/p5-mst-13.2.git] / pod / perltie.pod
index 8dc7c17..60df0cb 100644 (file)
@@ -23,7 +23,7 @@ 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.  All of the complexity of the class is
+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()
@@ -48,7 +48,7 @@ for you--you need to do that explicitly yourself.
 =head2 Tying Scalars
 
 A class implementing a tied scalar should define the following methods:
-TIESCALAR, FETCH, STORE, and possibly DESTROY.
+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:
@@ -60,10 +60,10 @@ 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 <F<Jarkko.Hietaniemi@hut.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.
+We'll use Jarkko Hietaniemi <F<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.
 
     package Nice;
     use Carp;
@@ -157,6 +157,12 @@ argument--the new value the user is trying to assign.
         return $new_nicety;
     }
 
+=item UNTIE this
+
+This method will be triggered when the C<untie> occurs. This can be useful
+if the class needs to know when no further calls will be made. (Except DESTROY
+of course.) See below for more details.
+
 =item DESTROY this
 
 This method will be triggered when the tied variable needs to be destructed.
@@ -180,17 +186,28 @@ TIESCALAR classes are certainly possible.
 =head2 Tying Arrays
 
 A class implementing a tied ordinary array should define the following
-methods: TIEARRAY, FETCH, STORE, and perhaps DESTROY.
+methods: TIEARRAY, FETCH, STORE, FETCHSIZE, STORESIZE and perhaps UNTIE and/or DESTROY.
+
+FETCHSIZE and STORESIZE are used to provide C<$#array> and
+equivalent C<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 B<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
+B<Tie::Array> simply C<croak>.
+
+In addition EXTEND will be called when perl would have pre-extended
+allocation in a real array.
 
-B<WARNING>: Tied arrays are I<incomplete>.  They are also distinctly lacking
-something for the C<$#ARRAY> access (which is hard, as it's an lvalue), as
-well as the other obvious array functions, like push(), pop(), shift(),
-unshift(), and splice().
+This means that tied arrays are now I<complete>. The example below needs
+upgrading to illustrate this. (The documentation in B<Tie::Array> is more
+complete.)
 
 For this discussion, we'll implement an array whose indices are fixed at
 its creation.  If you try to access anything beyond those bounds, you'll
-take an exception.  (Well, if you access an individual element; an
-aggregate assignment would be missed.) For example:
+take an exception.  For example:
 
     require Bounded_Array;
     tie @ary, 'Bounded_Array', 2;
@@ -249,6 +266,10 @@ index whose value we're trying to fetch.
       return $self->{ARRAY}[$idx];
     }
 
+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.
+
 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
@@ -271,6 +292,12 @@ there.  For example:
       return $self->{ARRAY}[$idx] = $value;
     }
 
+Negative indexes are treated the same as with FETCH.
+
+=item UNTIE this
+
+Will be called when C<untie> happens. (See below.)
+
 =item DESTROY this
 
 This method will be triggered when the tied variable needs to be destructed.
@@ -293,14 +320,14 @@ the following output demonstrates:
 
 =head2 Tying Hashes
 
-As the first Perl data type to be tied (see dbmopen()), hashes have the
-most complete and useful tie() implementation.  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.
-And DESTROY is called when the tied variable is garbage collected.
+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.  UNTIE is called when C<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::Hash module for most of your methods, redefining only the
@@ -582,6 +609,10 @@ thing, but we'll have to go through the LIST field indirectly.
        return each %{ $self->{LIST} }
     }
 
+=item UNTIE this
+
+This is called when C<untie> occurs.
+
 =item DESTROY this
 
 This method is triggered when a tied hash is about to go out of
@@ -594,9 +625,9 @@ or have auxiliary state to clean up.  Here's a very simple function:
 
 =back
 
-Note that functions such as keys() and values() may return huge array
-values when used on large objects, like DBM files.  You may prefer to
-use the each() function to iterate over such.  Example:
+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:
 
     # print out history file offsets
     use NDBM_File;
@@ -611,8 +642,10 @@ use the each() function to iterate over such.  Example:
 This is partially implemented now.
 
 A class implementing a tied filehandle should define the following
-methods: TIEHANDLE, at least one of PRINT, READLINE, GETC, or READ,
-and possibly DESTROY.
+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.
 
 It is especially useful when perl is embedded in some other program,
 where output to STDOUT and STDERR may have to be redirected in some
@@ -632,23 +665,51 @@ hold some internal information.
 
     sub TIEHANDLE { print "<shout>\n"; my $i; bless \$i, shift }
 
+=item WRITE this, LIST
+
+This method will be called when the handle is written to via the
+C<syswrite> function.
+
+    sub WRITE {
+       $r = shift;
+       my($buf,$len,$offset) = @_;
+       print "WRITE called, \$buf=$buf, \$len=$len, \$offset=$offset";
+    }
+
 =item PRINT this, LIST
 
-This method will be triggered every time the tied handle is printed to.
+This method will be triggered every time the tied handle is printed to
+with the C<print()> function.
 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($_),@_)),$\ }
 
-=item READ this LIST
+=item PRINTF this, LIST
+
+This method will be triggered every time the tied handle is printed to
+with the C<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, @_)."\n";
+    }
+
+=item READ this, LIST
 
 This method will be called when the handle is read from via the C<read>
 or C<sysread> functions.
 
     sub READ {
-       $r = shift;
-       my($buf,$len,$offset) = @_;
-       print "READ called, \$buf=$buf, \$len=$len, \$offset=$offset";
+       my $self = shift;
+       my $$bufref = \$_[0];
+       my(undef,$len,$offset) = @_;
+       print "READ called, \$buf=$bufref, \$len=$len, \$offset=$offset";
+       # add to $$bufref, set $len to number of characters read
+       $len;
     }
 
 =item READLINE this
@@ -656,7 +717,7 @@ or C<sysread> functions.
 This method will be called when the handle is read from via <HANDLE>.
 The method should return undef when there is no more data.
 
-    sub READLINE { $r = shift; "PRINT called $$r times\n"; }
+    sub READLINE { $r = shift; "READLINE called $$r times\n"; }
 
 =item GETC this
 
@@ -664,6 +725,18 @@ This method will be called when the C<getc> function is called.
 
     sub GETC { print "Don't GETC, Get Perl"; return "a"; }
 
+=item CLOSE this
+
+This method will be called when the handle is closed via the C<close>
+function.
+
+    sub CLOSE { print "CLOSE called.\n" }
+
+=item UNTIE this
+
+As with the other types of ties, this method will be called when C<untie> happens.
+It may be appropriate to "auto CLOSE" when this occurs.
+
 =item DESTROY this
 
 As with the other types of ties, this method will be called when the
@@ -682,6 +755,11 @@ Here's how to use our little example:
     print FOO $a, " plus ", $b, " equals ", $a + $b, "\n";
     print <FOO>;
 
+=head2 UNTIE this
+
+You can define for all tie types an UNTIE method that will be called
+at untie().
+
 =head2 The C<untie> Gotcha
 
 If you intend making use of the object returned from either tie() or
@@ -695,6 +773,7 @@ a scalar.
     package Remember;
 
     use strict;
+    use warnings;
     use IO::File;
 
     sub TIESCALAR {
@@ -788,16 +867,18 @@ 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 C<$x>.  That means that when untie() gets called
+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?
-Well, the good old C<-w> flag will spot any instances where you call
+Prior to the introduction of the optional UNTIE method the only way
+was the good old C<-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 is run with the C<-w> flag, Perl prints this
+the second script above this near the top C<use warnings 'untie'>
+or was run with the C<-w> flag, Perl prints this
 warning message:
 
     untie attempted while 1 inner references still exist
@@ -809,27 +890,46 @@ called:
     undef $x;
     untie $fred;
 
+Now that UNTIE exists the class designer can decide which parts of the
+class functionality are really associated with C<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:
+
+    sub UNTIE
+    {
+     my ($obj,$count) = @_;
+     carp "untie attempted while $count inner references still exist" if $count;
+    }
+
 =head1 SEE ALSO
 
 See L<DB_File> or L<Config> for some interesting tie() implementations.
+A good starting point for many tie() implementations is with one of the
+modules L<Tie::Scalar>, L<Tie::Array>, L<Tie::Hash>, or L<Tie::Handle>.
 
 =head1 BUGS
 
-Tied arrays are I<incomplete>.  They are also distinctly lacking something
-for the C<$#ARRAY> access (which is hard, as it's an lvalue), as well as
-the other obvious array functions, like push(), pop(), shift(), unshift(),
-and splice().
-
 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 experimental
 module that does attempt to address this need partially is the MLDBM
-module.  Check your nearest CPAN site as described in L<perlmod> for
+module.  Check your nearest CPAN site as described in L<perlmodlib> for
 source code to MLDBM.
 
 =head1 AUTHOR
 
 Tom Christiansen
 
-TIEHANDLE by Sven Verdoolaege <F<skimo@dns.ufsia.ac.be>>
+TIEHANDLE by Sven Verdoolaege <F<skimo@dns.ufsia.ac.be>> and Doug MacEachern <F<dougm@osf.org>>
+
+UNTIE by Nick Ing-Simmons <F<nick@ing-simmons.net>>
+