coppit has asked for the wisdom of the Perl Monks concerning the following question:
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:
(2) You'd also need to tie the FileHandle so that stuff like eof would work.sub read { my $self = shift; $self->{real_filehandle}->read(@_); }
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:
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?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
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 |