See Defining a New Stream for a simple example of defining a stream
that reads from a C FILE
stream.
For a more realistic example, library(codesio)
implements a
stream that can return a list of
all characters written to it. The source code for this library is
located in library/codesio.pl and library/codesio.c and
can serve as a useful sample for user defined streams both for input
and output. That code also illustrates other important features of
user defined streams, for instance ensuring that all the streams have
been closed when the foreign resource is unloaded.