3 # SEE DOCUMENTATION AT BOTTOM OF FILE
8 use vars qw(@ISA @EXPORT $VERSION);
10 @EXPORT = qw(wraphandle);
15 # The package version, both in 1.23 style *and* usable by MakeMaker:
19 #------------------------------
21 #------------------------------
27 #------------------------------
29 #------------------------------
31 my ($class, $stream) = @_;
34 ### Convert raw scalar to globref:
35 ref($stream) or $stream = \*$stream;
37 ### Wrap globref and incomplete objects:
38 if ((ref($stream) eq 'GLOB') or ### globref
39 (ref($stream) eq 'FileHandle') && !defined(&FileHandle::read)) {
40 return bless \$stream, $class;
42 $stream; ### already okay!
45 #------------------------------
47 #------------------------------
59 wantarray or croak("Can't call getlines in scalar context!");
69 return read($$self, $_[0], $_[1]);
73 return seek($$self, $_[0], $_[1]);
80 #------------------------------
87 IO::Wrap - wrap raw filehandles in IO::Handle interface
94 ### Do stuff with any kind of filehandle (including a bare globref), or
95 ### any kind of blessed object that responds to a print() message.
100 ### At this point, we have no idea what the user gave us...
101 ### a globref? a FileHandle? a scalar filehandle name?
103 $fh = wraphandle($fh);
105 ### At this point, we know we have an IO::Handle-like object!
107 $fh->print("Hey there!");
114 Let's say you want to write some code which does I/O, but you don't
115 want to force the caller to provide you with a FileHandle or IO::Handle
116 object. You want them to be able to say:
120 do_stuff($some_FileHandle_object);
121 do_stuff($some_IO_Handle_object);
125 do_stuff($any_object_with_a_print_method);
127 Sure, one way to do it is to force the caller to use tiehandle().
128 But that puts the burden on them. Another way to do it is to
129 use B<IO::Wrap>, which provides you with the following functions:
134 =item wraphandle SCALAR
136 This function will take a single argument, and "wrap" it based on
137 what it seems to be...
143 B<A raw scalar filehandle name,> like C<"STDOUT"> or C<"Class::HANDLE">.
144 In this case, the filehandle name is wrapped in an IO::Wrap object,
149 B<A raw filehandle glob,> like C<\*STDOUT>.
150 In this case, the filehandle glob is wrapped in an IO::Wrap object,
155 B<A blessed FileHandle object.>
156 In this case, the FileHandle is wrapped in an IO::Wrap object if and only
157 if your FileHandle class does not support the C<read()> method.
161 B<Any other kind of blessed object,> which is assumed to be already
162 conformant to the IO::Handle interface.
163 In this case, you just get back that object.
170 If you get back an IO::Wrap object, it will obey a basic subset of
171 the IO:: interface. That is, the following methods (note: I said
172 I<methods>, not named operators) should work on the thing you get back:
186 Clearly, when wrapping a raw external filehandle (like \*STDOUT),
187 I didn't want to close the file descriptor when the "wrapper" object is
188 destroyed... since the user might not appreciate that! Hence,
189 there's no DESTROY method in this class.
191 When wrapping a FileHandle object, however, I believe that Perl will
192 invoke the FileHandle::DESTROY when the last reference goes away,
193 so in that case, the filehandle is closed if the wrapped FileHandle
194 really was the last reference to it.
199 This module does not allow you to wrap filehandle names which are given
200 as strings that lack the package they were opened in. That is, if a user
201 opens FOO in package Foo, they must pass it to you either as C<\*FOO>
202 or as C<"Foo::FOO">. However, C<"STDIN"> and friends will work just fine.
207 $Id: Wrap.pm,v 1.2 2005/02/10 21:21:53 dfs Exp $
212 =item Primary Maintainer
214 David F. Skoll (F<dfs@roaringpenguin.com>).
216 =item Original Author
218 Eryq (F<eryq@zeegee.com>).
219 President, ZeeGee Software Inc (F<http://www.zeegee.com>).