summaryrefslogtreecommitdiff
path: root/pod
diff options
context:
space:
mode:
authorNick Ing-Simmons <nik@tiuk.ti.com>2000-09-01 17:21:57 +0000
committerNick Ing-Simmons <nik@tiuk.ti.com>2000-09-01 17:21:57 +0000
commit301e812593b886ce092a67093cee831022be6f82 (patch)
treedeeb2f784dc2140a7d17e5c858873ba42af2a6d3 /pod
parentb5ddfb46c2ec4c955ac9e13c00d741909a0734a7 (diff)
downloadperl-301e812593b886ce092a67093cee831022be6f82.tar.gz
Document UNTIE. Also tweak implementation to suppress the 'inner references'
warning when UNTIE exists and instead pass the count of extra references to the UNTIE method. p4raw-id: //depot/perl@6981
Diffstat (limited to 'pod')
-rw-r--r--pod/perltie.pod66
1 files changed, 54 insertions, 12 deletions
diff --git a/pod/perltie.pod b/pod/perltie.pod
index d08ed5630c..60df0cb23e 100644
--- a/pod/perltie.pod
+++ b/pod/perltie.pod
@@ -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:
@@ -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,7 +186,7 @@ TIESCALAR classes are certainly possible.
=head2 Tying Arrays
A class implementing a tied ordinary array should define the following
-methods: TIEARRAY, FETCH, STORE, FETCHSIZE, STORESIZE 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.
@@ -192,7 +198,7 @@ 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
+In addition EXTEND will be called when perl would have pre-extended
allocation in a real array.
This means that tied arrays are now I<complete>. The example below needs
@@ -260,10 +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
+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.
-
+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
@@ -285,8 +291,12 @@ there. For example:
}
return $self->{ARRAY}[$idx] = $value;
}
-
-Negative indexes are treated the same as with FETCH.
+
+Negative indexes are treated the same as with FETCH.
+
+=item UNTIE this
+
+Will be called when C<untie> happens. (See below.)
=item DESTROY this
@@ -316,8 +326,8 @@ 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.
+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
@@ -599,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
@@ -629,7 +643,7 @@ 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 and DESTROY. The class can also provide: BINMODE,
+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.
@@ -718,6 +732,11 @@ 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
@@ -855,7 +874,8 @@ 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 this near the top C<use warnings 'untie'>
or was run with the C<-w> flag, Perl prints this
@@ -870,6 +890,25 @@ 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.
@@ -891,3 +930,6 @@ source code to MLDBM.
Tom Christiansen
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>>
+