FileHandle class

Syntax

my $fh = new FileHandle(name, mode);
nameThe filename for the accessed file.
modeHow the file is accessed including r for read and w for write.
The FileHandle class manages input and output operations for files. When the object is scavenged, the file is closed.

Example

my @lines;
my $fh = new FileHandle("filename.txt", "r");
if (defined $fh) {
	@lines = $fh->getlines();
	undef $fh;
}

The example opens a text file, reads all of the lines from the file into an array, and closes the file.


autoflush variable

Syntax

$fh->autoflush = expression;
expression0 to flush output only on request, 1 to flush output after every write.

input_record_separator variable

Syntax

$fh->input_record_separator = undef;
You can set input_record_separator to a multi-character string to match a multi-character terminator or to undef to read through the end of file.

print subroutine

Syntax

$fh->print(string, ...);
stringOne or more strings for printing to the file.

printf subroutine

Syntax

$fh->printf(format, string, ...);
formatA string with tokens for interpolating and formatting strings.
stringOne or more strings to interpolate.

getline subroutine

Syntax

$fh->getline;

getlines subroutine

Syntax

$fh->getlines;