- File handling is the most important part in any programming language.
- A Perl filehandle is a variable that associates with a file.
- Perl filehandles are capable of creating, reading, opening and closing a file.
- Perl File handling is important as it is helpful in accessing file such as text files, log files or configuration files.
1) Open a file.
The open() function has three arguments:
- Filehandle: that associates with the file
- Mode: you can open a file for reading, writing or appending.
- Filename: the path to the file that is being opened.
To open a file in a specific mode, you need to pass the corresponding operand to the open() function.
The open file modes are explained in details as follows:
The open file modes are explained in details as follows:
- Read mode (<): you only can read the file but cannot change its content.
- Write mode (>): If the file does not exist, a new file is created. If the file already exists, the content of the file is wipe out, therefore, you should use the write mode with extra cautious.
- Append mode ( >>): You can open the file for appending new content to the existing content of the file. However, you cannot change the existing content in the file.
Example:
open(FH, '<', 'c:\temp\test.txt');
The open file returns true on success and false on failure. You can use the die() function to handle a file-opening failure.
open(FH, '<', 'c:\temp\test.txt') or die $!;
- $! is a special variable that conveys the error message telling why the open() function failed.
- It could be something like “No such file or directory” or “Permission denied”.
- In case the file c:\temp\test.txt does not exist, you get an error message “No such file or directory”.
1 2 3 4 5 6 7 8 9 | #!/usr/bin/perl use warnings; use strict; #Opening a file open(FH, '<', 'c:\temp\test.txt'); #Closing a file close(FH); |
3) Read a file
To read from a file in read mode, you need to put the filehandle variable inside angle brackets as follows:
To read the next line of the file:
$line = <FH>;
To read a file line by line to the end of the file:
while(<FH>){
}
1 2 3 4 5 6 7 8 9 10 11 12 13 | #!/usr/bin/perl use warnings; use strict; #Opening a file open(FH, '<', 'c:\temp\test.txt'); while(<FH>){ print $_; } #Closing a file close(FH); |
4) Write a file
You should use the print() function to write data into file as follows:
print FH $str;
Example of write file:
1 2 3 4 5 6 7 8 9 10 11 | #!/usr/bin/perl use warnings; use strict; my $str = "This is the sample text!"; open(FH, '>', 'c:\temp\test.txt') or die $!; print FH $str; close(FH); |
5) Perl Test Operator
- Perl file test operators are used to check various characteristics of a file.
- In order to perform read or write tasks, you use Perl file test operators to make sure the file is exist and readable.
- The Perl file test operators are logical operators which return true or false.
Example: To check if file is exist, you have to use -e operator
1 2 3 4 5 6 7 8 9 10 11 | #!/usr/bin/perl use warnings; use strict; my $filename = 'c:\temp\test.txt'; if(-e $filename){ print("File exists\n"); }else{ print("File does not exists\n"); } |
The following list explains the most important Perl file test operators:
-r: check if the file is readable
-w: check if the file is writable
-x: check if the file is executable
-e: check if the file exists.
-z: check if the file is empty.
-s: check if the file has nonzero size (returns size in bytes).
-f: check if the file is a plain file.
-d: check if the file is a directory.
-l: check if the file is a symbolic link.
This comment has been removed by a blog administrator.
ReplyDelete