Patch for pod/perlpod.pod
[p5sagit/p5-mst-13.2.git] / pod / perltie.pod
index ad5d66f..7c43141 100644 (file)
@@ -6,6 +6,8 @@ perltie - how to hide an object class in a simple variable
 
  tie VARIABLE, CLASSNAME, LIST
 
+ $object = tied VARIABLE
+
  untie VARIABLE
 
 =head1 DESCRIPTION
@@ -31,13 +33,14 @@ In the tie() call, C<VARIABLE> is the name of the variable to be
 enchanted.  C<CLASSNAME> is the name of a class implementing objects of
 the correct type.  Any additional arguments in the C<LIST> are passed to
 the appropriate constructor method for that class--meaning TIESCALAR(),
-TIEARRAY(), or TIEHASH().  (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 C<CLASSNAME>. (You don't actually
-have to return a reference to a right "type" (e.g. HASH or C<CLASSNAME>)
-so long as it's a properly blessed object.)
-
+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
+C<CLASSNAME>. (You don't actually have to return a reference to a right
+"type" (e.g. HASH or C<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 C<use> or C<require> a module
 for you--you need to do that explicitly yourself.
@@ -81,12 +84,12 @@ expected to return a blessed reference to a new scalar
         my $pid = shift || $$; # 0 means me
 
         if ($pid !~ /^\d+$/) {
-            carp "Nice::TieScalar got non-numeric pid $pid" if $^W;
+            carp "Nice::Tie::Scalar got non-numeric pid $pid" if $^W;
             return undef;
         }
 
         unless (kill 0, $pid) { # EPERM or ERSCH, no doubt
-            carp "Nice::TieScalar got bad pid $pid: $!" if $^W;
+            carp "Nice::Tie::Scalar got bad pid $pid: $!" if $^W;
             return undef;
         }
 
@@ -157,7 +160,7 @@ argument--the new value the user is trying to assign.
 =item 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 ncessary, since Perl
+As with other object classes, such a method is seldom necessary, since 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.
 
@@ -190,7 +193,7 @@ take an exception.  (Well, if you access an individual element; an
 aggregate assignment would be missed.) For example:
 
     require Bounded_Array;
-    tie @ary, Bounded_Array, 2;
+    tie @ary, 'Bounded_Array', 2;
     $| = 1;
     for $i (0 .. 10) {
         print "setting index $i: ";
@@ -216,7 +219,7 @@ anonymous ARRAY ref) will be accessed.
 In our example, just to show you that you don't I<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 C<{BOUND}> field will
-store the maximum bound allowed, and the C<{ARRAY} field will hold the
+store the maximum bound allowed, and the C<{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.
@@ -271,7 +274,7 @@ there.  For example:
 =item DESTROY this
 
 This method will be triggered when the tied variable needs to be destructed.
-As with the sclar tie class, this is almost never needed in a
+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.
 
@@ -301,8 +304,8 @@ functions to iterate over all the keys.  And DESTROY is called when the
 tied variable is garbage collected.
 
 If this seems like a lot, then feel free to merely inherit
-from the standard TieHash module for most of your methods, redefining only
-the interesting ones.  See L<TieHash> for details.
+from the standard Tie::Hash module for most of your methods, redefining only
+the interesting ones.  See L<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
@@ -315,7 +318,7 @@ with the name of the file (minus the dot) and you get back that dotfile's
 contents.  For example:
 
     use DotFiles;
-    tie %dot, DotFiles;
+    tie %dot, 'DotFiles';
     if ( $dot{profile} =~ /MANPATH/ ||
          $dot{login}   =~ /MANPATH/ ||
          $dot{cshrc}   =~ /MANPATH/    )
@@ -325,7 +328,7 @@ contents.  For example:
 
 Or here's another sample of using our tied class:
 
-    tie %him, DotFiles, 'daemon';
+    tie %him, 'DotFiles', 'daemon';
     foreach $f ( keys %him ) {
        printf "daemon dot file %s is size %d\n",
            $f, length $him{$f};
@@ -478,7 +481,14 @@ If they wanted to clobber something, they might say:
     $ob->clobber(1);
     $daemon_dots{signature} = "A true daemon\n";
 
-Where the clobber method is simply:
+Another way to lay hands on a reference to the underlying object is to
+use the tied() function, so they might alternately have set clobber
+using:
+
+    tie %daemon_dots, 'daemon';
+    tied(%daemon_dots)->clobber(1);
+
+The clobber method is simply:
 
     sub clobber {
        my $self = shift;
@@ -500,9 +510,17 @@ be careful to check whether they really want to clobber files.
        croak "@{[&whowasi]}: won't remove file $file"
            unless $self->{CLOBBER};
        delete $self->{LIST}->{$dot};
-       unlink($file) || carp "@{[&whowasi]}: can't unlink $file: $!";
+       my $success = unlink($file);
+       carp "@{[&whowasi]}: can't unlink $file: $!" unless $success;
+       $success;
     }
 
+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.
+
 =item CLEAR this
 
 This method is triggered when the whole hash is to be cleared, usually by
@@ -545,7 +563,7 @@ call.
     sub FIRSTKEY {
        carp &whowasi if $DEBUG;
        my $self = shift;
-       my $a = keys %{$self->{LIST}};
+       my $a = keys %{$self->{LIST}};          # reset each() iterator
        each %{$self->{LIST}}
     }
 
@@ -556,7 +574,7 @@ second argument which is the last key that had been accessed.  This is
 useful if you're carrying about ordering or calling the iterator from more
 than one sequence, or not really storing things in a hash anywhere.
 
-For our example, we our using a real hash so we'll just do the simple
+For our example, we're using a real hash so we'll just do the simple
 thing, but we'll have to indirect through the LIST field.
 
     sub NEXTKEY  {
@@ -583,7 +601,7 @@ use the each() function to iterate over such.  Example:
 
     # print out history file offsets
     use NDBM_File;
-    tie(%HIST, NDBM_File, '/usr/lib/news/history', 1, 0);
+    tie(%HIST, 'NDBM_File', '/usr/lib/news/history', 1, 0);
     while (($key,$val) = each %HIST) {
         print $key, ' = ', unpack('L',$val), "\n";
     }
@@ -591,7 +609,61 @@ use the each() function to iterate over such.  Example:
 
 =head2 Tying FileHandles
 
-This isn't implemented yet.  Sorry; maybe someday.
+This is partially implemented now.
+
+A class implementing a tied filehandle should define the following methods:
+TIEHANDLE, PRINT and/or READLINE, and possibly DESTROY.
+
+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
+special way. See nvi and the Apache module for examples.
+
+In our example we're going to create a shouting handle.
+
+    package Shout;
+
+=over
+
+=item 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. We won't use it in out example.
+
+    sub TIEHANDLE { print "<shout>\n"; my $i; bless \$i, shift }
+
+=item PRINT this, LIST
+
+This method will be triggered every time the tied handle is printed to.
+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 READLINE this
+
+This method will be called when the handle is read from. The method
+should return undef when there is no more data.
+
+    sub READLINE { $r = shift; "PRINT called $$r times\n"; }
+
+=item 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.
+
+    sub DESTROY { print "</shout>\n" }
+
+=back
+
+Here's how to use our little example:
+
+    tie(*FOO,'Shout');
+    print FOO "hello\n";
+    $a = 4; $b = 6;
+    print FOO $a, " plus ", $b, " equals ", $a + $b, "\n";
+    print <FOO>;
 
 =head1 SEE ALSO
 
@@ -615,3 +687,5 @@ source code to MLDBM.
 =head1 AUTHOR
 
 Tom Christiansen
+
+TIEHANDLE by Sven Verdoolaege E<lt>F<skimo@dns.ufsia.ac.be>E<gt>