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)).
| 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access