Debian Codegraph @

upstream/mageia-cauldron/man3pm/IO::Handle.3pm

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