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