The File::Slurp module is used to read contents of a file and store it into a string. It is a simple and efficient way of Reading/Writing/Modifying complete files. Just like its name, it allows you to read or write entire files with one simple call.
By importing this module to your program, the user can implement some functions like read_file, read_text, write_file, etc. to read and write content to/from the file.
Installation of File::Slurp Module:
To use this module, there is a need to first add it to your Perl language package. This can be done by using the following commands in your Perl Terminal and installing the required module.
Step 1: Open your terminal and run the following command:
perl -MCPAN -e shell

After entering into the cpan shell, follow the next step to install the File::Slurp module.
Step 2: Run following command to install the module:
install File::Slurp

This will install the File::Slurp module.
Step 3: Type and run ‘q’ command to exit from the cpan> prompt.
read_file function in Slurp:
File::Slurp’s read_file function reads entire contents of a file with the file name and returns it as a string. However, the use of File::Slurp is not encouraged as it has few encoding layer problems that may cause issues during compilation. File::Slurper aims to be an alternative to avoid the above-mentioned issues.
Syntax:
use File::Slurp;
my $text = read_file($filename);
Return: It returns a string.
read_text function in Slurp:
File::Slurper’s read_text function accepts an optional encoding argument (if any), and can automatically decode CRLF line endings if you request it (for Windows files).
Syntax:
use File::Slurper;
my $content = read_text($filename);
Return: It returns a string.
Note:
CRLF line endings are used to mark a line break in a text file (Windows line break types).
write_file function in Slurp:
File::Slurp’s write_file function is used to write to files all at once with the use of File::Slurp module. It writes to file with the help of a Scalar variable that contains the content of another file read by read_file function.
Syntax:
use File::Slurp;
write_file($filename, $content);
Returns: It doesn’t return any value, just writes the content to the file.
Example1: Using scalar to store file content
PERL
use File::Slurp;
my $content = read_file( 'C:\Users\GeeksForGeeks\GFG_Slurp.txt' );
write_file( 'C:\Users\GeeksForGeeks\Copyof_GFG_Slurp.txt' , $content );
|
Output :



Explanation:
In the above Perl code, initially, we used a slurp function to read a file named GFG_Slurp.txt containing some lines of text as an input into a scalar variable named $content and then wrote the contents of the file into another file Copyof_GFG_Slurp.txt as a single string.
Example2: Using Array to store file content
PERL
use File::Slurp;
my @lines = read_file( 'C:\Users\GeeksForGeeks\GFG_Slurp2.txt' );
write_file( 'C:\Users\GeeksForGeeks\Copyof_GFG_Slurp2.txt' , @lines );
|
Output :



Explanation:
In the above Perl code, initially, we used a slurp function to read a file named GFG_Slurp2.txt containing an array of lines of text as an input into a array variable named @lines and then wrote the contents of the entire file into a file named Copyof_GFG_Slurp2.txt as a single string.
Example3: Creating a function to use slurp method
Perl
use strict;
use warnings;
use File::Slurp;
get_a_string();
sub get_a_string
{
my $gfg_str = read_file( 'C:\Users\GeeksForGeeks\GFG_User_Slurp.txt' );
write_file( 'C:\Users\GeeksForGeeks\Copyof_GFG_User_Slurp.txt' , $gfg_str );
}
|
Output :



Explanation:
In the above Perl code, strict and warnings allow the user to enter code more liberally and catch errors sooner such as typos in variable names, etc. We called a user-defined function named get_a_string which in turn executes the slurp function i.e.., it reads a file containing some lines of text as an input into a variable named gfg_str and then wrote the contents of the entire file into a file as a single string.
Similar Reads
Perl | Modules
A module in Perl is a collection of related subroutines and variables that perform a set of programming tasks. Perl Modules are reusable. Various Perl modules are available on the Comprehensive Perl Archive Network (CPAN). These modules cover a wide range of categories such as network, CGI, XML proc
3 min read
Perl | Multiple Subroutines
Prerequisite: Perl | Subroutines or Functions A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language user want to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write
3 min read
Perl | sleep() Function
sleep() function in Perl is an inbuilt function which is used to delay the execution of the current script for a specified number of seconds or forever if parameter is not specified. The sleep( ) function accepts seconds as a parameter and returns the same on success. Syntax: sleep(seconds) Returns:
1 min read
Perl | LDAP Server
Lightweight Directory Access Protocol (LDAP) is an internet protocol that works on TCP/IP and is used to access information from directories. The LDAP protocol is usually used to access an active directory. It allows us to keep a directory of items and information about them. LDAP stores the data in
4 min read
Perl - Taint() Method
In Perl, taint mode is a way to make our code more secure. It makes our program fussier about the data that it receives from an external source. The external source means the users, the file system, the environment, locale information, and some system calls. Taint mode was created for situations whe
5 min read
Node.js Utility Module
The util module in Node.js provides a variety of utility functions that assist with tasks such as debugging, formatting, and inheritance. It includes methods for inspecting objects, formatting strings, and extending classes. Node.js Utility ModuleThe util module offers essential utilities that are n
5 min read
Scrapy - Item Loaders
In this article, we are going to discuss Item Loaders in Scrapy. Scrapy is used for extracting data, using spiders, that crawl through the website. The obtained data can also be processed, in the form, of Scrapy Items. The Item Loaders play a significant role, in parsing the data, before populating
15+ min read
Scrapy - Item Pipeline
Scrapy is a web scraping library that is used to scrape, parse and collect web data. For all these functions we are having a pipelines.py file which is used to handle scraped data through various components (known as class) which are executed sequentially. In this article, we will be learning throug
10 min read
Introduction to Perl
Perl is a general-purpose, high level interpreted and dynamic programming language. It was developed by Larry Wall, in 1987. There is no official Full form of the Perl, but still, the most used expansion is "Practical Extraction and Reporting Language". Some of the programmers also refer Perl as the
9 min read
Modes of Writing a Perl Code
Perl is a general purpose, high level interpreted and dynamic programming language. Perl supports both the procedural and Object-Oriented programming. At the beginning level, Perl was developed only for the system management and text handling but in later versions, Perl got the ability to handle reg
6 min read