6 IO::Handle - supply object methods for I/O handles
13 if ($fh->fdopen(fileno(STDIN),"r")) {
19 if ($fh->fdopen(fileno(STDOUT),"w")) {
20 $fh->print("Some text\n");
23 $fh->setvbuf($buffer_var, _IOLBF, 1024);
25 undef $fh; # automatically closes the file if it's open
31 C<IO::Handle> is the base class for all other IO handle classes. It is
32 not intended that objects of C<IO::Handle> would be created directly,
33 but instead C<IO::Handle> is inherited from by several other classes
36 If you are reading this documentation, looking for a replacement for
37 the C<FileHandle> package, then I suggest you read the documentation
40 A C<IO::Handle> object is a reference to a symbol (see the C<Symbol> package)
48 Creates a new C<IO::Handle> object.
50 =item new_from_fd ( FD, MODE )
52 Creates a C<IO::Handle> like C<new> does.
53 It requires two parameters, which are passed to the method C<fdopen>;
54 if the fdopen fails, the object is destroyed. Otherwise, it is returned
61 See L<perlfunc> for complete descriptions of each of the following
62 supported C<IO::Handle> methods, which are just front ends for the
63 corresponding built-in functions:
77 See L<perlvar> for complete descriptions of each of the following
78 supported C<IO::Handle> methods:
81 output_field_separator
82 output_record_separator
83 input_record_separator
90 format_line_break_characters
94 Furthermore, for doing normal I/O you might need these:
98 =item $fh->fdopen ( FD, MODE )
100 C<fdopen> is like an ordinary C<open> except that its first parameter
101 is not a filename but rather a file handle name, a IO::Handle object,
102 or a file descriptor number.
106 Returns true if the object is currently a valid file descriptor.
110 This works like <$fh> described in L<perlop/"I/O Operators">
111 except that it's more readable and can be safely called in an
112 array context but still returns just one line.
116 This works like <$fh> when called in an array context to
117 read all the remaining lines in a file, except that it's more readable.
118 It will also croak() if accidentally called in a scalar context.
120 =item $fh->ungetc ( ORD )
122 Pushes a character with the given ordinal value back onto the given
123 handle's input stream.
125 =item $fh->write ( BUF, LEN [, OFFSET }\] )
127 This C<write> is like C<write> found in C, that is it is the
128 opposite of read. The wrapper for the perl C<write> function is
129 called C<format_write>.
133 Flush the given handle's buffer.
137 Returns a true value if the given handle has experienced any errors
138 since it was opened or since the last call to C<clearerr>.
142 Clear the given handle's error indicator.
146 If the C functions setbuf() and/or setvbuf() are available, then
147 C<IO::Handle::setbuf> and C<IO::Handle::setvbuf> set the buffering
148 policy for an IO::Handle. The calling sequences for the Perl functions
149 are the same as their C counterparts--including the constants C<_IOFBF>,
150 C<_IOLBF>, and C<_IONBF> for setvbuf()--except that the buffer parameter
151 specifies a scalar variable to use as a buffer. WARNING: A variable
152 used as a buffer by C<setbuf> or C<setvbuf> must not be modified in any
153 way until the IO::Handle is closed or C<setbuf> or C<setvbuf> is called
154 again, or memory corruption may result!
156 Lastly, there is a special method for working under B<-T> and setuid/gid
163 Marks the object as taint-clean, and as such data read from it will also
164 be considered taint-clean. Note that this is a very trusting action to
165 take, and appropriate consideration for the data source and potential
166 vulnerability should be kept in mind.
172 A C<IO::Handle> object is a GLOB reference. Some modules that
173 inherit from C<IO::Handle> may want to keep object related variables
174 in the hash table part of the GLOB. In an attempt to prevent modules
175 trampling on each other I propose the that any such module should prefix
176 its variables with its own name separated by _'s. For example the IO::Socket
177 module keeps a C<timeout> variable in 'io_socket_timeout'.
182 L<perlop/"I/O Operators">,
187 Due to backwards compatibility, all filehandles resemble objects
188 of class C<IO::Handle>, or actually classes derived from that class.
189 They actually aren't. Which means you can't derive your own
190 class from C<IO::Handle> and inherit those methods.
194 Derived from FileHandle.pm by Graham Barr E<lt>F<bodg@tiuk.ti.com>E<gt>
200 use vars qw($VERSION $XS_VERSION @EXPORT_OK $AUTOLOAD @ISA);
209 $XS_VERSION = "1.15";
213 output_field_separator
214 output_record_separator
215 input_record_separator
218 format_lines_per_page
222 format_line_break_characters
240 ################################################
241 ## Interaction with the XS.
245 @IO::ISA = qw(DynaLoader);
246 bootstrap IO $XS_VERSION;
249 if ($AUTOLOAD =~ /::(_?[a-z])/) {
250 $AutoLoader::AUTOLOAD = $AUTOLOAD;
251 goto &AutoLoader::AUTOLOAD
253 my $constname = $AUTOLOAD;
254 $constname =~ s/.*:://;
255 my $val = constant($constname);
256 defined $val or croak "$constname is not a valid IO::Handle macro";
258 *$AUTOLOAD = sub { $val };
263 ################################################
264 ## Constructors, destructors.
268 my $class = ref($_[0]) || $_[0] || "IO::Handle";
269 @_ == 1 or croak "usage: new $class";
275 my $class = ref($_[0]) || $_[0] || "IO::Handle";
276 @_ == 3 or croak "usage: new_from_fd $class FD, MODE";
279 IO::Handle::fdopen($fh, @_)
285 # There is no need for DESTROY to do anything, because when the
286 # last reference to an IO object is gone, Perl automatically
287 # closes its associated files (if any). However, to avoid any
288 # attempts to autoload DESTROY, we here define it to do nothing.
293 ################################################
297 sub _open_mode_string {
299 $mode =~ /^\+?(<|>>?)$/
300 or $mode =~ s/^r(\+?)$/$1</
301 or $mode =~ s/^w(\+?)$/$1>/
302 or $mode =~ s/^a(\+?)$/$1>>/
303 or croak "IO::Handle: bad open mode: $mode";
308 @_ == 3 or croak 'usage: $fh->fdopen(FD, MODE)';
309 my ($fh, $fd, $mode) = @_;
312 if (ref($fd) && "".$fd =~ /GLOB\(/o) {
313 # It's a glob reference; Alias it as we cannot get name of anon GLOBs
314 my $n = qualify(*GLOB);
317 } elsif ($fd =~ m#^\d+$#) {
318 # It's an FD number; prefix with "=".
322 open($fh, _open_mode_string($mode) . '&' . $fd)
327 @_ == 1 or croak 'usage: $fh->close()';
333 ################################################
334 ## Normal I/O functions.
341 @_ == 1 or croak 'usage: $fh->opened()';
342 defined fileno($_[0]);
346 @_ == 1 or croak 'usage: $fh->fileno()';
351 @_ == 1 or croak 'usage: $fh->getc()';
356 @_ == 1 or croak 'usage: $fh->eof()';
361 @_ or croak 'usage: $fh->print([ARGS])';
367 @_ >= 2 or croak 'usage: $fh->printf(FMT,[ARGS])';
373 @_ == 1 or croak 'usage: $fh->getline';
375 return scalar <$this>;
378 *gets = \&getline; # deprecated
381 @_ == 1 or croak 'usage: $fh->getline()';
383 croak 'Can\'t call $fh->getlines in a scalar context, use $fh->getline';
389 @_ == 2 or croak 'usage: $fh->truncate(LEN)';
390 truncate($_[0], $_[1]);
394 @_ == 3 || @_ == 4 or croak '$fh->read(BUF, LEN [, OFFSET])';
395 read($_[0], $_[1], $_[2], $_[3] || 0);
399 @_ == 3 || @_ == 4 or croak '$fh->sysread(BUF, LEN [, OFFSET])';
400 sysread($_[0], $_[1], $_[2], $_[3] || 0);
404 @_ == 3 || @_ == 4 or croak '$fh->write(BUF, LEN [, OFFSET])';
406 print { $_[0] } substr($_[1], $_[3] || 0, $_[2]);
410 @_ == 3 || @_ == 4 or croak '$fh->syswrite(BUF, LEN [, OFFSET])';
411 syswrite($_[0], $_[1], $_[2], $_[3] || 0);
415 @_ == 1 or croak 'usage: $fh->stat()';
419 ################################################
420 ## State modification functions.
424 my $old = new SelectSaver qualify($_[0], caller);
426 $| = @_ > 1 ? $_[1] : 1;
430 sub output_field_separator {
431 my $old = new SelectSaver qualify($_[0], caller);
433 $, = $_[1] if @_ > 1;
437 sub output_record_separator {
438 my $old = new SelectSaver qualify($_[0], caller);
440 $\ = $_[1] if @_ > 1;
444 sub input_record_separator {
445 my $old = new SelectSaver qualify($_[0], caller);
447 $/ = $_[1] if @_ > 1;
451 sub input_line_number {
452 my $old = new SelectSaver qualify($_[0], caller);
454 $. = $_[1] if @_ > 1;
458 sub format_page_number {
459 my $old = new SelectSaver qualify($_[0], caller);
461 $% = $_[1] if @_ > 1;
465 sub format_lines_per_page {
466 my $old = new SelectSaver qualify($_[0], caller);
468 $= = $_[1] if @_ > 1;
472 sub format_lines_left {
473 my $old = new SelectSaver qualify($_[0], caller);
475 $- = $_[1] if @_ > 1;
480 my $old = new SelectSaver qualify($_[0], caller);
482 $~ = qualify($_[1], caller) if @_ > 1;
486 sub format_top_name {
487 my $old = new SelectSaver qualify($_[0], caller);
489 $^ = qualify($_[1], caller) if @_ > 1;
493 sub format_line_break_characters {
494 my $old = new SelectSaver qualify($_[0], caller);
496 $: = $_[1] if @_ > 1;
500 sub format_formfeed {
501 my $old = new SelectSaver qualify($_[0], caller);
503 $^L = $_[1] if @_ > 1;
512 formline($picture, @_);
517 @_ < 3 || croak 'usage: $fh->write( [FORMAT_NAME] )';
520 my $oldfmt = $fh->format_name($fmt);
522 $fh->format_name($oldfmt);
529 @_ == 3 || croak 'usage: $fh->fcntl( OP, VALUE );';
530 my ($fh, $op, $val) = @_;
531 my $r = fcntl($fh, $op, $val);
532 defined $r && $r eq "0 but true" ? 0 : $r;
536 @_ == 3 || croak 'usage: $fh->ioctl( OP, VALUE );';
537 my ($fh, $op, $val) = @_;
538 my $r = ioctl($fh, $op, $val);
539 defined $r && $r eq "0 but true" ? 0 : $r;