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.9 $ =~ /(\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, @_)
264 # FileHandle::DESTROY use to call close(). This creates a problem
265 # if 2 Handle objects have the same fd. sv_clear will call io close
266 # when the refcount in the xpvio becomes zero.
268 # It is defined as empty to stop AUTOLOAD being called :-)
272 ################################################
276 sub _open_mode_string {
278 $mode =~ /^\+?(<|>>?)$/
279 or $mode =~ s/^r(\+?)$/$1</
280 or $mode =~ s/^w(\+?)$/$1>/
281 or $mode =~ s/^a(\+?)$/$1>>/
282 or croak "IO::Handle: bad open mode: $mode";
287 @_ == 3 or croak 'usage: $fh->fdopen(FD, MODE)';
288 my ($fh, $fd, $mode) = @_;
291 if (ref($fd) && "".$fd =~ /GLOB\(/o) {
292 # It's a glob reference; Alias it as we cannot get name of anon GLOBs
293 my $n = qualify(*GLOB);
296 } elsif ($fd =~ m#^\d+$#) {
297 # It's an FD number; prefix with "=".
301 open($fh, _open_mode_string($mode) . '&' . $fd)
306 @_ == 1 or croak 'usage: $fh->close()';
310 # This may seem as though it should be in IO::Pipe, but the
311 # object gets blessed out of IO::Pipe when reader/writer is called
312 waitpid(${*$fh}{'io_pipe_pid'},0)
313 if(defined ${*$fh}{'io_pipe_pid'});
318 ################################################
319 ## Normal I/O functions.
330 @_ == 1 or croak 'usage: $fh->opened()';
331 defined fileno($_[0]);
335 @_ == 1 or croak 'usage: $fh->fileno()';
340 @_ == 1 or croak 'usage: $fh->getc()';
345 @_ == 1 or croak 'usage: $fh->gets()';
351 @_ == 1 or croak 'usage: $fh->eof()';
356 @_ or croak 'usage: $fh->print([ARGS])';
362 @_ >= 2 or croak 'usage: $fh->printf(FMT,[ARGS])';
368 @_ == 1 or croak 'usage: $fh->getline';
370 return scalar <$this>;
374 @_ == 1 or croak 'usage: $fh->getline()';
377 croak "Can't call IO::Handle::getlines in a scalar context, use IO::Handle::getline";
382 @_ == 2 or croak 'usage: $fh->truncate(LEN)';
383 truncate($_[0], $_[1]);
387 @_ == 3 || @_ == 4 or croak '$fh->read(BUF, LEN [, OFFSET])';
388 read($_[0], $_[1], $_[2], $_[3] || 0);
392 @_ == 3 || @_ == 4 or croak '$fh->write(BUF, LEN [, OFFSET])';
394 print { $_[0] } substr($_[1], $_[3] || 0, $_[2]);
398 @_ == 1 or croak 'usage: $fh->stat()';
402 ################################################
403 ## State modification functions.
407 my $old = new SelectSaver qualify($_[0], caller);
409 $| = @_ > 1 ? $_[1] : 1;
413 sub output_field_separator {
414 my $old = new SelectSaver qualify($_[0], caller);
416 $, = $_[1] if @_ > 1;
420 sub output_record_separator {
421 my $old = new SelectSaver qualify($_[0], caller);
423 $\ = $_[1] if @_ > 1;
427 sub input_record_separator {
428 my $old = new SelectSaver qualify($_[0], caller);
430 $/ = $_[1] if @_ > 1;
434 sub input_line_number {
435 my $old = new SelectSaver qualify($_[0], caller);
437 $. = $_[1] if @_ > 1;
441 sub format_page_number {
442 my $old = new SelectSaver qualify($_[0], caller);
444 $% = $_[1] if @_ > 1;
448 sub format_lines_per_page {
449 my $old = new SelectSaver qualify($_[0], caller);
451 $= = $_[1] if @_ > 1;
455 sub format_lines_left {
456 my $old = new SelectSaver qualify($_[0], caller);
458 $- = $_[1] if @_ > 1;
463 my $old = new SelectSaver qualify($_[0], caller);
465 $~ = qualify($_[1], caller) if @_ > 1;
469 sub format_top_name {
470 my $old = new SelectSaver qualify($_[0], caller);
472 $^ = qualify($_[1], caller) if @_ > 1;
476 sub format_line_break_characters {
477 my $old = new SelectSaver qualify($_[0], caller);
479 $: = $_[1] if @_ > 1;
483 sub format_formfeed {
484 my $old = new SelectSaver qualify($_[0], caller);
486 $^L = $_[1] if @_ > 1;
495 formline($picture, @_);
500 @_ < 3 || croak 'usage: $fh->write( [FORMAT_NAME] )';
503 my $oldfmt = $fh->format_name($fmt);
505 $fh->format_name($oldfmt);