Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

IO::Handle

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides object-oriented filehandle methods compatible with Perl’s IO::Handle. All I/O methods delegate to FILEHANDLE_TABLE, the real I/O layer.

IO objects are blessed hashrefs. The __fh_id key stores the filehandle ID (u64) that maps to an entry in FILEHANDLE_TABLE.

Functions

SEEK_CUR

Seek position constants (0, 1, 2). Exported both as constant subs and package scalars.

SEEK_END

Seek position constants (0, 1, 2). Exported both as constant subs and package scalars.

SEEK_SET

Seek position constants (0, 1, 2). Exported both as constant subs and package scalars.

autoflush

Gets or sets the autoflush flag. Returns the previous value.

$fh->autoflush(1);
my $prev = $fh->autoflush();

binmode

Sets the encoding layer on the filehandle. Supports :utf8, :raw, and :bytes. Other layers are no-ops.

$fh->binmode(":utf8");
$fh->binmode(":raw");

blocking

Gets or sets blocking mode. Stub that always returns 1.

$fh->blocking(0);

clearerr

Error tracking stubs. error() always returns 0 (no error). clearerr() always returns 0.

close

Closes the filehandle and removes it from FILEHANDLE_TABLE. Returns 1 on success, undef on failure.

$fh->close();

eof

Returns true if the filehandle is at end-of-file.

while (!$fh->eof) { ... }

error

Error tracking stubs. error() always returns 0 (no error). clearerr() always returns 0.

fcntl

Stubs that return 0. Not yet implemented.

fdopen

Associates an existing IO::Handle object with a file descriptor. Similar to new_from_fd but operates on an already-created object.

my $fh = IO::Handle->new;
$fh->fdopen(fileno(STDOUT), "w");

fileno

Returns the underlying OS file descriptor number, or undef if not open.

my $fd = $fh->fileno();

flush

Flushes the output buffer. Always returns 1.

$fh->flush();

format_formfeed

Format-related accessor stubs. Return empty string.

format_line_break_characters

Format-related accessor stubs. Return empty string.

format_lines_left

Format-related accessor stubs. Return 0.

format_lines_per_page

Format-related accessor stubs. Return 0.

format_name

Format-related accessor stubs. Return empty string.

format_page_number

Format-related accessor stubs. Return 0.

format_top_name

Format-related accessor stubs. Return empty string.

format_write

Format write stub. Returns 1.

getc

Reads a single character. Checks the ungetc buffer first, then reads from the filehandle.

my $ch = $fh->getc();

getline

Reads one record from the filehandle, respecting $/. Croaks if called with arguments.

my $line = $fh->getline();

getlines

Reads all remaining records from the filehandle, respecting $/. Must be called in list context; croaks in scalar context.

my @lines = $fh->getlines();

input_line_number

Gets or sets the per-handle line number (the OO equivalent of $.).

my $lineno = $fh->input_line_number();
$fh->input_line_number(0);

input_record_separator

Get or set the global $/, $,, and $\ special variables respectively.

$fh->input_record_separator("\n");
$fh->output_field_separator(", ");
$fh->output_record_separator("\n");

ioctl

Stubs that return 0. Not yet implemented.

new

Constructor. Returns a new, unopened IO::Handle blessed hashref.

use IO::Handle;
my $fh = IO::Handle->new();

new_from_fd

Constructs an IO::Handle from an existing file descriptor or glob ref. The fd is duped before wrapping.

use IO::Handle;
my $fh = IO::Handle->new_from_fd(fileno(STDOUT), "w");
my $fh = IO::Handle->new_from_fd(\*STDIN, "r");

opened

Returns true if the filehandle is currently open.

if ($fh->opened) { ... }

output_field_separator

Get or set the global $/, $,, and $\ special variables respectively.

$fh->input_record_separator("\n");
$fh->output_field_separator(", ");
$fh->output_record_separator("\n");

output_record_separator

Get or set the global $/, $,, and $\ special variables respectively.

$fh->input_record_separator("\n");
$fh->output_field_separator(", ");
$fh->output_record_separator("\n");

print

Outputs the given arguments to the filehandle. Respects :utf8 layer.

$fh->print("hello ", "world");

printf

Formatted output via sprintf semantics.

$fh->printf("%s = %d\n", $name, $value);

read

Buffered read of $len bytes into $buf, with optional $offset.

my $n = $fh->read($buf, 1024);
my $n = $fh->read($buf, 1024, $offset);

say

Like print, but appends a newline after all arguments.

$fh->say("hello world");

setbuf

Buffer control stubs. Return 0.

setvbuf

Buffer control stubs. Return 0.

stat

Returns the 13-element stat list for the filehandle via fstat(2).

my @st = $fh->stat();

sync

Flushes the handle to disk. Delegates to flush().

sysread

Unbuffered read of $len bytes into $buf, with optional $offset. Same implementation as read in PetaPerl.

my $n = $fh->sysread($buf, 1024);

syswrite

Unbuffered write of $buf (or a substring thereof) to the filehandle.

my $n = $fh->syswrite($buf);
my $n = $fh->syswrite($buf, $len, $offset);

truncate

Truncates the file to $len bytes.

$fh->truncate(0);

ungetc

Pushes a character (by ordinal value) back onto the handle’s internal unget buffer.

$fh->ungetc(ord('A'));

untaint

Marks the handle as untainted. No-op that returns 1.

write

Writes $len bytes from $buf starting at $offset to the filehandle. Perl5 signature: write($buf, $len, $offset).

$fh->write($buf, length($buf), 0);