1IO::Wrap(3) User Contributed Perl Documentation IO::Wrap(3)
2
3
4
6 IO::Wrap - wrap raw filehandles in IO::Handle interface
7
9 use IO::Wrap;
10
11 ### Do stuff with any kind of filehandle (including a bare globref), or
12 ### any kind of blessed object that responds to a print() message.
13 ###
14 sub do_stuff {
15 my $fh = shift;
16
17 ### At this point, we have no idea what the user gave us...
18 ### a globref? a FileHandle? a scalar filehandle name?
19
20 $fh = wraphandle($fh);
21
22 ### At this point, we know we have an IO::Handle-like object!
23
24 $fh->print("Hey there!");
25 ...
26 }
27
29 Let's say you want to write some code which does I/O, but you don't
30 want to force the caller to provide you with a FileHandle or IO::Handle
31 object. You want them to be able to say:
32
33 do_stuff(\*STDOUT);
34 do_stuff('STDERR');
35 do_stuff($some_FileHandle_object);
36 do_stuff($some_IO_Handle_object);
37
38 And even:
39
40 do_stuff($any_object_with_a_print_method);
41
42 Sure, one way to do it is to force the caller to use tiehandle(). But
43 that puts the burden on them. Another way to do it is to use IO::Wrap,
44 which provides you with the following functions:
45
46 wraphandle SCALAR
47 This function will take a single argument, and "wrap" it based on
48 what it seems to be...
49
50 · A raw scalar filehandle name, like "STDOUT" or "Class::HANDLE".
51 In this case, the filehandle name is wrapped in an IO::Wrap
52 object, which is returned.
53
54 · A raw filehandle glob, like "\*STDOUT". In this case, the
55 filehandle glob is wrapped in an IO::Wrap object, which is
56 returned.
57
58 · A blessed FileHandle object. In this case, the FileHandle is
59 wrapped in an IO::Wrap object if and only if your FileHandle
60 class does not support the "read()" method.
61
62 · Any other kind of blessed object, which is assumed to be
63 already conformant to the IO::Handle interface. In this case,
64 you just get back that object.
65
66 If you get back an IO::Wrap object, it will obey a basic subset of the
67 IO:: interface. That is, the following methods (note: I said methods,
68 not named operators) should work on the thing you get back:
69
70 close
71 getline
72 getlines
73 print ARGS...
74 read BUFFER,NBYTES
75 seek POS,WHENCE
76 tell
77
79 Clearly, when wrapping a raw external filehandle (like \*STDOUT), I
80 didn't want to close the file descriptor when the "wrapper" object is
81 destroyed... since the user might not appreciate that! Hence, there's
82 no DESTROY method in this class.
83
84 When wrapping a FileHandle object, however, I believe that Perl will
85 invoke the FileHandle::DESTROY when the last reference goes away, so in
86 that case, the filehandle is closed if the wrapped FileHandle really
87 was the last reference to it.
88
90 This module does not allow you to wrap filehandle names which are given
91 as strings that lack the package they were opened in. That is, if a
92 user opens FOO in package Foo, they must pass it to you either as
93 "\*FOO" or as "Foo::FOO". However, "STDIN" and friends will work just
94 fine.
95
97 $Id: Wrap.pm,v 1.2 2005/02/10 21:21:53 dfs Exp $
98
100 Primary Maintainer
101 David F. Skoll (dfs@roaringpenguin.com).
102
103 Original Author
104 Eryq (eryq@zeegee.com). President, ZeeGee Software Inc
105 (http://www.zeegee.com).
106
108 Hey! The above document had some coding errors, which are explained
109 below:
110
111 Around line 212:
112 '=item' outside of any '=over'
113
114 =over without closing =back
115
116
117
118perl v5.16.3 2005-02-10 IO::Wrap(3)