1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
.\" -*- mode: troff; coding: utf-8 -*-
.\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
.ie n \{\
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "IO::Handle 3perl"
.TH IO::Handle 3perl 2024-05-30 "perl v5.38.2" "Perl Programmers Reference Guide"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH NAME
IO::Handle \- supply object methods for I/O handles
.SH SYNOPSIS
.IX Header "SYNOPSIS"
.Vb 1
\& use IO::Handle;
\&
\& my $io = IO::Handle\->new();
\& if ($io\->fdopen(fileno(STDIN),"r")) {
\& print $io\->getline;
\& $io\->close;
\& }
\&
\& my $io = IO::Handle\->new();
\& if ($io\->fdopen(fileno(STDOUT),"w")) {
\& $io\->print("Some text\en");
\& }
\&
\& # setvbuf is not available by default on Perls 5.8.0 and later.
\& use IO::Handle \*(Aq_IOLBF\*(Aq;
\& $io\->setvbuf(my $buffer_var, _IOLBF, 1024);
\&
\& undef $io; # automatically closes the file if it\*(Aqs open
\&
\& autoflush STDOUT 1;
.Ve
.SH DESCRIPTION
.IX Header "DESCRIPTION"
\&\f(CW\*(C`IO::Handle\*(C'\fR is the base class for all other IO handle classes. It is
not intended that objects of \f(CW\*(C`IO::Handle\*(C'\fR would be created directly,
but instead \f(CW\*(C`IO::Handle\*(C'\fR is inherited from by several other classes
in the IO hierarchy.
.PP
If you are reading this documentation, looking for a replacement for
the \f(CW\*(C`FileHandle\*(C'\fR package, then I suggest you read the documentation
for \f(CW\*(C`IO::File\*(C'\fR too.
.SH CONSTRUCTOR
.IX Header "CONSTRUCTOR"
.IP "new ()" 4
.IX Item "new ()"
Creates a new \f(CW\*(C`IO::Handle\*(C'\fR object.
.IP "new_from_fd ( FD, MODE )" 4
.IX Item "new_from_fd ( FD, MODE )"
Creates an \f(CW\*(C`IO::Handle\*(C'\fR like \f(CW\*(C`new\*(C'\fR does.
It requires two parameters, which are passed to the method \f(CW\*(C`fdopen\*(C'\fR;
if the fdopen fails, the object is destroyed. Otherwise, it is returned
to the caller.
.SH METHODS
.IX Header "METHODS"
See perlfunc for complete descriptions of each of the following
supported \f(CW\*(C`IO::Handle\*(C'\fR methods, which are just front ends for the
corresponding built-in functions:
.PP
.Vb 10
\& $io\->close
\& $io\->eof
\& $io\->fcntl( FUNCTION, SCALAR )
\& $io\->fileno
\& $io\->format_write( [FORMAT_NAME] )
\& $io\->getc
\& $io\->ioctl( FUNCTION, SCALAR )
\& $io\->read ( BUF, LEN, [OFFSET] )
\& $io\->print ( ARGS )
\& $io\->printf ( FMT, [ARGS] )
\& $io\->say ( ARGS )
\& $io\->stat
\& $io\->sysread ( BUF, LEN, [OFFSET] )
\& $io\->syswrite ( BUF, [LEN, [OFFSET]] )
\& $io\->truncate ( LEN )
.Ve
.PP
See perlvar for complete descriptions of each of the following
supported \f(CW\*(C`IO::Handle\*(C'\fR methods. All of them return the previous
value of the attribute and takes an optional single argument that when
given will set the value. If no argument is given the previous value
is unchanged (except for \f(CW$io\fR\->autoflush will actually turn ON
autoflush by default).
.PP
.Vb 7
\& $io\->autoflush ( [BOOL] ) $|
\& $io\->format_page_number( [NUM] ) $%
\& $io\->format_lines_per_page( [NUM] ) $=
\& $io\->format_lines_left( [NUM] ) $\-
\& $io\->format_name( [STR] ) $~
\& $io\->format_top_name( [STR] ) $^
\& $io\->input_line_number( [NUM]) $.
.Ve
.PP
The following methods are not supported on a per-filehandle basis.
.PP
.Vb 4
\& IO::Handle\->format_line_break_characters( [STR] ) $:
\& IO::Handle\->format_formfeed( [STR]) $^L
\& IO::Handle\->output_field_separator( [STR] ) $,
\& IO::Handle\->output_record_separator( [STR] ) $\e
\&
\& IO::Handle\->input_record_separator( [STR] ) $/
.Ve
.PP
Furthermore, for doing normal I/O you might need these:
.ie n .IP "$io\->fdopen ( FD, MODE )" 4
.el .IP "\f(CW$io\fR\->fdopen ( FD, MODE )" 4
.IX Item "$io->fdopen ( FD, MODE )"
\&\f(CW\*(C`fdopen\*(C'\fR is like an ordinary \f(CW\*(C`open\*(C'\fR except that its first parameter
is not a filename but rather a file handle name, an IO::Handle object,
or a file descriptor number. (For the documentation of the \f(CW\*(C`open\*(C'\fR
method, see IO::File.)
.ie n .IP $io\->opened 4
.el .IP \f(CW$io\fR\->opened 4
.IX Item "$io->opened"
Returns true if the object is currently a valid file descriptor, false
otherwise.
.ie n .IP $io\->getline 4
.el .IP \f(CW$io\fR\->getline 4
.IX Item "$io->getline"
This works like <$io> described in "I/O Operators" in perlop
except that it's more readable and can be safely called in a
list context but still returns just one line. If used as the conditional
within a \f(CW\*(C`while\*(C'\fR or C\-style \f(CW\*(C`for\*(C'\fR loop, however, you will need to
emulate the functionality of <$io> with \f(CW\*(C`defined($_ = $io\->getline)\*(C'\fR.
.ie n .IP $io\->getlines 4
.el .IP \f(CW$io\fR\->getlines 4
.IX Item "$io->getlines"
This works like <$io> when called in a list context to read all
the remaining lines in a file, except that it's more readable.
It will also \fBcroak()\fR if accidentally called in a scalar context.
.ie n .IP "$io\->ungetc ( ORD )" 4
.el .IP "\f(CW$io\fR\->ungetc ( ORD )" 4
.IX Item "$io->ungetc ( ORD )"
Pushes a character with the given ordinal value back onto the given
handle's input stream. Only one character of pushback per handle is
guaranteed.
.ie n .IP "$io\->write ( BUF, LEN [, OFFSET ] )" 4
.el .IP "\f(CW$io\fR\->write ( BUF, LEN [, OFFSET ] )" 4
.IX Item "$io->write ( BUF, LEN [, OFFSET ] )"
This \f(CW\*(C`write\*(C'\fR is somewhat like \f(CW\*(C`write\*(C'\fR found in C, in that it is the
opposite of read. The wrapper for the perl \f(CW\*(C`write\*(C'\fR function is
called \f(CW\*(C`format_write\*(C'\fR. However, whilst the C \f(CW\*(C`write\*(C'\fR function returns
the number of bytes written, this \f(CW\*(C`write\*(C'\fR function simply returns true
if successful (like \f(CW\*(C`print\*(C'\fR). A more C\-like \f(CW\*(C`write\*(C'\fR is \f(CW\*(C`syswrite\*(C'\fR
(see above).
.ie n .IP $io\->error 4
.el .IP \f(CW$io\fR\->error 4
.IX Item "$io->error"
Returns a true value if the given handle has experienced any errors
since it was opened or since the last call to \f(CW\*(C`clearerr\*(C'\fR, or if the
handle is invalid. It only returns false for a valid handle with no
outstanding errors.
.ie n .IP $io\->clearerr 4
.el .IP \f(CW$io\fR\->clearerr 4
.IX Item "$io->clearerr"
Clear the given handle's error indicator. Returns \-1 if the handle is
invalid, 0 otherwise.
.ie n .IP $io\->sync 4
.el .IP \f(CW$io\fR\->sync 4
.IX Item "$io->sync"
\&\f(CW\*(C`sync\*(C'\fR synchronizes a file's in-memory state with that on the
physical medium. \f(CW\*(C`sync\*(C'\fR does not operate at the perlio api level, but
operates on the file descriptor (similar to sysread, sysseek and
systell). This means that any data held at the perlio api level will not
be synchronized. To synchronize data that is buffered at the perlio api
level you must use the flush method. \f(CW\*(C`sync\*(C'\fR is not implemented on all
platforms. Returns "0 but true" on success, \f(CW\*(C`undef\*(C'\fR on error, \f(CW\*(C`undef\*(C'\fR
for an invalid handle. See \fBfsync\fR\|(3c).
.ie n .IP $io\->flush 4
.el .IP \f(CW$io\fR\->flush 4
.IX Item "$io->flush"
\&\f(CW\*(C`flush\*(C'\fR causes perl to flush any buffered data at the perlio api level.
Any unread data in the buffer will be discarded, and any unwritten data
will be written to the underlying file descriptor. Returns "0 but true"
on success, \f(CW\*(C`undef\*(C'\fR on error.
.ie n .IP "$io\->printflush ( ARGS )" 4
.el .IP "\f(CW$io\fR\->printflush ( ARGS )" 4
.IX Item "$io->printflush ( ARGS )"
Turns on autoflush, print ARGS and then restores the autoflush status of the
\&\f(CW\*(C`IO::Handle\*(C'\fR object. Returns the return value from print.
.ie n .IP "$io\->blocking ( [ BOOL ] )" 4
.el .IP "\f(CW$io\fR\->blocking ( [ BOOL ] )" 4
.IX Item "$io->blocking ( [ BOOL ] )"
If called with an argument \f(CW\*(C`blocking\*(C'\fR will turn on non-blocking IO if
\&\f(CW\*(C`BOOL\*(C'\fR is false, and turn it off if \f(CW\*(C`BOOL\*(C'\fR is true.
.Sp
\&\f(CW\*(C`blocking\*(C'\fR will return the value of the previous setting, or the
current setting if \f(CW\*(C`BOOL\*(C'\fR is not given.
.Sp
If an error occurs \f(CW\*(C`blocking\*(C'\fR will return undef and \f(CW$!\fR will be set.
.PP
If the C functions \fBsetbuf()\fR and/or \fBsetvbuf()\fR are available, then
\&\f(CW\*(C`IO::Handle::setbuf\*(C'\fR and \f(CW\*(C`IO::Handle::setvbuf\*(C'\fR set the buffering
policy for an IO::Handle. The calling sequences for the Perl functions
are the same as their C counterparts\-\-including the constants \f(CW\*(C`_IOFBF\*(C'\fR,
\&\f(CW\*(C`_IOLBF\*(C'\fR, and \f(CW\*(C`_IONBF\*(C'\fR for \fBsetvbuf()\fR\-\-except that the buffer parameter
specifies a scalar variable to use as a buffer. You should only
change the buffer before any I/O, or immediately after calling flush.
.PP
WARNING: The \fBIO::Handle::setvbuf()\fR is not available by default on
Perls 5.8.0 and later because \fBsetvbuf()\fR is rather specific to using
the stdio library, while Perl prefers the new perlio subsystem instead.
.PP
WARNING: A variable used as a buffer by \f(CW\*(C`setbuf\*(C'\fR or \f(CW\*(C`setvbuf\*(C'\fR \fBmust not
be modified\fR in any way until the IO::Handle is closed or \f(CW\*(C`setbuf\*(C'\fR or
\&\f(CW\*(C`setvbuf\*(C'\fR is called again, or memory corruption may result! Remember that
the order of global destruction is undefined, so even if your buffer
variable remains in scope until program termination, it may be undefined
before the file IO::Handle is closed. Note that you need to import the
constants \f(CW\*(C`_IOFBF\*(C'\fR, \f(CW\*(C`_IOLBF\*(C'\fR, and \f(CW\*(C`_IONBF\*(C'\fR explicitly. Like C, setbuf
returns nothing. setvbuf returns "0 but true", on success, \f(CW\*(C`undef\*(C'\fR on
failure.
.PP
Lastly, there is a special method for working under \fB\-T\fR and setuid/gid
scripts:
.ie n .IP $io\->untaint 4
.el .IP \f(CW$io\fR\->untaint 4
.IX Item "$io->untaint"
Marks the object as taint-clean, and as such data read from it will also
be considered taint-clean. Note that this is a very trusting action to
take, and appropriate consideration for the data source and potential
vulnerability should be kept in mind. Returns 0 on success, \-1 if setting
the taint-clean flag failed. (eg invalid handle)
.SH NOTE
.IX Header "NOTE"
An \f(CW\*(C`IO::Handle\*(C'\fR object is a reference to a symbol/GLOB reference (see
the Symbol package). Some modules that
inherit from \f(CW\*(C`IO::Handle\*(C'\fR may want to keep object related variables
in the hash table part of the GLOB. In an attempt to prevent modules
trampling on each other I propose the that any such module should prefix
its variables with its own name separated by _'s. For example the IO::Socket
module keeps a \f(CW\*(C`timeout\*(C'\fR variable in 'io_socket_timeout'.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
perlfunc,
"I/O Operators" in perlop,
IO::File
.SH BUGS
.IX Header "BUGS"
Due to backwards compatibility, all filehandles resemble objects
of class \f(CW\*(C`IO::Handle\*(C'\fR, or actually classes derived from that class.
They actually aren't. Which means you can't derive your own
class from \f(CW\*(C`IO::Handle\*(C'\fR and inherit those methods.
.SH HISTORY
.IX Header "HISTORY"
Derived from FileHandle.pm by Graham Barr <\fIgbarr@pobox.com\fR>
|