7 IO::Handle - supply object methods for filehandles
14 if ($fh->open "< file") {
19 $fh = new IO::Handle "> FOO";
25 $fh = new IO::Handle "file", "r";
28 undef $fh; # automatically closes the file
31 $fh = new IO::Handle "file", O_WRONLY|O_APPEND;
34 undef $fh; # automatically closes the file
40 $fh->setvbuf($buffer_var, _IOLBF, 1024);
46 C<IO::Handle::new> creates a C<IO::Handle>, which is a reference to a
47 newly created symbol (see the C<Symbol> package). If it receives any
48 parameters, they are passed to C<IO::Handle::open>; if the open fails,
49 the C<IO::Handle> object is destroyed. Otherwise, it is returned to
52 C<IO::Handle::new_from_fd> creates a C<IO::Handle> like C<new> does.
53 It requires two parameters, which are passed to C<IO::Handle::fdopen>;
54 if the fdopen fails, the C<IO::Handle> object is destroyed.
55 Otherwise, it is returned to the caller.
57 C<IO::Handle::open> accepts one parameter or two. With one parameter,
58 it is just a front end for the built-in C<open> function. With two
59 parameters, the first parameter is a filename that may include
60 whitespace or other special characters, and the second parameter is
61 the open mode in either Perl form (">", "+<", etc.) or POSIX form
64 C<IO::Handle::fdopen> is like C<open> except that its first parameter
65 is not a filename but rather a file handle name, a IO::Handle object,
66 or a file descriptor number.
68 C<IO::Handle::write> is like C<write> found in C, that is it is the
69 opposite of read. The wrapper for the perl C<write> function is
70 called C<format_write>.
72 C<IO::Handle::opened> returns true if the object is currently a valid
75 If the C functions fgetpos() and fsetpos() are available, then
76 C<IO::Handle::getpos> returns an opaque value that represents the
77 current position of the IO::Handle, and C<IO::Handle::setpos> uses
78 that value to return to a previously visited position.
80 If the C function setvbuf() is available, then C<IO::Handle::setvbuf>
81 sets the buffering policy for the IO::Handle. The calling sequence
82 for the Perl function is the same as its C counterpart, including the
83 macros C<_IOFBF>, C<_IOLBF>, and C<_IONBF>, except that the buffer
84 parameter specifies a scalar variable to use as a buffer. WARNING: A
85 variable used as a buffer by C<IO::Handle::setvbuf> must not be
86 modified in any way until the IO::Handle is closed or until
87 C<IO::Handle::setvbuf> is called again, or memory corruption may
90 See L<perlfunc> for complete descriptions of each of the following
91 supported C<IO::Handle> methods, which are just front ends for the
92 corresponding built-in functions:
103 See L<perlvar> for complete descriptions of each of the following
104 supported C<IO::Handle> methods:
107 output_field_separator
108 output_record_separator
109 input_record_separator
112 format_lines_per_page
116 format_line_break_characters
120 Furthermore, for doing normal I/O you might need these:
126 See L<perlfunc/print>.
130 See L<perlfunc/printf>.
134 This works like <$fh> described in L<perlop/"I/O Operators">
135 except that it's more readable and can be safely called in an
136 array context but still returns just one line.
140 This works like <$fh> when called in an array context to
141 read all the remaining lines in a file, except that it's more readable.
142 It will also croak() if accidentally called in a scalar context.
148 The reference returned from new is a GLOB reference. Some modules that
149 inherit from C<IO::Handle> may want to keep object related variables
150 in the hash table part of the GLOB. In an attempt to prevent modules
151 trampling on each other I propose the that any such module should prefix
152 its variables with its own name separated by _'s. For example the IO::Socket
153 module keeps a C<timeout> variable in 'io_socket_timeout'.
158 L<perlop/"I/O Operators">,
159 L<POSIX/"FileHandle">
163 Due to backwards compatibility, all filehandles resemble objects
164 of class C<IO::Handle>, or actually classes derived from that class.
165 They actually aren't. Which means you can't derive your own
166 class from C<IO::Handle> and inherit those methods.
170 Derived from FileHandle.pm by Graham Barr <bodg@tiuk.ti.com>
175 use vars qw($VERSION @EXPORT_OK $AUTOLOAD);
184 ## TEMPORARY workaround as perl expects handles to be <FileHandle> objects
186 @FileHandle::ISA = qw(IO::Handle);
189 $VERSION = sprintf("%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/);
193 output_field_separator
194 output_record_separator
195 input_record_separator
198 format_lines_per_page
202 format_line_break_characters
222 ################################################
223 ## Interaction with the XS.
227 @IO::ISA = qw(DynaLoader);
228 bootstrap IO $VERSION;
231 if ($AUTOLOAD =~ /::(_?[a-z])/) {
232 $AutoLoader::AUTOLOAD = $AUTOLOAD;
233 goto &AutoLoader::AUTOLOAD
235 my $constname = $AUTOLOAD;
236 $constname =~ s/.*:://;
237 my $val = constant($constname);
238 defined $val or croak "$constname is not a valid IO::Handle macro";
239 *$AUTOLOAD = sub { $val };
244 ################################################
245 ## Constructors, destructors.
249 @_ == 1 or croak 'usage: new IO::Handle';
250 my $class = ref($_[0]) || $_[0];
256 @_ == 3 or croak 'usage: new_from_fd IO::Handle FD, MODE';
259 IO::Handle::fdopen($fh, @_)
266 # FileHandle::DESTROY use to call close(). This creates a problem
267 # if 2 Handle objects have the same fd. sv_clear will call io close
268 # when the refcount in the xpvio becomes zero.
270 # It is defined as empty to stop AUTOLOAD being called :-)
274 ################################################
278 sub _open_mode_string {
280 $mode =~ /^\+?(<|>>?)$/
281 or $mode =~ s/^r(\+?)$/$1</
282 or $mode =~ s/^w(\+?)$/$1>/
283 or $mode =~ s/^a(\+?)$/$1>>/
284 or croak "IO::Handle: bad open mode: $mode";
289 @_ == 3 or croak 'usage: $fh->fdopen(FD, MODE)';
290 my ($fh, $fd, $mode) = @_;
293 if (ref($fd) && "".$fd =~ /GLOB\(/o) {
294 # It's a glob reference; Alias it as we cannot get name of anon GLOBs
295 my $n = qualify(*GLOB);
298 } elsif ($fd =~ m#^\d+$#) {
299 # It's an FD number; prefix with "=".
303 open($fh, _open_mode_string($mode) . '&' . $fd)
308 @_ == 1 or croak 'usage: $fh->close()';
312 # This may seem as though it should be in IO::Pipe, but the
313 # object gets blessed out of IO::Pipe when reader/writer is called
314 waitpid(${*$fh}{'io_pipe_pid'},0)
315 if(defined ${*$fh}{'io_pipe_pid'});
320 ################################################
321 ## Normal I/O functions.
332 @_ == 1 or croak 'usage: $fh->opened()';
333 defined fileno($_[0]);
337 @_ == 1 or croak 'usage: $fh->fileno()';
342 @_ == 1 or croak 'usage: $fh->getc()';
347 @_ == 1 or croak 'usage: $fh->gets()';
353 @_ == 1 or croak 'usage: $fh->eof()';
358 @_ or croak 'usage: $fh->print([ARGS])';
364 @_ >= 2 or croak 'usage: $fh->printf(FMT,[ARGS])';
370 @_ == 1 or croak 'usage: $fh->getline';
372 return scalar <$this>;
376 @_ == 1 or croak 'usage: $fh->getline()';
379 croak "Can't call IO::Handle::getlines in a scalar context, use IO::Handle::getline";
384 @_ == 2 or croak 'usage: $fh->truncate(LEN)';
385 truncate($_[0], $_[1]);
389 @_ == 3 || @_ == 4 or croak '$fh->read(BUF, LEN [, OFFSET])';
390 read($_[0], $_[1], $_[2], $_[3] || 0);
394 @_ == 3 || @_ == 4 or croak '$fh->write(BUF, LEN [, OFFSET])';
396 print { $_[0] } substr($_[1], $_[3] || 0, $_[2]);
400 @_ == 1 or croak 'usage: $fh->stat()';
404 ################################################
405 ## State modification functions.
409 my $old = new SelectSaver qualify($_[0], caller);
411 $| = @_ > 1 ? $_[1] : 1;
415 sub output_field_separator {
416 my $old = new SelectSaver qualify($_[0], caller);
418 $, = $_[1] if @_ > 1;
422 sub output_record_separator {
423 my $old = new SelectSaver qualify($_[0], caller);
425 $\ = $_[1] if @_ > 1;
429 sub input_record_separator {
430 my $old = new SelectSaver qualify($_[0], caller);
432 $/ = $_[1] if @_ > 1;
436 sub input_line_number {
437 my $old = new SelectSaver qualify($_[0], caller);
439 $. = $_[1] if @_ > 1;
443 sub format_page_number {
444 my $old = new SelectSaver qualify($_[0], caller);
446 $% = $_[1] if @_ > 1;
450 sub format_lines_per_page {
451 my $old = new SelectSaver qualify($_[0], caller);
453 $= = $_[1] if @_ > 1;
457 sub format_lines_left {
458 my $old = new SelectSaver qualify($_[0], caller);
460 $- = $_[1] if @_ > 1;
465 my $old = new SelectSaver qualify($_[0], caller);
467 $~ = qualify($_[1], caller) if @_ > 1;
471 sub format_top_name {
472 my $old = new SelectSaver qualify($_[0], caller);
474 $^ = qualify($_[1], caller) if @_ > 1;
478 sub format_line_break_characters {
479 my $old = new SelectSaver qualify($_[0], caller);
481 $: = $_[1] if @_ > 1;
485 sub format_formfeed {
486 my $old = new SelectSaver qualify($_[0], caller);
488 $^L = $_[1] if @_ > 1;
497 formline($picture, @_);
502 @_ < 3 || croak 'usage: $fh->write( [FORMAT_NAME] )';
505 my $oldfmt = $fh->format_name($fmt);
507 $fh->format_name($oldfmt);