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
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
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
print FOO "hello\n";
$a = 4; $b = 6;
print FOO $a, " plus ", $b, " equals ", $a + $b, "\n";
+ print <FOO>;
=head1 SEE ALSO
sub TIEHANDLE {
bless {}, shift;
}
+ sub READLINE {
+ "Out of inspiration";
+ }
sub DESTROY {
print "and destroyed as well\n";
}
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