man-pages/man3/IO::Wrap.3pm.html
2021-03-31 01:06:50 +01:00

205 lines
5.0 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of IO::Wrap</TITLE>
</HEAD><BODY>
<H1>IO::Wrap</H1>
Section: User Contributed Perl Documentation (3pm)<BR>Updated: 2019-02-28<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
IO::Wrap - wrap raw filehandles in IO::Handle interface
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
use IO::Wrap;
### Do stuff with any kind of filehandle (including a bare globref), or
### any kind of blessed object that responds to a print() message.
###
sub do_stuff {
my $fh = shift;
### At this point, we have no idea what the user gave us...
### a globref? a FileHandle? a scalar filehandle name?
$fh = wraphandle($fh);
### At this point, we know we have an IO::Handle-like object!
$fh-&gt;print(&quot;Hey there!&quot;);
...
}
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
Let's say you want to write some code which does I/O, but you don't
want to force the caller to provide you with a FileHandle or IO::Handle
object. You want them to be able to say:
<P>
<PRE>
do_stuff(\*STDOUT);
do_stuff('STDERR');
do_stuff($some_FileHandle_object);
do_stuff($some_IO_Handle_object);
</PRE>
<P>
And even:
<P>
<PRE>
do_stuff($any_object_with_a_print_method);
</PRE>
<P>
Sure, one way to do it is to force the caller to use <B>tiehandle()</B>.
But that puts the burden on them. Another way to do it is to
use <B>IO::Wrap</B>, which provides you with the following functions:
<DL COMPACT>
<DT id="1">wraphandle <FONT SIZE="-1">SCALAR</FONT><DD>
This function will take a single argument, and ``wrap'' it based on
what it seems to be...
<DL COMPACT><DT id="2"><DD>
<DL COMPACT>
<DT id="3">&bull;<DD>
<B>A raw scalar filehandle name,</B> like <TT>&quot;STDOUT&quot;</TT> or <TT>&quot;Class::HANDLE&quot;</TT>.
In this case, the filehandle name is wrapped in an IO::Wrap object,
which is returned.
<DT id="4">&bull;<DD>
<B>A raw filehandle glob,</B> like <TT>&quot;\*STDOUT&quot;</TT>.
In this case, the filehandle glob is wrapped in an IO::Wrap object,
which is returned.
<DT id="5">&bull;<DD>
<B>A blessed FileHandle object.</B>
In this case, the FileHandle is wrapped in an IO::Wrap object if and only
if your FileHandle class does not support the <TT>&quot;read()&quot;</TT> method.
<DT id="6">&bull;<DD>
<B>Any other kind of blessed object,</B> which is assumed to be already
conformant to the IO::Handle interface.
In this case, you just get back that object.
</DL>
</DL>
<DL COMPACT><DT id="7"><DD>
</DL>
</DL>
<P>
If you get back an IO::Wrap object, it will obey a basic subset of
the <FONT SIZE="-1">IO::</FONT> interface. That is, the following methods (note: I said
<I>methods</I>, not named operators) should work on the thing you get back:
<P>
<PRE>
close
getline
getlines
print ARGS...
read BUFFER,NBYTES
seek POS,WHENCE
tell
</PRE>
<A NAME="lbAE">&nbsp;</A>
<H2>NOTES</H2>
Clearly, when wrapping a raw external filehandle (like \*STDOUT),
I didn't want to close the file descriptor when the ``wrapper'' object is
destroyed... since the user might not appreciate that! Hence,
there's no <FONT SIZE="-1">DESTROY</FONT> method in this class.
<P>
When wrapping a FileHandle object, however, I believe that Perl will
invoke the FileHandle::DESTROY when the last reference goes away,
so in that case, the filehandle is closed if the wrapped FileHandle
really was the last reference to it.
<A NAME="lbAF">&nbsp;</A>
<H2>WARNINGS</H2>
This module does not allow you to wrap filehandle names which are given
as strings that lack the package they were opened in. That is, if a user
opens <FONT SIZE="-1">FOO</FONT> in package Foo, they must pass it to you either as <TT>&quot;\*FOO&quot;</TT>
or as <TT>&quot;Foo::FOO&quot;</TT>. However, <TT>&quot;STDIN&quot;</TT> and friends will work just fine.
<A NAME="lbAG">&nbsp;</A>
<H2>VERSION</H2>
<TT>$Id:</TT> Wrap.pm,v 1.2 2005/02/10 21:21:53 dfs Exp $
<A NAME="lbAH">&nbsp;</A>
<H2>AUTHOR</H2>
<DL COMPACT>
<DT id="8">Primary Maintainer<DD>
Dianne Skoll (<I><A HREF="mailto:dfs@roaringpenguin.com">dfs@roaringpenguin.com</A></I>).
<DT id="9">Original Author<DD>
Eryq (<I><A HREF="mailto:eryq@zeegee.com">eryq@zeegee.com</A></I>).
President, ZeeGee Software Inc (<I><A HREF="http://www.zeegee.com">http://www.zeegee.com</A></I>).
<P>
</DL>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="10"><A HREF="#lbAB">NAME</A><DD>
<DT id="11"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="12"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="13"><A HREF="#lbAE">NOTES</A><DD>
<DT id="14"><A HREF="#lbAF">WARNINGS</A><DD>
<DT id="15"><A HREF="#lbAG">VERSION</A><DD>
<DT id="16"><A HREF="#lbAH">AUTHOR</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:46 GMT, March 31, 2021
</BODY>
</HTML>