Updated April 5, 2023
Introduction to Perl Read File
Perl read file is used to read the content of a file, we have to assign file handler on the file to perform various file operations on the file. File reading operations is very important and useful to read the content of the file. There are basically three types of methods available to read the file i.e. using file handler operator, using getc function, and using read function. File handler using the operator is the most important and useful method available in Perl to read any file.
How to Read File in Perl Using Various Methods?
Below is the various method which was available to read a file are as follows. File reading operations is very important and useful in Perl to read the content of file.
1. Below is the mode to read and opened the file to access for various operations on file.
- < – This operator is used to read only access on the file.
- > – This operator is used to creates, writes and truncates the file.
- >> – This operator is used to writes, append, and creates the file.
- +< – This operator is used to read and writes the file at the same time.
- +> – This operator is used to read, writes, creates, and truncates the file.
- +>> – This operator is used to read, writes, creates, and appends the file.
2. Below is the different types of ways or methods available to read the file are as follows.
- Using file handler operator
- Using getc function
- Using read function
3. In Perl read file is used to read the content of a file, in Perl we have assigning file handler to perform the various file operations on the file.
4. File handler using the operator is the most important and useful method available to read any file in Perl.
Methods
Below we have discussed the method of read file are as follows.
1. Using File Handler Operator
- Read file using operator is most important and useful method to read file in Perl.
- This is the main and important method which is used to read file. Using this method we have read file using the operator.
- We have use <> operator to read file in Perl. This operator is very important at the time of read file.
- While using <> operator in Perl it will return the list or multiple lines from the specified file handler.
- File handler operator i.e. <> used in list context of the file. This operator is used in a list context of the file.
- Using <> operator in Perl we have reading single or multiple lines from the file and store it in scalar format.
- The below example shows that read file lines from the abc.txt file and store the content of the file in scalar format.
Example:
We have using the below sample text file to describe example read methods are as follows.
Code:
# Open file name as abc.txt to read first line content using file handler operator.
open(fh, "abc.txt") or die "Not opening";
# Reading first line of the file using <> operator in Perl.
$firstline = <fh>;
print "$firstline\n";
Output:
In the above example, we have using the abc.txt file to describe the example of read file handler operator. We have read the first line from the file using the <> operator in Perl.
2. Using getc Function
- Read file using getc function is most important and useful to read file.
- This is the main and important method which is used to read file. Using this method we have read file using the function.
- Getc function in the read file will return a single character from the file. It will return the first character from the first line.
- This function is most useful when we have read a single character from the file.
- If the error occurred in the file which we have used with getc function then it will return an undef as output to the user.
- Getc function returns a single character from the specified file handler or STDIN if we have not specified a function.
- The below example shows that the read file first character from the abc.txt file is as follows.
Example:
Code:
# Open file name as abc.txt to read first character using getc function in Perl.
open(fh, "abc.txt") or die "Not opening";
# Reading first character of the file using getc function in Perl.
$firstchar_getc = getc(fh);
print "$firstchar_getc\n";
Output:
In the above example, we have to use the abc.txt file to describe the example of read file using getc function in Perl. We have read the first character from the file using getc function in Perl.
3. Using Read Function
- Read file using read function is most important and useful to read file.
- This is the main and important method which is used to read file. Using this method, we have read file using the read function.
- We have read multiple lines from file using a read function. We have read multiple line at one time using read function.
- Using read function we will read the content of file by the file handler till if it does not reach the end of the file.
Example:
Code:
# Open file name as abc.txt to read first character using getc function in Perl.
open(FH, "abc.txt")or die "Not opening";
print "Reading file \n";
# Reading the file till it not reaches to the end of file.
while(<FH>)
{
# Using below operator it will print single line at one time.
print $_;
}
close;
Output:
In the above example, we have to use the abc.txt file to describe the example of read file using the read function.
Conclusion
File reading operations is very important and useful to read the content of file. Perl read file is used to read the content of a file, in Perl we have to assign file handler on the file to perform various file operations on the file.
Recommended Articles
We hope that this EDUCBA information on “Perl Read File” was beneficial to you. You can view EDUCBA’s recommended articles for more information.