This is partially implemented now.
A class implementing a tied filehandle should define the following methods:
-TIEHANDLE, PRINT and/or READLINE, and possibly DESTROY.
+TIEHANDLE, at least one of PRINT, READLINE, GETC or READ, 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
=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.
+This method will be called when the handle is read from via <HANDLE>.
+The method should return undef when there is no more data.
sub READLINE { $r = shift; "PRINT called $$r times\n"; }
+
+=item READ this LIST
+This method will be called when the handle is read from via the C<read>
+or C<sysread> functions.
+
+ sub READ {
+ $r = shift;
+ my($buf,$len,$offset) = @_;
+ print "READ called, \$buf=$buf, \$len=$len, \$offset=$offset";
+ }
+
+=item GETC this
+
+This method will be called when the C<getc> function is called.
+
+ sub GETC { print "Don't GETC, Get Perl"; return "a"; }
+
=item DESTROY this
As with the other types of ties, this method will be called when the
{
dSP; dTARGET;
GV *gv;
+ MAGIC *mg;
if (MAXARG <= 0)
gv = stdingv;
gv = (GV*)POPs;
if (!gv)
gv = argvgv;
+
+ if (SvMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q'))) {
+ PUSHMARK(SP);
+ XPUSHs(mg->mg_obj);
+ PUTBACK;
+ ENTER;
+ perl_call_method("GETC", GIMME);
+ LEAVE;
+ SPAGAIN;
+ if (GIMME == G_SCALAR)
+ SvSetSV_nosteal(TARG, TOPs);
+ RETURN;
+ }
if (!gv || do_eof(gv)) /* make sure we have fp with something */
RETPUSHUNDEF;
TAINT;
Sock_size_t bufsize;
SV *bufsv;
STRLEN blen;
+ MAGIC *mg;
gv = (GV*)*++MARK;
+ if (SvMAGICAL(gv) && (mg = mg_find((SV*)gv, 'q'))) {
+ SV *sv;
+
+ PUSHMARK(MARK-1);
+ *MARK = mg->mg_obj;
+ ENTER;
+ perl_call_method("READ", G_SCALAR);
+ LEAVE;
+ SPAGAIN;
+ sv = POPs;
+ SP = ORIGMARK;
+ PUSHs(sv);
+ RETURN;
+ }
+
if (!gv)
goto say_undef;
bufsv = *++MARK;
}
sub DESTROY {
print "and destroyed as well\n";
- }
+ }
+ sub READ {
+ shift;
+ print STDOUT "foo->can(READ)(@_)\n";
+ return 100;
+ }
+ sub GETC {
+ shift;
+ print STDOUT "Don't GETC, Get Perl\n";
+ return "a";
+ }
}
{
local(*FOO);
tie(*FOO,'foo');
print FOO "sentence.", "reversed", "a", "is", "This";
print "-- ", <FOO>, " --\n";
+ my($buf,$len,$offset);
+ $buf = "string";
+ $len = 10; $offset = 1;
+ read(FOO, $buf, $len, $offset) == 100 or die "foo->READ failed";
+ getc(FOO) eq "a" or die "foo->GETC failed";
}
EXPECT
This is a reversed sentence.
-- Out of inspiration --
+foo->can(READ)(string 10 1)
+Don't GETC, Get Perl
and destroyed as well
########
my @a; $a[2] = 1; for (@a) { $_ = 2 } print "@a\n"