In Perl, file handling is the process of creating, reading, writing, updating, and deleting files. Perl provides a variety of built-in functions and modules that make it easy to work with files. Here’s an introduction to file handling in Perl:
File modes:
When opening a file in Perl, you need to specify a file mode, which determines how the file can be accessed. There are three main file modes:
- Read mode (<): Opens the file for reading. The file must already exist.
- Write mode (>): Opens the file for writing. If the file does not exist, it will be created. If the file already exists, its contents will be truncated.
- Append mode (>>): Opens the file for writing, but appends new data to the end of the file instead of overwriting its contents. If the file does not exist, it will be created.
File handling functions:
Here are some of the most commonly used built-in file-handling functions in Perl:
- open(): Opens a file and returns a file handle.
- close(): Closes a file handle.
- print(): Writes data to a file.
- read(): Reads data from a file.
- seek(): Moves the file pointer to a specific location in the file.
- tell(): Returns the current position of the file pointer.
- stat(): Returns information about a file, such as its size, owner, and permissions.
File handling modules:
In addition to the built-in file-handling functions, Perl also provides a number of modules that can make file handling easier and more efficient. Here are some of the most commonly used file-handling modules in Perl:
- File::Basename: Provides functions for working with file paths, such as extracting the filename or directory from a path.
- File::Copy: Provides functions for copying files.
- File::Find: Provides functions for searching a directory tree for files that match a pattern.
- File::Path: Provides functions for creating and removing directories.
- File::Spec: Provides functions for working with file paths that are platform-independent.
Overall, file handling is an important aspect of programming in Perl. Whether you need to read data from a file, write data to a file, or perform more complex file operations, Perl provides a variety of tools and modules to help you get the job done.
In Perl, a FileHandle associates a name to an external file, that can be used until the end of the program or until the FileHandle is closed. In short, a FileHandle is like a connection that can be used to modify the contents of an external file and a name is given to the connection (the FileHandle) for faster access and ease.
The three basic FileHandles in Perl are STDIN, STDOUT, and STDERR, which represent Standard Input, Standard Output, and Standard Error devices respectively. File Handling is usually done through the open function.
Syntax: open(FileHandle, Mode, FileName);
Parameters:
- FileHandle- The reference to the file, that can be used within the program or until its closure.
- Mode- Mode in which a file is to be opened.
- FileName- The name of the file to be opened.
Also, Mode and FileName can be clubbed to form a single expression for opening.
Syntax: open(FileHandle, Expression);
Parameters:
- FileHandle- The reference to the file, that can be used within the program or until its closure.
- Expression- Mode and FileName clubbed together.
The FileHandle is closed using the close function.
Syntax: close(FileHandle);
Parameters:
- FileHandle- The FileHandle to be closed.
Reading from and Writing to a File using FileHandle
Reading from a FileHandle can be done through the print function.
Syntax: print(<FileHandle>);
Parameters:
- FileHandle- FileHandle opened in read mode or a similar mode.
Writing to a File can also be done through the print function.
Syntax: print FileHandle String
Parameters:
- FileHandle- FileHandle opened in write mode or a similar mode.
- String- The String to be inserted in the file.
Different Modes in File Handling
Mode |
Explanation |
“<“ |
Read Only Mode |
“>” |
Creates file (if necessary), Clears the contents of the File and Writes to it |
“>>” |
Creates file (if necessary), Appends to the File |
“+<“ |
Reads and Writes but does NOT Create |
“+>” |
Creates file (if necessary), Clears, Reads and Writes |
“+>>” |
Creates file (if necessary), Reads and Appends |
Examples: Consider a file Hello.txt containing the string “Welcome to GeeksForGeeks!!!” initially.
- Mode = “<“ This is read-only Mode. This mode is used to Read the content line by line from the file.
Perl
#!/usr/bin/perl
open (r, "<" , "Hello.txt" );
print (<r>);
close (r);
|
Output:
Mode = “>” This is write-only Mode. Original contents of the File are cleared once it is opened in this Mode. It creates a new File with the same name, if one is not found.
Perl
#!/usr/bin/perl
open (r, "<" , "Hello.txt" );
print ( "Existing Content of Hello.txt: " . <r>);
open (w, ">" , "Hello.txt" );
seek r, 0, 0;
print "\nWriting to File..." ;
print w "Content of this file is changed" ;
close (w);
seek r, 0, 0;
print ( "\nUpdated Content of Hello.txt: " .<r>);
close (r);
|
- Output:

- Mode=”>>” This is Append Mode. Original content of the File is not cleared when it is opened in this Mode. This Mode cannot be used to overwrite as the String always attaches at the End. It creates a new File with the same name, if one is not found.
Perl
#!/usr/bin/perl
open (r, "<" , "Hello.txt" );
print ( "Existing Content of Hello.txt: " . <r>);
open (A, ">>" , "Hello.txt" );
seek r, 0, 0;
print "\nAppending to File..." ;
print A " Hello Geeks!!!" ;
close (A);
seek r, 0, 0;
print ( "\nUpdated Content of Hello.txt: " .<r>);
close (r);
|
Output:

- Mode = “+<“ This is Read-Write Mode. This can be used to overwrite an existing String in File. It cannot create a new File.
Perl
#!/usr/bin/perl
open (rw, "+<" , "Hello.txt" );
print ( "Existing Content of Hello.txt: " .<rw>);
print rw "Added using Read-Write Mode." ;
seek rw, 0, 0;
print ( "\nUpdated contents of Hello.txt: " .<rw>);
close (rw);
|
Output:

Mode = “+>” This is Read-Write Mode. The difference between “+<” and “+>” is that “+>” can create a new File, if one with the name is not found, but a “+<” cannot.
Perl
#!/usr/bin/perl
open (r, "<" , "Hello.txt" );
print ( "Existing Content of Hello.txt: " . <r>);
close (r);
open (rw, "+>" , "Hello.txt" );
print ( "\nContents of Hello.txt gets cleared..." );
print rw "Hello!!! This is updated file." ;
seek rw, 0, 0;
print ( "\nUpdated Content of Hello.txt: " .<rw>);
close (rw);
|
Output:

Mode = “+>>” This is Read-Append Mode. This can be used to Read from a File as well as Append to it. A new File with same name is created, if one is not Found.
Perl
open (ra, "+>>" , "Hello.txt" );
seek ra, 0, 0;
print ( "Existing Content of the File: " . <ra>);
print "\nAppending to the File...." ;
print ra "Added using Read-Append Mode" ;
seek ra, 0, 0;
print ( "\nUpdated content of the File: " . <ra>);
close (rw);
|
Output:

- Open a FileHandle to write i.e. “>”, “>>”, “+<“, “+>” or “+>>”.
- Select the FileHandle using select function.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
20 Feb, 2023
Like Article
Save Article