How to use File::Slurp to read entire file into variable in Perl?
Question
How to use File::Slurp to read entire file into variable in Perl?
Reading an Entire File into a Variable Using File::Slurp in Perl
The File::Slurp module is a simple and convenient way to read (or write) entire files with minimal code. As its name suggests, it “slurps” the whole content of a file into a scalar or list variable in one go, which is very useful for small to medium files where you want to process the entire content at once.
Here's a quick overview of how File::Slurp works for reading files:
- You use the
read_filefunction, which reads the complete file and returns the content. - The content can be returned as a scalar (whole file contents as one string) or as a list (each line as an element).
File::Slurpis part of the core Perl ecosystem and is compatible with Perl 5.8 and later.
Key Perl Concepts Used Here
- Sigils: The
$sigil represents a scalar variable. Since the whole content is read as one string, it goes into a scalar, e.g.,$content. - Context: In scalar context,
read_filereturns the entire file as a single string. In list context, it returns a list of lines. - TMTOWTDI (There’s More Than One Way To Do It): You can also use other core functions like
slurpinPath::Tiny, or manual filehandles, butFile::Slurpis known for its simplicity.
Example: Reading a File’s Content into a Scalar Variable
use strict;
use warnings;
use File::Slurp;
# Replace 'example.txt' with your file path
my $filename = 'example.txt';
# read entire file content into scalar variable
my $content = read_file($filename);
print "Content of file '$filename':\n";
print "---------------------------------\n";
print $content;
print "\n---------------------------------\n";
How to run the example: Create a text file named example.txt with some sample text in the same directory as your script, then run the Perl script (e.g., perl script.pl).
Common Pitfalls & Gotchas
- File::Slurp and Unicode: By default,
File::Slurpdoes not handle Unicode well unless you specify the encoding explicitly:
my $content = read_file($filename, { binmode => ':utf8' });
- Large files: For very large files, reading the entire content into memory can be inefficient and may exhaust system memory.
- Deprecated warnings: In recent Perl developments,
File::Slurphas been discouraged in favor of modules likePath::Tiny. However,File::Slurpremains widely used in legacy scripts. - File existence and errors:
read_filewill die on failure by default. You can wrap inevalor use theerr_modeoption to handle errors gracefully.
Summary
Using File::Slurp to read an entire file into a scalar variable is as simple as calling read_file($filename). It simplifies code by replacing manual filehandle reading loops, providing a concise way to slurp file data in Perl.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 59ms
Content of file 'example.txt':
---------------------------------
---------------------------------
(empty)Was this helpful?
Related Questions
- How to get file modification time with Path::Tiny in Perl?
- How to use Path::Tiny touch to create empty file in Perl?
- How to use List::Util reduce for custom aggregation in Perl?
- How to compare two DateTime objects in Perl?
- How to use DateTime::Duration for time periods in Perl?
- How to decode Base64 string in Perl using MIME::Base64?