Perl | Slurp Module - GeeksforGeeks

17 Feb, 2022
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
# Perl code to illustrate the slurp functionuse File::Slurp; # read the whole file into a scalarmy $content = read_file('C:\Users\GeeksForGeeks\GFG_Slurp.txt'); # write out a whole file from a scalarwrite_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
# perl code to illustrate the slurp functionuse File::Slurp; # read the whole file into a scalarmy @lines = read_file('C:\Users\GeeksForGeeks\GFG_Slurp2.txt'); # write out a whole file from a scalarwrite_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
# Perl code to illustrate the slurp functionuse strict;use warnings;use File::Slurp; # calling user defined functionget_a_string(); sub get_a_string{  # read entire file into a scalar  my $gfg_str = read_file('C:\Users\GeeksForGeeks\GFG_User_Slurp.txt');    # write entire file from a scalar  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. 
sumitgumber28
Perl-File-Functions
Perl-files
Picked
Perl
Perl
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Perl | Arrays (push, pop, shift, unshift)
Perl | Arrays
Perl Tutorial - Learn Perl With Examples
Use of print() and say() in Perl
Perl | length() Function
Perl | join() Function
Perl | Basic Syntax of a Perl Program
Perl | Boolean Values
Perl | sleep() Function
Perl | Subroutines or Functions