IO::Handle - supply object methods for filehandles
use IO::Handle; $fh = new IO::Handle; if ($fh->open "< file") { print <$fh>; $fh->close; } $fh = new IO::Handle "> FOO"; if (defined $fh) { print $fh "bar\n"; $fh->close; } $fh = new IO::Handle "file", "r"; if (defined $fh) { print <$fh>; undef $fh; # automatically closes the file } $fh = new IO::Handle "file", O_WRONLY|O_APPEND; if (defined $fh) { print $fh "corge\n"; undef $fh; # automatically closes the file } $pos = $fh->getpos; $fh->setpos $pos; $fh->setvbuf($buffer_var, _IOLBF, 1024); autoflush STDOUT 1;
IO::Handle::new creates a IO::Handle, which is a reference to a newly created symbol (see the Symbol package). If it receives any parameters, they are passed to IO::Handle::open; if the open fails, the IO::Handle object is destroyed. Otherwise, it is returned to the caller.
IO::Handle::new
IO::Handle
Symbol
IO::Handle::open
IO::Handle::new_from_fd creates a IO::Handle like new does. It requires two parameters, which are passed to IO::Handle::fdopen; if the fdopen fails, the IO::Handle object is destroyed. Otherwise, it is returned to the caller.
IO::Handle::new_from_fd
new
IO::Handle::fdopen
IO::Handle::open accepts one parameter or two. With one parameter, it is just a front end for the built-in open function. With two parameters, the first parameter is a filename that may include whitespace or other special characters, and the second parameter is the open mode in either Perl form (">", "+<", etc.) or POSIX form ("w", "r+", etc.).
open
IO::Handle::fdopen is like open except that its first parameter is not a filename but rather a file handle name, a IO::Handle object, or a file descriptor number.
IO::Handle::write is like write found in C, that is it is the opposite of read. The wrapper for the perl write function is called format_write.
IO::Handle::write
write
format_write
IO::Handle::opened returns true if the object is currently a valid file descriptor.
IO::Handle::opened
If the C functions fgetpos() and fsetpos() are available, then IO::Handle::getpos returns an opaque value that represents the current position of the IO::Handle, and IO::Handle::setpos uses that value to return to a previously visited position.
IO::Handle::getpos
IO::Handle::setpos
If the C function setvbuf() is available, then IO::Handle::setvbuf sets the buffering policy for the IO::Handle. The calling sequence for the Perl function is the same as its C counterpart, including the macros _IOFBF, _IOLBF, and _IONBF, except that the buffer parameter specifies a scalar variable to use as a buffer. WARNING: A variable used as a buffer by IO::Handle::setvbuf must not be modified in any way until the IO::Handle is closed or until IO::Handle::setvbuf is called again, or memory corruption may result!
IO::Handle::setvbuf
_IOFBF
_IOLBF
_IONBF
See perlfunc for complete descriptions of each of the following supported IO::Handle methods, which are just front ends for the corresponding built-in functions:
close fileno getc gets eof read truncate stat
See perlvar for complete descriptions of each of the following supported IO::Handle methods:
autoflush output_field_separator output_record_separator input_record_separator input_line_number format_page_number format_lines_per_page format_lines_left format_name format_top_name format_line_break_characters format_formfeed format_write
Furthermore, for doing normal I/O you might need these:
See "print" in perlfunc.
See "printf" in perlfunc.
This works like <$fh> described in "I/O Operators" in perlop except that it's more readable and can be safely called in an array context but still returns just one line.
This works like <$fh> when called in an array context to read all the remaining lines in a file, except that it's more readable. It will also croak() if accidentally called in a scalar context.
The reference returned from new is a GLOB reference. Some modules that inherit from IO::Handle may want to keep object related variables in the hash table part of the GLOB. In an attempt to prevent modules trampling on each other I propose the that any such module should prefix its variables with its own name separated by _'s. For example the IO::Socket module keeps a timeout variable in 'io_socket_timeout'.
timeout
perlfunc, "I/O Operators" in perlop, "FileHandle" in POSIX
Due to backwards compatibility, all filehandles resemble objects of class IO::Handle, or actually classes derived from that class. They actually aren't. Which means you can't derive your own class from IO::Handle and inherit those methods.
Derived from FileHandle.pm by Graham Barr <bodg@tiuk.ti.com>
To install IO, copy and paste the appropriate command in to your terminal.
cpanm
cpanm IO
CPAN shell
perl -MCPAN -e shell install IO
For more information on module installation, please visit the detailed CPAN module installation guide.