Perl Tutorial

File I/O

Opening, reading, and writing files in Perl

Opening Files

In Perl, you interact with files using Filehandles. You use the open function to connect a filehandle to a file.
perl
# Reading (<)
open(my $fh, "<", "input.txt") or die "Could not open file: $!";

# Writing (>)
open(my $out, ">", "output.txt") or die "Could not open file: $!";

# Appending (>>)
open(my $log, ">>", "log.txt") or die "Could not open file: $!";
Wait! The or die part is essential. It ensures your script stops immediately if the file can't be opened, and $! explains why (e.g., "File not found").

Reading Line-by-Line

The most memory-efficient way to read a file is one line at a time using a while loop.
perl
while (my $line = <$fh>) {
    chomp($line);
    print "Read: $line\n";
}

Writing to a File

To write, simply put the filehandle after the print keyword.
perl
print $out "This is a new line\n";

Closing Files

It is good practice to close your filehandles when you are finished with them.
perl
close($fh);

Summary

TermDescription
<Open for reading
>Open for writing (Overwrites!)
>>Open for appending
$fhA scalar variable acting as a filehandle
$!Special variable containing the OS error message
autodieA module that makes open fail automatically without or die
Best ForProcessing logs, generating reports, and configuration management
Logic"Stream-based I/O"
SafetyAlways check the return value of open
ModernUse 3-argument open for better security
StandardPart of the Perl core since version 1
IdentityPerl's file handling is extremely fast and robust