coppit has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,

For argument's sake, let's say you wanted to create a wrapper class for FileHandle, where you store a real FileHandle as a member of the class (OO aggregation instead of inheritance). (More details: Best way to implement ungetstring?).

One approach is to: (1) Have your class provide custom definitions for all the standard FileHandle methods which would simply call the analogous method on the real FileHandle object:

sub read { my $self = shift; $self->{real_filehandle}->read(@_); }
(2) You'd also need to tie the FileHandle so that stuff like eof would work.

Question: Is there something special about CORE::pipe? There's no TIEHANDLE function for it, and somehow it seems to know that I'm not using a "real" FileHandle. That is, this code:

my $out = new MyFileHandleWrapper; my $in = new MyFileHandleWrapper; pipe $out, $in or die; # Fork, close $in and read from $out in the parent, # print to $in in the child
Raises warnings about trying to close and print to unopened filehandles. However, if I change MyFileHandleWrapper to FileHandle, it works fine. If I'm careful to provide all the methods of FileHandle, and I tie all the TIEHANDLE functions, shouldn't this work?

Code is here if you want to run "make test" on my distro.

Thanks!

Replies are listed 'Best First'.
Re: Is ::pipe magical?
by Zaxo (Archbishop) on Oct 21, 2003 at 03:06 UTC

    The properties of a pipe handle derive from those of unix pipes. They are filehandles of a different kind from those of static disk files.

    One property that I think is relevant to your problem is that, like sockets and terminals, they are not seekable. The ioctls for that don't exist in the operating system. FileHandle is derived from IO::File, IO::Seekable, and IO::Handle. It is itself a wrapper for them.

    To see an object interface for pipes, take a look at IO::Pipe. Perhaps you can work that into your wrapper class somehow. A unified treatment would be neat. I'm not sure how you would want to treat seek with that, but either failing or some kind of seekable buffering would be sensible there.

    After Compline,
    Zaxo