7 IO::Handle - supply object methods for I/O handles
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> is the base class for all other IO handle classes.
47 A C<IO::Handle> object is a reference to a symbol (see the C<Symbol> package)
55 Creates a new C<IO::Handle> object.
57 =item new_from_fd ( FD, MODE )
59 Creates a C<IO::Handle> like C<new> does.
60 It requires two parameters, which are passed to the method C<fdopen>;
61 if the fdopen fails, the object is destroyed. Otherwise, it is returned
68 If the C function setvbuf() is available, then C<IO::Handle::setvbuf>
69 sets the buffering policy for the IO::Handle. The calling sequence
70 for the Perl function is the same as its C counterpart, including the
71 macros C<_IOFBF>, C<_IOLBF>, and C<_IONBF>, except that the buffer
72 parameter specifies a scalar variable to use as a buffer. WARNING: A
73 variable used as a buffer by C<IO::Handle::setvbuf> must not be
74 modified in any way until the IO::Handle is closed or until
75 C<IO::Handle::setvbuf> is called again, or memory corruption may
78 See L<perlfunc> for complete descriptions of each of the following
79 supported C<IO::Handle> methods, which are just front ends for the
80 corresponding built-in functions:
95 See L<perlvar> for complete descriptions of each of the following
96 supported C<IO::Handle> methods:
99 output_field_separator
100 output_record_separator
101 input_record_separator
104 format_lines_per_page
108 format_line_break_characters
112 Furthermore, for doing normal I/O you might need these:
118 This works like <$fh> described in L<perlop/"I/O Operators">
119 except that it's more readable and can be safely called in an
120 array context but still returns just one line.
124 This works like <$fh> when called in an array context to
125 read all the remaining lines in a file, except that it's more readable.
126 It will also croak() if accidentally called in a scalar context.
128 =item $fh->fdopen ( FD, MODE )
130 C<fdopen> is like an ordinary C<open> except that its first parameter
131 is not a filename but rather a file handle name, a IO::Handle object,
132 or a file descriptor number.
134 =item $fh->write ( BUF, LEN [, OFFSET }\] )
136 C<write> is like C<write> found in C, that is it is the
137 opposite of read. The wrapper for the perl C<write> function is
138 called C<format_write>.
142 Returns true if the object is currently a valid file descriptor.
146 Lastly, a special method for working under B<-T> and setuid/gid scripts:
152 Marks the object as taint-clean, and as such data read from it will also
153 be considered taint-clean. Note that this is a very trusting action to
154 take, and appropriate consideration for the data source and potential
155 vulnerability should be kept in mind.
161 A C<IO::Handle> object is a GLOB reference. Some modules that
162 inherit from C<IO::Handle> may want to keep object related variables
163 in the hash table part of the GLOB. In an attempt to prevent modules
164 trampling on each other I propose the that any such module should prefix
165 its variables with its own name separated by _'s. For example the IO::Socket
166 module keeps a C<timeout> variable in 'io_socket_timeout'.
171 L<perlop/"I/O Operators">,
172 L<POSIX/"FileHandle">
176 Due to backwards compatibility, all filehandles resemble objects
177 of class C<IO::Handle>, or actually classes derived from that class.
178 They actually aren't. Which means you can't derive your own
179 class from C<IO::Handle> and inherit those methods.
183 Derived from FileHandle.pm by Graham Barr E<lt>F<bodg@tiuk.ti.com>E<gt>
188 use vars qw($RCS $VERSION @EXPORT_OK $AUTOLOAD);
197 ## TEMPORARY workaround as perl expects handles to be <FileHandle> objects
199 @FileHandle::ISA = qw(IO::Handle);
202 $RCS = sprintf("%s", q$Revision: 1.15 $ =~ /([\d\.]+)/);
206 output_field_separator
207 output_record_separator
208 input_record_separator
211 format_lines_per_page
215 format_line_break_characters
235 ################################################
236 ## Interaction with the XS.
240 @IO::ISA = qw(DynaLoader);
241 bootstrap IO $VERSION;
244 if ($AUTOLOAD =~ /::(_?[a-z])/) {
245 $AutoLoader::AUTOLOAD = $AUTOLOAD;
246 goto &AutoLoader::AUTOLOAD
248 my $constname = $AUTOLOAD;
249 $constname =~ s/.*:://;
250 my $val = constant($constname);
251 defined $val or croak "$constname is not a valid IO::Handle macro";
252 *$AUTOLOAD = sub { $val };
257 ################################################
258 ## Constructors, destructors.
262 my $class = ref($_[0]) || $_[0] || "IO::Handle";
263 @_ == 1 or croak "usage: new $class";
269 my $class = ref($_[0]) || $_[0] || "IO::Handle";
270 @_ == 3 or croak "usage: new_from_fd $class FD, MODE";
272 IO::Handle::fdopen($fh, @_)
280 # During global object destruction, this function may be called
281 # on FILEHANDLEs as well as on the GLOBs that contains them.
282 # Thus the following trickery. If only the CORE file operators
283 # could deal with FILEHANDLEs, it wouldn't be necessary...
285 if ($fh =~ /=FILEHANDLE\(/) {
288 if defined fileno(TMP);
292 if defined fileno($fh);
296 ################################################
300 sub _open_mode_string {
302 $mode =~ /^\+?(<|>>?)$/
303 or $mode =~ s/^r(\+?)$/$1</
304 or $mode =~ s/^w(\+?)$/$1>/
305 or $mode =~ s/^a(\+?)$/$1>>/
306 or croak "IO::Handle: bad open mode: $mode";
311 @_ == 3 or croak 'usage: $fh->fdopen(FD, MODE)';
312 my ($fh, $fd, $mode) = @_;
315 if (ref($fd) && "".$fd =~ /GLOB\(/o) {
316 # It's a glob reference; Alias it as we cannot get name of anon GLOBs
317 my $n = qualify(*GLOB);
320 } elsif ($fd =~ m#^\d+$#) {
321 # It's an FD number; prefix with "=".
325 open($fh, _open_mode_string($mode) . '&' . $fd)
330 @_ == 1 or croak 'usage: $fh->close()';
334 # This may seem as though it should be in IO::Pipe, but the
335 # object gets blessed out of IO::Pipe when reader/writer is called
336 waitpid(${*$fh}{'io_pipe_pid'},0)
337 if(defined ${*$fh}{'io_pipe_pid'});
342 ################################################
343 ## Normal I/O functions.
350 @_ == 1 or croak 'usage: $fh->opened()';
351 defined fileno($_[0]);
355 @_ == 1 or croak 'usage: $fh->fileno()';
360 @_ == 1 or croak 'usage: $fh->getc()';
365 @_ == 1 or croak 'usage: $fh->gets()';
371 @_ == 1 or croak 'usage: $fh->eof()';
376 @_ or croak 'usage: $fh->print([ARGS])';
382 @_ >= 2 or croak 'usage: $fh->printf(FMT,[ARGS])';
388 @_ == 1 or croak 'usage: $fh->getline';
390 return scalar <$this>;
394 @_ == 1 or croak 'usage: $fh->getline()';
396 croak 'Can\'t call $fh->getlines in a scalar context, use $fh->getline';
402 @_ == 2 or croak 'usage: $fh->truncate(LEN)';
403 truncate($_[0], $_[1]);
407 @_ == 3 || @_ == 4 or croak '$fh->read(BUF, LEN [, OFFSET])';
408 read($_[0], $_[1], $_[2], $_[3] || 0);
412 @_ == 3 || @_ == 4 or croak '$fh->sysread(BUF, LEN [, OFFSET])';
413 sysread($_[0], $_[1], $_[2], $_[3] || 0);
417 @_ == 3 || @_ == 4 or croak '$fh->write(BUF, LEN [, OFFSET])';
419 print { $_[0] } substr($_[1], $_[3] || 0, $_[2]);
423 @_ == 3 || @_ == 4 or croak '$fh->syswrite(BUF, LEN [, OFFSET])';
424 sysread($_[0], $_[1], $_[2], $_[3] || 0);
428 @_ == 1 or croak 'usage: $fh->stat()';
432 ################################################
433 ## State modification functions.
437 my $old = new SelectSaver qualify($_[0], caller);
439 $| = @_ > 1 ? $_[1] : 1;
443 sub output_field_separator {
444 my $old = new SelectSaver qualify($_[0], caller);
446 $, = $_[1] if @_ > 1;
450 sub output_record_separator {
451 my $old = new SelectSaver qualify($_[0], caller);
453 $\ = $_[1] if @_ > 1;
457 sub input_record_separator {
458 my $old = new SelectSaver qualify($_[0], caller);
460 $/ = $_[1] if @_ > 1;
464 sub input_line_number {
465 my $old = new SelectSaver qualify($_[0], caller);
467 $. = $_[1] if @_ > 1;
471 sub format_page_number {
472 my $old = new SelectSaver qualify($_[0], caller);
474 $% = $_[1] if @_ > 1;
478 sub format_lines_per_page {
479 my $old = new SelectSaver qualify($_[0], caller);
481 $= = $_[1] if @_ > 1;
485 sub format_lines_left {
486 my $old = new SelectSaver qualify($_[0], caller);
488 $- = $_[1] if @_ > 1;
493 my $old = new SelectSaver qualify($_[0], caller);
495 $~ = qualify($_[1], caller) if @_ > 1;
499 sub format_top_name {
500 my $old = new SelectSaver qualify($_[0], caller);
502 $^ = qualify($_[1], caller) if @_ > 1;
506 sub format_line_break_characters {
507 my $old = new SelectSaver qualify($_[0], caller);
509 $: = $_[1] if @_ > 1;
513 sub format_formfeed {
514 my $old = new SelectSaver qualify($_[0], caller);
516 $^L = $_[1] if @_ > 1;
525 formline($picture, @_);
530 @_ < 3 || croak 'usage: $fh->write( [FORMAT_NAME] )';
533 my $oldfmt = $fh->format_name($fmt);
535 $fh->format_name($oldfmt);
542 @_ == 3 || croak 'usage: $fh->fcntl( OP, VALUE );';
543 my ($fh, $op, $val) = @_;
544 my $r = fcntl($fh, $op, $val);
545 defined $r && $r eq "0 but true" ? 0 : $r;
549 @_ == 3 || croak 'usage: $fh->ioctl( OP, VALUE );';
550 my ($fh, $op, $val) = @_;
551 my $r = ioctl($fh, $op, $val);
552 defined $r && $r eq "0 but true" ? 0 : $r;