FileHandle - Supply Object Methods for Filehandles

use FileHandle; $fh = new FileHandle; if ($fh->open "< file") {
 print <$fh>; $fh->close;
}
$fh = new FileHandle "> file"; if (defined $fh) {
 print $fh "bar\n"; $fh->close;
}
$fh = new FileHandle "file", "r"; if (defined $fh) {
 print <$fh>; undef $fh; # automatically closes the file
}
$fh = new FileHandle "file", O_WRONLY|O_APPEND; if (defined $fh) {
 print $fh "stuff\n"; undef $fh; # automatically closes the file
}
$pos = $fh->getpos; $fh->setpos($pos); $fh->setvbuf($buffer_var, _IOLBF, 1024); ($readfh, $writefh) = FileHandle::pipe; autoflush STDOUT 1;

The following supported FileHandle methods are just front ends for the corresponding built-in Perl functions:

clearerr getc
close gets
eof seek
fileno tell

The following supported FileHandle methods correspond to Perl special variables:

autoflush format_page_number
format_formfeed format_top_name
format_line_break_characters input_line_number
format_lines_left input_record_separator
format_lines_per_page output_field_separator
format_name output_record_separator

Furthermore, for doing normal I/O you might need these methods:

Bugs

Due to backward compatibility, all filehandles resemble objects of class FileHandle, or actually classes derived from that class. But they aren't. Which means you can't derive your own class from FileHandle and inherit those methods.

While it may look as though the filehandle methods corresponding to the built-in variables are unique to a particular filehandle, currently some of them are not, including the following:

input_line_number()
input_record_separator()
output_record_separator()