FileHandle



use FileHandle; $fh = new FileHandle; if ($fh->open("< file")) {
 print $line while defined($line = $fh->getline); $fh->close;
}
$pos = $fh->getpos; # like tell() $fh->setpos($pos); # like seek() ($readfh, $writefh) = FileHandle::pipe(); autoflush STDOUT 1;


The FileHandle module mostly serves as a mechanism for cloaking Perl's punctuation variables in longer, more OO-looking calls. It is provided for compatibility with older releases, but is now really only a frontend for several more specific modules, like IO::Handle and IO::File.[5] Its best property is the low-level access it provides to certain rare functions from the C library (clearerr(3), fgetpos(3), fsetpos(3), and setvbuf(3)).

[5]Because it loads so much code, this module costs you a megabyte or so of memory.

Variable Method
$| 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
$^L format_formfeed

Instead of saying:

$ofh = select(HANDLE); $~ = 'SomeFormat'; $| = 1; select($ofh);


you can just say:

use FileHandle; HANDLE->format_name('SomeFormat'); HANDLE->autoflush(1);


Currently, three methods (output_field_separator, output_record_separator, and input_record_separator) only pretend to be per-handle methods: setting them on one handle actually affects all filehandles. They are therefore only supported as class methods, not as per-filehandle methods. This restriction may be lifted someday.

To get a lexically scoped filehandle, instead of using filehandle autovivification:

open my $fh, "< somefile" or die "can't open somefile: $!";


one could say:

use FileHandle; my $fh = FileHandle->new("< somefile") or die "can't open somefile: $!";


FileHandle inherits from IO::File, which inherits from IO::Handle and IO::Seekable. Virtually all the module's functionality is available more efficiently through basic, unadorned Perl calls, except for the following, not all of which may be implemented on all non-Unix platforms: