Skip to content

Perl File I/O Guide — Reading, Writing, and File Operations

DodaTech Updated 2026-06-28 2 min read

In this tutorial, you will learn about Perl File I/O Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Perl file I/O opens files with the open function using < for read, > for write, and >> for append -- with the diamond operator <> reading input line by line and die for error handling.

Opening and Reading Files

use strict;
use warnings;

# Open for reading
open(my $fh, "<", "input.txt")
    or die "Cannot open input.txt: $!";

# Read line by line
while (my $line = <$fh>) {
    chomp $line;
    print "Line: $line\n";
}

close($fh);

Writing Files

# Open for writing (overwrites)
open(my $fh, ">", "output.txt")
    or die "Cannot open output.txt: $!";

print $fh "Hello, World!\n";
print $fh "Line 2\n";
close($fh);

# Open for appending
open(my $log, ">>", "app.log")
    or die "Cannot open app.log: $!";
print $log "Event at " . localtime() . "\n";
close($log);

Reading Entire File

# Slurp entire file into string
open(my $fh, "<", "data.txt")
    or die "Cannot open: $!";
local $/;  # enable slurp mode
my $content = <$fh>;
close($fh);

# Read file into array
open(my $fh, "<", "lines.txt")
    or die "Cannot open: $!";
my @lines = <$fh>;
close($fh);
chomp @lines;  # remove newlines from all

File Tests

# File test operators
if (-e "file.txt")     { print "Exists\n" }
if (-f "file.txt")     { print "Regular file\n" }
if (-d "dir")          { print "Directory\n" }
if (-r "file.txt")     { print "Readable\n" }
if (-w "file.txt")     { print "Writable\n" }
if (-z "file.txt")     { print "Empty\n" }
if (-s "file.txt")     { print "Non-empty\n" }
print "Size: " . (-s "file.txt") . "\n";

Common Mistakes

1. Forgetting the 3-argument open

Use open($fh, "<", $filename) instead of open($fh, "<$filename") to avoid shell injection.

2. Not checking return value

Always check: open(...) or die "Error: $!". Failed opens are silent without checks.

3. Forgetting chomp

Lines from <> include newline. Always chomp before processing unless you need the newline.

Practice Questions

1. What does the diamond operator <> do? Reads one line from the file handle. In a while loop, reads all lines until EOF.

2. How do you open a file for appending? Use > for write, >> for append: open(my $fh, ">>", "file.log").

3. What is the purpose of $! in error messages? $! contains the system error message (like "Permission denied" or "No such file").

FAQ

{{< faq question="What does local $/ do?" >}} Setting $/ to undef enables slurp mode -- the next read gets the entire file as one scalar. {{< /faq >}}

{{< faq question="Can I open a file for both reading and writing?" >}} Yes, use +< for read/write (no truncate) or +> for read/write (truncate). {{< /faq >}}

{{< faq question="How do I handle binary files?" >}} Use binmode($fh) after opening to disable encoding layers and handle raw bytes. {{< /faq >}}

What's Next

Now learn about control flow in Perl.

Topic Description Link
Control Flow if, unless, given/when {{< ref "09-control-flow" >}}
Loops while, for, foreach, until {{< ref "10-loops" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro