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 If the C function setvbuf() is available, then C<IO::Handle::setvbuf>
62 sets the buffering policy for the IO::Handle. The calling sequence
63 for the Perl function is the same as its C counterpart, including the
64 macros C<_IOFBF>, C<_IOLBF>, and C<_IONBF>, except that the buffer
65 parameter specifies a scalar variable to use as a buffer. WARNING: A
66 variable used as a buffer by C<IO::Handle::setvbuf> must not be
67 modified in any way until the IO::Handle is closed or until
68 C<IO::Handle::setvbuf> is called again, or memory corruption may
71 See L<perlfunc> for complete descriptions of each of the following
72 supported C<IO::Handle> methods, which are just front ends for the
73 corresponding built-in functions:
87 See L<perlvar> for complete descriptions of each of the following
88 supported C<IO::Handle> methods:
91 output_field_separator
92 output_record_separator
93 input_record_separator
100 format_line_break_characters
104 Furthermore, for doing normal I/O you might need these:
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->fdopen ( FD, MODE )
122 C<fdopen> is like an ordinary C<open> except that its first parameter
123 is not a filename but rather a file handle name, a IO::Handle object,
124 or a file descriptor number.
126 =item $fh->write ( BUF, LEN [, OFFSET }\] )
128 C<write> is like C<write> found in C, that is it is the
129 opposite of read. The wrapper for the perl C<write> function is
130 called C<format_write>.
134 Returns true if the object is currently a valid file descriptor.
138 Lastly, a special method for working under B<-T> and setuid/gid scripts:
144 Marks the object as taint-clean, and as such data read from it will also
145 be considered taint-clean. Note that this is a very trusting action to
146 take, and appropriate consideration for the data source and potential
147 vulnerability should be kept in mind.
153 A C<IO::Handle> object is a GLOB reference. Some modules that
154 inherit from C<IO::Handle> may want to keep object related variables
155 in the hash table part of the GLOB. In an attempt to prevent modules
156 trampling on each other I propose the that any such module should prefix
157 its variables with its own name separated by _'s. For example the IO::Socket
158 module keeps a C<timeout> variable in 'io_socket_timeout'.
163 L<perlop/"I/O Operators">,
168 Due to backwards compatibility, all filehandles resemble objects
169 of class C<IO::Handle>, or actually classes derived from that class.
170 They actually aren't. Which means you can't derive your own
171 class from C<IO::Handle> and inherit those methods.
175 Derived from FileHandle.pm by Graham Barr E<lt>F<bodg@tiuk.ti.com>E<gt>
181 use vars qw($VERSION $XS_VERSION @EXPORT_OK $AUTOLOAD @ISA);
190 $XS_VERSION = "1.15";
194 output_field_separator
195 output_record_separator
196 input_record_separator
199 format_lines_per_page
203 format_line_break_characters
223 ################################################
224 ## Interaction with the XS.
228 @IO::ISA = qw(DynaLoader);
229 bootstrap IO $XS_VERSION;
232 if ($AUTOLOAD =~ /::(_?[a-z])/) {
233 $AutoLoader::AUTOLOAD = $AUTOLOAD;
234 goto &AutoLoader::AUTOLOAD
236 my $constname = $AUTOLOAD;
237 $constname =~ s/.*:://;
238 my $val = constant($constname);
239 defined $val or croak "$constname is not a valid IO::Handle macro";
241 *$AUTOLOAD = sub { $val };
246 ################################################
247 ## Constructors, destructors.
251 my $class = ref($_[0]) || $_[0] || "IO::Handle";
252 @_ == 1 or croak "usage: new $class";
258 my $class = ref($_[0]) || $_[0] || "IO::Handle";
259 @_ == 3 or croak "usage: new_from_fd $class FD, MODE";
262 IO::Handle::fdopen($fh, @_)
268 # There is no need for DESTROY to do anything, because when the
269 # last reference to an IO object is gone, Perl automatically
270 # closes its associated files (if any). However, to avoid any
271 # attempts to autoload DESTROY, we here define it to do nothing.
276 ################################################
280 sub _open_mode_string {
282 $mode =~ /^\+?(<|>>?)$/
283 or $mode =~ s/^r(\+?)$/$1</
284 or $mode =~ s/^w(\+?)$/$1>/
285 or $mode =~ s/^a(\+?)$/$1>>/
286 or croak "IO::Handle: bad open mode: $mode";
291 @_ == 3 or croak 'usage: $fh->fdopen(FD, MODE)';
292 my ($fh, $fd, $mode) = @_;
295 if (ref($fd) && "".$fd =~ /GLOB\(/o) {
296 # It's a glob reference; Alias it as we cannot get name of anon GLOBs
297 my $n = qualify(*GLOB);
300 } elsif ($fd =~ m#^\d+$#) {
301 # It's an FD number; prefix with "=".
305 open($fh, _open_mode_string($mode) . '&' . $fd)
310 @_ == 1 or croak 'usage: $fh->close()';
316 ################################################
317 ## Normal I/O functions.
324 @_ == 1 or croak 'usage: $fh->opened()';
325 defined fileno($_[0]);
329 @_ == 1 or croak 'usage: $fh->fileno()';
334 @_ == 1 or croak 'usage: $fh->getc()';
339 @_ == 1 or croak 'usage: $fh->eof()';
344 @_ or croak 'usage: $fh->print([ARGS])';
350 @_ >= 2 or croak 'usage: $fh->printf(FMT,[ARGS])';
356 @_ == 1 or croak 'usage: $fh->getline';
358 return scalar <$this>;
361 *gets = \&getline; # deprecated
364 @_ == 1 or croak 'usage: $fh->getline()';
366 croak 'Can\'t call $fh->getlines in a scalar context, use $fh->getline';
372 @_ == 2 or croak 'usage: $fh->truncate(LEN)';
373 truncate($_[0], $_[1]);
377 @_ == 3 || @_ == 4 or croak '$fh->read(BUF, LEN [, OFFSET])';
378 read($_[0], $_[1], $_[2], $_[3] || 0);
382 @_ == 3 || @_ == 4 or croak '$fh->sysread(BUF, LEN [, OFFSET])';
383 sysread($_[0], $_[1], $_[2], $_[3] || 0);
387 @_ == 3 || @_ == 4 or croak '$fh->write(BUF, LEN [, OFFSET])';
389 print { $_[0] } substr($_[1], $_[3] || 0, $_[2]);
393 @_ == 3 || @_ == 4 or croak '$fh->syswrite(BUF, LEN [, OFFSET])';
394 syswrite($_[0], $_[1], $_[2], $_[3] || 0);
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);
512 @_ == 3 || croak 'usage: $fh->fcntl( OP, VALUE );';
513 my ($fh, $op, $val) = @_;
514 my $r = fcntl($fh, $op, $val);
515 defined $r && $r eq "0 but true" ? 0 : $r;
519 @_ == 3 || croak 'usage: $fh->ioctl( OP, VALUE );';
520 my ($fh, $op, $val) = @_;
521 my $r = ioctl($fh, $op, $val);
522 defined $r && $r eq "0 but true" ? 0 : $r;