home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 May
/
Chip_2000-05_cd1.bin
/
zkuste
/
Perl
/
ActivePerl-5.6.0.613.msi
/
䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥
/
_45e532c87038331027a377cff63c4946
< prev
next >
Wrap
Text File
|
2000-03-23
|
5KB
|
157 lines
<HTML>
<HEAD>
<TITLE>IO::Wrap - wrap raw filehandles in IO::Handle interface</TITLE>
<LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
<LINK REV="made" HREF="mailto:">
</HEAD>
<BODY>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> IO::Wrap - wrap raw filehandles in IO::Handle interface</P></STRONG>
</TD></TR>
</TABLE>
<A NAME="__index__"></A>
<!-- INDEX BEGIN -->
<UL>
<LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
<LI><A HREF="#synopsis">SYNOPSIS</A></LI>
<LI><A HREF="#description">DESCRIPTION</A></LI>
<LI><A HREF="#notes">NOTES</A></LI>
<LI><A HREF="#warnings">WARNINGS</A></LI>
<LI><A HREF="#author">AUTHOR</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>IO::Wrap - wrap raw filehandles in IO::Handle interface</P>
<P>
<HR>
<H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
<UL>
<LI>Linux</LI>
<LI>Solaris</LI>
<LI>Windows</LI>
</UL>
<HR>
<H1><A NAME="synopsis">SYNOPSIS</A></H1>
<PRE>
use IO::Wrap;
</PRE>
<PRE>
# 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;</PRE>
<PRE>
### At this point, we have no idea what the user gave us...
### a globref? a FileHandle? a scalar filehandle name?</PRE>
<PRE>
$fh = wraphandle($fh);</PRE>
<PRE>
### At this point, we know we have an IO::Handle-like object!</PRE>
<PRE>
$fh->print("Hey there!");
...
}</PRE>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>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 tiehandle().
But that puts the burden on them. Another way to do it is to
use <STRONG>IO::Wrap</STRONG>, which provides you with the following functions:</P>
<DL>
<DT><STRONG><A NAME="item_wraphandle">wraphandle SCALAR</A></STRONG><BR>
<DD>
This function will take a single argument, and ``wrap'' it based on
what it seems to be...
<UL>
<LI>
<STRONG>A raw scalar filehandle name,</STRONG> like <CODE>"STDOUT"</CODE> or <CODE>"Class::HANDLE"</CODE>.
In this case, the filehandle name is wrapped in an IO::Wrap object,
which is returned.
<P></P>
<LI>
<STRONG>A raw filehandle glob,</STRONG> like <CODE>\*STDOUT</CODE>.
In this case, the filehandle glob is wrapped in an IO::Wrap object,
which is returned.
<P></P>
<LI>
<STRONG>A blessed FileHandle object.</STRONG>
In this case, the FileHandle is wrapped in an IO::Wrap object if and only
if your FileHandle class does not support the <A HREF="../../../lib/Pod/perlfunc.html#item_read"><CODE>read()</CODE></A> method.
<P></P>
<LI>
<STRONG>Any other kind of blessed object,</STRONG> which is assumed to be already
conformant to the IO::Handle interface.
In this case, you just get back that object.
<P></P></UL>
</DL>
<P>If you get back an IO::Wrap object, it will obey a basic subset of
the IO:: interface. That is, the following methods (note: I said
<EM>methods</EM>, 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>
<P>
<HR>
<H1><A NAME="notes">NOTES</A></H1>
<P>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 DESTROY method in this class.</P>
<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.</P>
<P>
<HR>
<H1><A NAME="warnings">WARNINGS</A></H1>
<P>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 FOO in package Foo, they must pass it to you either as <CODE>\*FOO</CODE>
or as <CODE>"Foo::FOO"</CODE>. However, <CODE>"STDIN"</CODE> and friends will work just fine.</P>
<P>
<HR>
<H1><A NAME="author">AUTHOR</A></H1>
<P>Eryq (<EM><A HREF="mailto:eryq@zeegee.com">eryq@zeegee.com</A></EM>).
President, ZeeGee Software Inc (<EM><A HREF="http://www.zeegee.com">http://www.zeegee.com</A></EM>).</P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> IO::Wrap - wrap raw filehandles in IO::Handle interface</P></STRONG>
</TD></TR>
</TABLE>
</BODY>
</HTML>