1IO::Handle(3pm)        Perl Programmers Reference Guide        IO::Handle(3pm)
2
3
4

NAME

6       IO::Handle - supply object methods for I/O handles
7

SYNOPSIS

9           use IO::Handle;
10
11           $io = IO::Handle->new();
12           if ($io->fdopen(fileno(STDIN),"r")) {
13               print $io->getline;
14               $io->close;
15           }
16
17           $io = IO::Handle->new();
18           if ($io->fdopen(fileno(STDOUT),"w")) {
19               $io->print("Some text\n");
20           }
21
22           # setvbuf is not available by default on Perls 5.8.0 and later.
23           use IO::Handle '_IOLBF';
24           $io->setvbuf($buffer_var, _IOLBF, 1024);
25
26           undef $io;       # automatically closes the file if it's open
27
28           autoflush STDOUT 1;
29

DESCRIPTION

31       "IO::Handle" is the base class for all other IO handle classes. It is
32       not intended that objects of "IO::Handle" would be created directly,
33       but instead "IO::Handle" is inherited from by several other classes in
34       the IO hierarchy.
35
36       If you are reading this documentation, looking for a replacement for
37       the "FileHandle" package, then I suggest you read the documentation for
38       "IO::File" too.
39

CONSTRUCTOR

41       new ()
42           Creates a new "IO::Handle" object.
43
44       new_from_fd ( FD, MODE )
45           Creates an "IO::Handle" like "new" does.  It requires two
46           parameters, which are passed to the method "fdopen"; if the fdopen
47           fails, the object is destroyed. Otherwise, it is returned to the
48           caller.
49

METHODS

51       See perlfunc for complete descriptions of each of the following
52       supported "IO::Handle" methods, which are just front ends for the
53       corresponding built-in functions:
54
55           $io->close
56           $io->eof
57           $io->fcntl( FUNCTION, SCALAR )
58           $io->fileno
59           $io->format_write( [FORMAT_NAME] )
60           $io->getc
61           $io->ioctl( FUNCTION, SCALAR )
62           $io->read ( BUF, LEN, [OFFSET] )
63           $io->print ( ARGS )
64           $io->printf ( FMT, [ARGS] )
65           $io->say ( ARGS )
66           $io->stat
67           $io->sysread ( BUF, LEN, [OFFSET] )
68           $io->syswrite ( BUF, [LEN, [OFFSET]] )
69           $io->truncate ( LEN )
70
71       See perlvar for complete descriptions of each of the following
72       supported "IO::Handle" methods.  All of them return the previous value
73       of the attribute and takes an optional single argument that when given
74       will set the value.  If no argument is given the previous value is
75       unchanged (except for $io->autoflush will actually turn ON autoflush by
76       default).
77
78           $io->autoflush ( [BOOL] )                         $|
79           $io->format_page_number( [NUM] )                  $%
80           $io->format_lines_per_page( [NUM] )               $=
81           $io->format_lines_left( [NUM] )                   $-
82           $io->format_name( [STR] )                         $~
83           $io->format_top_name( [STR] )                     $^
84           $io->input_line_number( [NUM])                    $.
85
86       The following methods are not supported on a per-filehandle basis.
87
88           IO::Handle->format_line_break_characters( [STR] ) $:
89           IO::Handle->format_formfeed( [STR])               $^L
90           IO::Handle->output_field_separator( [STR] )       $,
91           IO::Handle->output_record_separator( [STR] )      $\
92
93           IO::Handle->input_record_separator( [STR] )       $/
94
95       Furthermore, for doing normal I/O you might need these:
96
97       $io->fdopen ( FD, MODE )
98           "fdopen" is like an ordinary "open" except that its first parameter
99           is not a filename but rather a file handle name, an IO::Handle
100           object, or a file descriptor number.  (For the documentation of the
101           "open" method, see IO::File.)
102
103       $io->opened
104           Returns true if the object is currently a valid file descriptor,
105           false otherwise.
106
107       $io->getline
108           This works like <$io> described in "I/O Operators" in perlop except
109           that it's more readable and can be safely called in a list context
110           but still returns just one line.  If used as the conditional within
111           a "while" or C-style "for" loop, however, you will need to emulate
112           the functionality of <$io> with "defined($_ = $io->getline)".
113
114       $io->getlines
115           This works like <$io> when called in a list context to read all the
116           remaining lines in a file, except that it's more readable.  It will
117           also croak() if accidentally called in a scalar context.
118
119       $io->ungetc ( ORD )
120           Pushes a character with the given ordinal value back onto the given
121           handle's input stream.  Only one character of pushback per handle
122           is guaranteed.
123
124       $io->write ( BUF, LEN [, OFFSET ] )
125           This "write" is somewhat like "write" found in C, in that it is the
126           opposite of read. The wrapper for the perl "write" function is
127           called "format_write". However, whilst the C "write" function
128           returns the number of bytes written, this "write" function simply
129           returns true if successful (like "print"). A more C-like "write" is
130           "syswrite" (see above).
131
132       $io->error
133           Returns a true value if the given handle has experienced any errors
134           since it was opened or since the last call to "clearerr", or if the
135           handle is invalid. It only returns false for a valid handle with no
136           outstanding errors.
137
138       $io->clearerr
139           Clear the given handle's error indicator. Returns -1 if the handle
140           is invalid, 0 otherwise.
141
142       $io->sync
143           "sync" synchronizes a file's in-memory state  with  that  on the
144           physical medium. "sync" does not operate at the perlio api level,
145           but operates on the file descriptor (similar to sysread, sysseek
146           and systell). This means that any data held at the perlio api level
147           will not be synchronized. To synchronize data that is buffered at
148           the perlio api level you must use the flush method. "sync" is not
149           implemented on all platforms. Returns "0 but true" on success,
150           "undef" on error, "undef" for an invalid handle. See fsync(3c).
151
152       $io->flush
153           "flush" causes perl to flush any buffered data at the perlio api
154           level.  Any unread data in the buffer will be discarded, and any
155           unwritten data will be written to the underlying file descriptor.
156           Returns "0 but true" on success, "undef" on error.
157
158       $io->printflush ( ARGS )
159           Turns on autoflush, print ARGS and then restores the autoflush
160           status of the "IO::Handle" object. Returns the return value from
161           print.
162
163       $io->blocking ( [ BOOL ] )
164           If called with an argument "blocking" will turn on non-blocking IO
165           if "BOOL" is false, and turn it off if "BOOL" is true.
166
167           "blocking" will return the value of the previous setting, or the
168           current setting if "BOOL" is not given.
169
170           If an error occurs "blocking" will return undef and $! will be set.
171
172       If the C functions setbuf() and/or setvbuf() are available, then
173       "IO::Handle::setbuf" and "IO::Handle::setvbuf" set the buffering policy
174       for an IO::Handle.  The calling sequences for the Perl functions are
175       the same as their C counterparts--including the constants "_IOFBF",
176       "_IOLBF", and "_IONBF" for setvbuf()--except that the buffer parameter
177       specifies a scalar variable to use as a buffer. You should only change
178       the buffer before any I/O, or immediately after calling flush.
179
180       WARNING: The IO::Handle::setvbuf() is not available by default on Perls
181       5.8.0 and later because setvbuf() is rather specific to using the stdio
182       library, while Perl prefers the new perlio subsystem instead.
183
184       WARNING: A variable used as a buffer by "setbuf" or "setvbuf" must not
185       be modified in any way until the IO::Handle is closed or "setbuf" or
186       "setvbuf" is called again, or memory corruption may result! Remember
187       that the order of global destruction is undefined, so even if your
188       buffer variable remains in scope until program termination, it may be
189       undefined before the file IO::Handle is closed. Note that you need to
190       import the constants "_IOFBF", "_IOLBF", and "_IONBF" explicitly. Like
191       C, setbuf returns nothing. setvbuf returns "0 but true", on success,
192       "undef" on failure.
193
194       Lastly, there is a special method for working under -T and setuid/gid
195       scripts:
196
197       $io->untaint
198           Marks the object as taint-clean, and as such data read from it will
199           also be considered taint-clean. Note that this is a very trusting
200           action to take, and appropriate consideration for the data source
201           and potential vulnerability should be kept in mind. Returns 0 on
202           success, -1 if setting the taint-clean flag failed. (eg invalid
203           handle)
204

NOTE

206       An "IO::Handle" object is a reference to a symbol/GLOB reference (see
207       the "Symbol" package).  Some modules that inherit from "IO::Handle" may
208       want to keep object related variables in the hash table part of the
209       GLOB. In an attempt to prevent modules trampling on each other I
210       propose the that any such module should prefix its variables with its
211       own name separated by _'s. For example the IO::Socket module keeps a
212       "timeout" variable in 'io_socket_timeout'.
213

SEE ALSO

215       perlfunc, "I/O Operators" in perlop, IO::File
216

BUGS

218       Due to backwards compatibility, all filehandles resemble objects of
219       class "IO::Handle", or actually classes derived from that class.  They
220       actually aren't.  Which means you can't derive your own class from
221       "IO::Handle" and inherit those methods.
222

HISTORY

224       Derived from FileHandle.pm by Graham Barr <gbarr@pobox.com>
225
226
227
228perl v5.32.1                      2021-05-31                   IO::Handle(3pm)
Impressum