Skip to Content
Programming Perl, 3rd Edition
book

Programming Perl, 3rd Edition

by Larry Wall, Tom Christiansen, Jon Orwant
July 2000
Intermediate to advanced
1104 pages
35h 1m
English
O'Reilly Media, Inc.
Content preview from Programming Perl, 3rd Edition

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

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.
Start your free trial

You might also like

Programming Perl, 4th Edition

Programming Perl, 4th Edition

Tom Christiansen, brian d foy, Larry Wall, Jon Orwant
Learning Perl, 7th Edition

Learning Perl, 7th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Programming the Perl DBI

Programming the Perl DBI

Tim Bunce, Alligator Descartes

Publisher Resources

ISBN: 0596000278Supplemental ContentErrata