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.

Output:

done.
A file file1.txt will be created in our system.

Perl Open File

We can open a file in following ways:

(<) Syntax

The < sign is used to open an already existing file. It opens the file in read mode.

(>) Syntax

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.

(+>+<) Syntax

(>>) Syntax

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.

Output:

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.

Output:

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.

Output:

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.

Output:

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.

Output:

	
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.

Output:

	
done

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:

Test operatorsDescription
-AReturn total access time of file since the program started.
-bCheck whether file is block device or not.
-BCheck whether it is a binary file.
-cCheck whether file is character device.
-CReturn inode change time of file since the program started.
-dCheck whether file is a directory.
-eCheck whether file exists or not.
-fCheck type of file whether it is regular, symbolic link or other type of file.
-gCheck whether file have setgid bit set.
-kCheck whether file have sticky bit set.
-lCheck if file is a symbolic link. In dos, it always return false.
-MReturn file modification time in days since the program started.
-oCheck if file is owned by an effective uid, in dos, it always return true.
-OCheck if file is owned by a real uid, in dos, it always return true.
-pCheck whether file is a named pipe.
-rCheck whether file is readable or not.
-RCheck whether file is readable by real uid or not. In dos, it is same as -r.
-sReturn the file size in bytes.
-SCheck if file is a socket.
-tCheck if file is opened to a tty (terminal)
-TCheck if file is a text file.
-uCheck if file has setuid bit set.
-wCheck if file is writable or not.
-WCheck if file is writable by real uid/gid.
-xCheck if file can be executed or not.
-XCheck if file can be executed by real uid/gid.
-zCheck if file size is zero or not.

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.

Output:

	
file1.txt is a text file, 67 bytes