a547c7ae28306b393d21c928effabe6fe2e4c378
[dh-make-perl] / dev / arm / libio-stringy-perl / io-stringy-2.110 / docs / IO / Wrap.pm.html
1 <HTML>
2 <HEAD>
3   <TITLE>IO::Wrap 2.102</TITLE>
4 </HEAD>
5 <BODY 
6        bgcolor="#FFFFFF" link="#CC3366" vlink="#993366" alink="#FF6666">
7 <FONT FACE="sans-serif" SIZE=-1><A HREF="http://www.zeegee.com" TARGET="_top"><IMG SRC="icons/zeegee.gif" ALT="ZeeGee Software" ALIGN="RIGHT" BORDER="0"></A><A NAME="__TOP__"><H1>IO::Wrap 2.102</H1>
8 </A><UL>
9 <LI> <A NAME="menu:NAME"><A HREF="#NAME">NAME</A></A>
10 <LI> <A NAME="menu:SYNOPSIS"><A HREF="#SYNOPSIS">SYNOPSIS</A></A>
11 <LI> <A NAME="menu:DESCRIPTION"><A HREF="#DESCRIPTION">DESCRIPTION</A></A>
12 <LI> <A NAME="menu:NOTES"><A HREF="#NOTES">NOTES</A></A>
13 <LI> <A NAME="menu:WARNINGS"><A HREF="#WARNINGS">WARNINGS</A></A>
14 <LI> <A NAME="menu:VERSION"><A HREF="#VERSION">VERSION</A></A>
15 <LI> <A NAME="menu:AUTHOR"><A HREF="#AUTHOR">AUTHOR</A></A>
16 </UL>
17
18
19 <P><HR>
20 <A NAME="NAME"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> NAME</H2></A>
21
22
23 <P>IO::Wrap - wrap raw filehandles in IO::Handle interface
24
25
26
27 <P><HR>
28 <A NAME="SYNOPSIS"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> SYNOPSIS</H2></A>
29
30 <FONT SIZE=3 FACE="courier"><PRE>
31    use IO::Wrap;
32        
33    ### Do stuff with any kind of filehandle (including a bare globref), or 
34    ### any kind of blessed object that responds to a print() message.
35    ###
36    sub do_stuff {
37        my $fh = shift;         
38        
39        ### At this point, we have no idea what the user gave us... 
40        ### a globref? a FileHandle? a scalar filehandle name?
41        
42        $fh = wraphandle($fh);  
43         
44        ### At this point, we know we have an IO::Handle-like object!
45        
46        $fh-&gt;print(&quot;Hey there!&quot;);
47        ...
48    }
49     
50 </PRE></FONT>
51
52
53 <P><HR>
54 <A NAME="DESCRIPTION"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> DESCRIPTION</H2></A>
55
56
57 <P>Let's say you want to write some code which does I/O, but you don't 
58 want to force the caller to provide you with a FileHandle or IO::Handle
59 object.  You want them to be able to say:
60
61 <FONT SIZE=3 FACE="courier"><PRE>
62     do_stuff(\*STDOUT);
63     do_stuff('STDERR');
64     do_stuff($some_FileHandle_object);
65     do_stuff($some_IO_Handle_object);
66 </PRE></FONT>
67
68 <P>And even:
69
70 <FONT SIZE=3 FACE="courier"><PRE>
71     do_stuff($any_object_with_a_print_method);
72 </PRE></FONT>
73
74 <P>Sure, one way to do it is to force the caller to use tiehandle().  
75 But that puts the burden on them.  Another way to do it is to 
76 use <B>IO::Wrap</B>, which provides you with the following functions:
77
78
79
80 <DL>
81 <P><DT><B><A NAME="item:wraphandle"><A NAME="item:wraphandle_SCALAR">wraphandle SCALAR</A></A></B></DT>
82 <DD>
83 This function will take a single argument, and &quot;wrap&quot; it based on
84 what it seems to be...
85
86
87
88 <UL>
89 <P><LI>
90 <P><B>A raw scalar filehandle name,</B> like <CODE>&quot;STDOUT&quot;</CODE> or <CODE>&quot;Class::HANDLE&quot;</CODE>.
91 In this case, the filehandle name is wrapped in an IO::Wrap object, 
92 which is returned.
93
94 <P><LI>
95 <P><B>A raw filehandle glob,</B> like <CODE>\*STDOUT</CODE>.
96 In this case, the filehandle glob is wrapped in an IO::Wrap object, 
97 which is returned.
98
99 <P><LI>
100 <P><B>A blessed FileHandle object.</B>
101 In this case, the FileHandle is wrapped in an IO::Wrap object if and only
102 if your FileHandle class does not support the <CODE>read()</CODE> method.
103
104 <P><LI>
105 <P><B>Any other kind of blessed object,</B> which is assumed to be already
106 conformant to the IO::Handle interface.
107 In this case, you just get back that object.
108
109 </UL>
110
111 </DL>
112
113
114 <P>If you get back an IO::Wrap object, it will obey a basic subset of
115 the IO:: interface.  That is, the following methods (note: I said
116 <I>methods</I>, not named operators) should work on the thing you get back:
117
118 <FONT SIZE=3 FACE="courier"><PRE>
119     close 
120     getline 
121     getlines 
122     print ARGS...
123     read BUFFER,NBYTES
124     seek POS,WHENCE
125     tell 
126 </PRE></FONT>
127
128
129 <P><HR>
130 <A NAME="NOTES"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> NOTES</H2></A>
131
132
133 <P>Clearly, when wrapping a raw external filehandle (like \*STDOUT), 
134 I didn't want to close the file descriptor when the &quot;wrapper&quot; object is
135 destroyed... since the user might not appreciate that!  Hence,
136 there's no DESTROY method in this class.
137
138
139 <P>When wrapping a FileHandle object, however, I believe that Perl will 
140 invoke the FileHandle::DESTROY when the last reference goes away,
141 so in that case, the filehandle is closed if the wrapped FileHandle
142 really was the last reference to it.
143
144
145
146 <P><HR>
147 <A NAME="WARNINGS"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> WARNINGS</H2></A>
148
149
150 <P>This module does not allow you to wrap filehandle names which are given
151 as strings that lack the package they were opened in. That is, if a user 
152 opens FOO in package Foo, they must pass it to you either as <CODE>\*FOO</CODE> 
153 or as <CODE>&quot;Foo::FOO&quot;</CODE>.  However, <CODE>&quot;STDIN&quot;</CODE> and friends will work just fine.
154
155
156
157 <P><HR>
158 <A NAME="VERSION"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> VERSION</H2></A>
159
160
161 <P>$Id: Wrap.pm,v 2.102 2001/08/17 02:06:33 eryq Exp $
162     
163
164
165
166 <P><HR>
167 <A NAME="AUTHOR"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> AUTHOR</H2></A>
168
169
170 <P>Eryq (<I><FILE><A HREF="mailto:eryq@zeegee.com">eryq@zeegee.com</A></FILE></I>).
171 President, ZeeGee Software Inc (<I><FILE><A HREF="http://www.zeegee.com">http://www.zeegee.com</A></FILE></I>).
172
173 <P><HR>
174 <ADDRESS><FONT SIZE=-1>
175 Generated Sun Dec 21 13:54:38 2003 by cvu_pod2html
176 </FONT></ADDRESS>
177 </FONT></BODY>
178 </HTML>