summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSven Verdoolaege <skimo@breughel.ufsia.ac.be>1996-08-29 15:14:51 +0200
committerAndy Dougherty <doughera@lafcol.lafayette.edu>1996-08-29 15:14:51 +0200
commit58f51617c7df66bccaec3ed5e7aa1f1bd5ca8566 (patch)
treeeb0a2a9814785b286ff0ddc3fa6075190c59647b
parent13776d5a5d849dbac5bdbd98560852fb47797581 (diff)
downloadperl-58f51617c7df66bccaec3ed5e7aa1f1bd5ca8566.tar.gz
more TIEHANDLE
This adds support for a READLINE method.
-rw-r--r--pod/perltie.pod18
-rwxr-xr-xt/op/misc.t5
2 files changed, 17 insertions, 6 deletions
diff --git a/pod/perltie.pod b/pod/perltie.pod
index c5d3686232..41517ac73d 100644
--- a/pod/perltie.pod
+++ b/pod/perltie.pod
@@ -611,10 +611,8 @@ use the each() function to iterate over such. Example:
This is partially implemeted now.
-A class implementing a tied scalar should define the folowing methods:
-TIEHANDLE, PRINT, and possibly DESTROY.
-
-In future READLINE, EOF and possibly others will be added.
+A class implementing a tied filehandle should define the folowing 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
@@ -632,7 +630,7 @@ This is the constructor for the class. That means it is expected to
return a blessed reference of some sort. The refence can be used to
hold some internal information. We won't use it in out example.
- sub TIEHANDLE { print "<shout>\n"; bless [], shift }
+ sub TIEHANDLE { print "<shout>\n"; my $i; bless \$i, shift }
=item PRINT this, LIST
@@ -640,7 +638,14 @@ This method will be triggered every time the tied handle is printed to.
Beyond its self refence it also expects the list that was passed to
the print function.
- sub PRINT { shift; for (@_) { print uc($_) } }
+ 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
@@ -658,6 +663,7 @@ Here's how to use our little example:
print FOO "hello\n";
$a = 4; $b = 6;
print FOO $a, " plus ", $b, " equals ", $a + $b, "\n";
+ print <FOO>;
=head1 SEE ALSO
diff --git a/t/op/misc.t b/t/op/misc.t
index 3b88a0a91d..e3bf57638d 100755
--- a/t/op/misc.t
+++ b/t/op/misc.t
@@ -179,6 +179,9 @@ BEGIN failed--compilation aborted at - line 1.
sub TIEHANDLE {
bless {}, shift;
}
+ sub READLINE {
+ "Out of inspiration";
+ }
sub DESTROY {
print "and destroyed as well\n";
}
@@ -187,7 +190,9 @@ BEGIN failed--compilation aborted at - line 1.
local(*FOO);
tie(*FOO,'foo');
print FOO "sentence.", "reversed", "a", "is", "This";
+ print "-- ", <FOO>, " --\n";
}
EXPECT
This is a reversed sentence.
+-- Out of inspiration --
and destroyed as well