What are the arguments used in IO::Pipe perl constructors?
What I see in documentation is:
IO::Pipe::new optionally takes two arguments, which should be objects blessed into IO::Handle, or a subclass thereof. These two objects will be used for the system call to pipe. If no arguments are given then method handles is called on the new IO::Pipe object.
I don’t really get what it means. Can someone provide an example/explanation?
It’s straight-forward once you learn object-oriented programming. See the Stack Overflow archive for recommendations of appropriate teaching material. To follow the explanation below, you need to keep in mind that
blessworks not only on hashrefs.You should also already completely understand what the underlying
pipePOSIX system call does, and what a file descriptor is in C and Perl and what all sort of things it can point to, and how a FD is passed around in Perl as a glob. If these pieces are confusing, too, open separate questions and reference this one.tl;dr version:
$readerand$writerare expected to be file handles you or something else has opened earlier, most likely from theopenfunction or aIO::Fileinstance. The documentation mentions IO::Handle foremost because likely you want to also pipe FDs that are not proper files, but standard streams (STDIN, STDOUT) connected to certain processes, and for this purpose IO::Handle suffices.Detailed version:
The
$readerand$writervariables are expected to contain object instances of typeIO::Handle($readeris-aIO::Handle).IO::Handleis rarely used, more often its subclassIO::File.For historical reasons, a lot of things that are not strict subclasses of IO::Handle work, too. It suffices that they merely behave like IO::Handle (“duck type”), i.e. provide some of the methods mentioned in the documentation, and those need not be inherited from IO::Handle.