O'Reilly logo

Programming Perl, 3rd Edition by Jon Orwant, Tom Christiansen, Larry Wall

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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)).

VariableMethod
$|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
$^Lformat_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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required