Perl | Slurp Module

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 entring 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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Perl code to illustrate the slurp function
use File::Slurp;
  
# read the whole file into a scalar
my $content = read_file('C:\Users\GeeksForGeeks\GFG_Slurp.txt');
  
# write out a whole file from a scalar
write_file('C:\Users\GeeksForGeeks\Copyof_GFG_Slurp.txt', $content);

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# perl code to illustrate the slurp function
use File::Slurp;
  
# read the whole file into a scalar
my @lines = read_file('C:\Users\GeeksForGeeks\GFG_Slurp2.txt');
  
# write out a whole file from a scalar
write_file('C:\Users\GeeksForGeeks\Copyof_GFG_Slurp2.txt', @lines);

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Perl code to illustrate the slurp function
use strict;
use warnings;
use File::Slurp;
  
# calling user defined function
get_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);
}

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.