Perl File Handling
File handling is the most important part in any programming language. A filehandle is an internal Perl structure that associates with a file name.
Perl File handling is important as it is helpful in accessing file such as text files, log files or configuration files.
Perl filehandles are capable of creating, reading, opening and closing a file.
Perl Create File
We are creating a file, file1.txt with the help of open() function.
The $fh (file handle) is a scalar variable and we can define it inside or before the open() function. Here we have define it inside the function. The '>' sign means we are opening this file for writing. The $filename denotes the path or file location.
Once file is open, use $fh in print statement. The print() function will print the above text in the file.
Now we are closing $fh. Well, closing the file is not required in perl. Your file will be automatically closed when variable goes out of scope.
A file file1.txt will be created in our system.
Perl Open File
We can open a file in following ways:
The < sign is used to open an already existing file. It opens the file in read mode.
The > sign is used to open and create the file if it doesn't exists. It opens the file in write mode.The "<" sign will empty the file before opening it. It will clear all your data of that file. To prevent this use (+) sign before ">" or "<" characters.
The >> sign is used to read and append the file content. It places the file pointer at the end of the file where you can append the information. Here also, to read from this file, you need to put (+) sign before ">>" sign.
Perl Read File
You can read a complete file at once or you can read it one line at a time. We'll show an example for both. Opening a file to read is similar to open a file to write. With only one difference that ">" is used to write and "<" is used to read the file.
We have created a file file1.txt with the following content:
To read Single line at a time
First line of file1.txt will be displayed. Content of $row will be printed with "done" to make it clear that we reached at the end of our program.
This is the First Line. Done.
To read Multi lines at a time
Now we know to read single line from a file. To read multiple lines put $row in a while loop.
Every time, when while loop will reach its condition, it will execute my $row = <$fh>. It will read the next line from the file. At the last line, $fh will return undef which is false and loop will terminate.
This is the First Line. This is the Second Line. This is the Third Line. This is the Fourth Line. Done.
Perl Write File
Through file writing, we'll append lines in the file1.txt. As already stated, new lines will be added at the last of the file.
This line is added in the file1.txt A complete new file is created
Perl Close File
Perl close file is used to close a file handle using close() function. File closing is not compulsory in perl. Perl automatically closes file once the variable is out of scope.
Perl File Handle Operator, <FILEHANDL>
File handle operator is the main method to read information from a file. It is used to get input from user. In scalar context, it returns a single line from the filehandle and in line context, it returns a list of lines from the filehandle.
1. What is your age? 18 You are eligible to vote
2. What is your age? 16 You are not eligible to vote.
Perl File Handle print() function
The print() function prints back the information given through filehandle operators.
Welcome to my site
Perl Copying a File
We can copy content of one file into another file as it is. First open file1 then open file2. Copy the content of file 1 to file2 by reading its line through a while loop.
A new file file2.pl will be created in the location where file1.pl exists.
Perl File Test Operators
There are different test operators to check different information about a file. Some of the test operators are as follows:
Perl Using File Test Operators
To test different features within Perl, a series of test operators are present. In the given example, we have tested different features of file1.txt. All the outcomes are merged with join() function.
file1.txt is a text file, 67 bytes
Next TopicPerl chop() Vs chomp()