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 = new IO::Handle;
12           if ($io->fdopen(fileno(STDIN),"r")) {
13               print $io->getline;
14               $io->close;
15           }
16
17           $io = new IO::Handle;
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
111           +within a "while" or C-style "for" loop, however, you will need to
112           +emulate the functionality of <$io> with "defined($_ =
113           $io->getline)".
114
115       $io->getlines
116           This works like <$io> when called in a list context to read all the
117           remaining lines in a file, except that it's more readable.  It will
118           also croak() if accidentally called in a scalar context.
119
120       $io->ungetc ( ORD )
121           Pushes a character with the given ordinal value back onto the given
122           handle's input stream.  Only one character of pushback per handle
123           is guaranteed.
124
125       $io->write ( BUF, LEN [, OFFSET ] )
126           This "write" is like "write" found in C, that is it is the opposite
127           of read. The wrapper for the perl "write" function is called
128           "format_write".
129
130       $io->error
131           Returns a true value if the given handle has experienced any errors
132           since it was opened or since the last call to "clearerr", or if the
133           handle is invalid. It only returns false for a valid handle with no
134           outstanding errors.
135
136       $io->clearerr
137           Clear the given handle's error indicator. Returns -1 if the handle
138           is invalid, 0 otherwise.
139
140       $io->sync
141           "sync" synchronizes a file's in-memory state  with  that  on the
142           physical medium. "sync" does not operate at the perlio api level,
143           but operates on the file descriptor (similar to sysread, sysseek
144           and systell). This means that any data held at the perlio api level
145           will not be synchronized. To synchronize data that is buffered at
146           the perlio api level you must use the flush method. "sync" is not
147           implemented on all platforms. Returns "0 but true" on success,
148           "undef" on error, "undef" for an invalid handle. See fsync(3c).
149
150       $io->flush
151           "flush" causes perl to flush any buffered data at the perlio api
152           level.  Any unread data in the buffer will be discarded, and any
153           unwritten data will be written to the underlying file descriptor.
154           Returns "0 but true" on success, "undef" on error.
155
156       $io->printflush ( ARGS )
157           Turns on autoflush, print ARGS and then restores the autoflush
158           status of the "IO::Handle" object. Returns the return value from
159           print.
160
161       $io->blocking ( [ BOOL ] )
162           If called with an argument "blocking" will turn on non-blocking IO
163           if "BOOL" is false, and turn it off if "BOOL" is true.
164
165           "blocking" will return the value of the previous setting, or the
166           current setting if "BOOL" is not given.
167
168           If an error occurs "blocking" will return undef and $! will be set.
169
170       If the C functions setbuf() and/or setvbuf() are available, then
171       "IO::Handle::setbuf" and "IO::Handle::setvbuf" set the buffering policy
172       for an IO::Handle.  The calling sequences for the Perl functions are
173       the same as their C counterparts--including the constants "_IOFBF",
174       "_IOLBF", and "_IONBF" for setvbuf()--except that the buffer parameter
175       specifies a scalar variable to use as a buffer. You should only change
176       the buffer before any I/O, or immediately after calling flush.
177
178       WARNING: The IO::Handle::setvbuf() is not available by default on Perls
179       5.8.0 and later because setvbuf() is rather specific to using the stdio
180       library, while Perl prefers the new perlio subsystem instead.
181
182       WARNING: A variable used as a buffer by "setbuf" or "setvbuf" must not
183       be modified in any way until the IO::Handle is closed or "setbuf" or
184       "setvbuf" is called again, or memory corruption may result! Remember
185       that the order of global destruction is undefined, so even if your
186       buffer variable remains in scope until program termination, it may be
187       undefined before the file IO::Handle is closed. Note that you need to
188       import the constants "_IOFBF", "_IOLBF", and "_IONBF" explicitly. Like
189       C, setbuf returns nothing. setvbuf returns "0 but true", on success,
190       "undef" on failure.
191
192       Lastly, there is a special method for working under -T and setuid/gid
193       scripts:
194
195       $io->untaint
196           Marks the object as taint-clean, and as such data read from it will
197           also be considered taint-clean. Note that this is a very trusting
198           action to take, and appropriate consideration for the data source
199           and potential vulnerability should be kept in mind. Returns 0 on
200           success, -1 if setting the taint-clean flag failed. (eg invalid
201           handle)
202

NOTE

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

SEE ALSO

213       perlfunc, "I/O Operators" in perlop, IO::File
214

BUGS

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

HISTORY

222       Derived from FileHandle.pm by Graham Barr <gbarr@pobox.com>
223
224
225
226perl v5.10.1                      2009-06-23                   IO::Handle(3pm)
Impressum