Perl read file
last modified October 8, 2026
In this article we show how to read files in Perl.
To read files in Perl, we can use built-in functions such as open
and read, or use a module such as Path::Tiny for
convenience.
sky water rock falcon cloud war nice cup wrong forest blue bottle pen chair cup
We use a simple text file.
Perl read text file I
In the first example, we read the contents of the text file with
open and a for loop.
#!/usr/bin/perl
use v5.34.0;
use warnings;
open my $fh, "<", "words.txt"
or die "Cannot open words.txt: $!";
print $_ for <$fh>;
close $fh;
We read the contents of the file line by line.
open my $fh, "<", "words.txt"
or die "Cannot open words.txt: $!";
With open function, we open a file handle in read-only mode to the
specified text file.
print $_ for <$fh>;
We go over the file handle with the for loop and print the contents
of the file line by line.
close $fh;
In the end, we close the handle with close.
$ ./main.pl sky water rock falcon cloud war nice cup wrong forest blue bottle pen chair cup
Perl read text file II
The second example uses a while loop to read a text file.
#!/usr/bin/perl
use v5.34.0;
use warnings;
open my $fh, "<", "words.txt"
or die "Cannot open words.txt: $!";
while (<$fh>) {
print $_;
}
close $fh;
Using the while loop and the <> operator, we read the file
line by line.
Perl diamond operator
Perl allows to read a file without explicitly opening it.
#!/usr/bin/perl
use v5.34.0;
use warnings;
while (<>) {
print $_;
}
The diamond operator (<>) reads from each file named in
@ARGV, in order; if no filenames are supplied, it reads standard
input. The operator opens and closes the files for you.
$ ./main.pl words.txt sky water rock falcon cloud war nice cup wrong forest blue bottle pen chair cup
We pass the file name as a parameter to the Perl program.
Perl slurp text file
For relatively small files, we can read the whole file into a variable in one
step. slurp uses the file's configured I/O layers; use
slurp_utf8 to decode UTF-8 text or slurp_raw when you
need raw bytes.
#!/usr/bin/perl
use v5.34.0;
use warnings;
use Path::Tiny;
my $f = path('./words.txt');
my $res = $f->slurp;
print($res);
In the example, we use the Path::Tiny module.
my $f = path('./words.txt');
We create a path object.
my $res = $f->slurp;
We read the contents into a variable with slurp.
Perl read text file into array
In the next example, we read the text file into an array.
#!/usr/bin/perl
use v5.34.0;
use warnings;
use Path::Tiny;
my $f = path('./words.txt');
my @lines = $f->lines;
print $_ for (@lines);
In the program, we use Path::Tiny and its lines member
function.
Perl head/tail example
In the next example, we create a head/tail utility.
#!/usr/bin/perl
use v5.34.0;
use warnings;
use Path::Tiny;
my $f = path('./words.txt');
my $n = shift || 5;
my @lines = $f->lines( { count => $n } );
print $_ for (@lines);
The program reads the first n lines when n is
positive, or the last |n| lines when it is negative.
my $n = shift || 5;
We provide the number of lines to read on the command line. If we do not give any value, five lines are read from the top.
my @lines = $f->lines( { count => $n } );
The number of lines to read is given to the count option. It also
accepts negative integers.
$ ./main.pl 3 sky water rock $ ./main.pl sky water rock falcon cloud $ ./main.pl -2 chair cup
Perl read web page
In the next example, we read a remote file (a web page resource).
#!/usr/bin/perl
use v5.34.0;
use warnings;
use HTTP::Tiny;
my $url = 'https://httpbin.org/robots.txt';
my $r = HTTP::Tiny->new->get($url);
if ($r->{success}) {
print $r->{content};
} else {
die "Request failed: $r->{status} $r->{reason}\n";
}
HTTP::Tiny is a core Perl HTTP client. This example uses HTTPS;
check the response's success field before using its
content.
$ ./main.pl User-agent: * Disallow: /deny
Perl read binary file
The following example reads a binary file and prints the contents in hexadecimal.
#!/usr/bin/perl
use v5.34.0;
use warnings;
open my $fh, "<:raw", "favicon.ico"
or die "Cannot open favicon.ico: $!";
my $block_size = 1024;
my $data;
my $column = 0;
while (1) {
my $bytes_read = read $fh, $data, $block_size;
defined $bytes_read or die "Cannot read favicon.ico: $!";
last if $bytes_read == 0;
for my $byte ( unpack 'C*', $data ) {
printf "%02x ", $byte;
print "\n" if ++$column % 20 == 0;
}
}
print "\n" if $column % 20;
close $fh;
The program reads a small icon. It uses open, read,
and unpack.
open my $fh, "<:raw", "favicon.ico"
or die "Cannot open favicon.ico: $!";
We open the file for reading in raw mode.
my $bytes_read = read $fh, $data, $block_size; defined $bytes_read or die "Cannot read favicon.ico: $!"; last if $bytes_read == 0;
We read up to 1024 bytes per iteration, stop at end-of-file, and report read errors.
for my $byte ( unpack 'C*', $data ) {
printf "%02x ", $byte;
}
In raw mode, read returns bytes. unpack 'C*'
interprets each byte as an unsigned integer, which we print in hexadecimal,
with up to 20 bytes per line. The loop distinguishes end-of-file (zero) from a
read error (undef).
$ ./main.pl 00 00 01 00 01 00 10 10 00 00 00 00 00 00 68 05 00 00 16 00 00 00 28 00 00 00 10 00 00 00 20 00 00 00 01 00 08 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 ff ff ff 00 4d 45 3d 00 00 00 00 00 00 00 ...
In this article we have read text and binary files in Perl.
Author
List all Perl tutorials.