cpan beginner

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_file function, 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::Slurp is 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_file returns 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 slurp in Path::Tiny, or manual filehandles, but File::Slurp is 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::Slurp does 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::Slurp has been discouraged in favor of modules like Path::Tiny. However, File::Slurp remains widely used in legacy scripts.
  • File existence and errors: read_file will die on failure by default. You can wrap in eval or use the err_mode option 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

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Content of file 'example.txt':
---------------------------------

---------------------------------
STDERR
(empty)

Was this helpful?

Related Questions