From: Sven Verdoolaege Date: Thu, 29 Aug 1996 13:14:51 +0000 (+0200) Subject: more TIEHANDLE X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=58f51617c7df66bccaec3ed5e7aa1f1bd5ca8566;p=p5sagit%2Fp5-mst-13.2.git more TIEHANDLE This adds support for a READLINE method. --- diff --git a/pod/perltie.pod b/pod/perltie.pod index c5d3686..41517ac 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 "\n"; bless [], shift } + sub TIEHANDLE { print "\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 ; =head1 SEE ALSO diff --git a/t/op/misc.t b/t/op/misc.t index 3b88a0a..e3bf576 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 "-- ", , " --\n"; } EXPECT This is a reversed sentence. +-- Out of inspiration -- and destroyed as well