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 entering into the cpan shell, follow the next step to install the File::Slurp module.
Step 2: Run following command to install the module:
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.
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).
my $content = read_text($filename);
Return: It returns a string.
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.
Returns: It doesn’t return any value, just writes the content to the file.
Example1: Using scalar to store file content
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
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
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.
Please Login to comment...