Perl | File Handling Introduction - GeeksforGeeks

01 Apr, 2019
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 open.
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 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 StringParameters:
FileHandle- FileHandle opened in write mode or a similar mode.
String- The String to be inserted in the file.
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.#!/usr/bin/perl  # Opening a File in Read-only modeopen(r, "<", "Hello.txt");  # Printing content of the Fileprint(<r>);  # Closing the Fileclose(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.#!/usr/bin/perl  # Opening File Hello.txt in Read modeopen(r, "<", "Hello.txt");   # Printing the existing content of the fileprint("Existing Content of Hello.txt: " . <r>);   # Opening File in Write modeopen(w, ">", "Hello.txt");   # Set r to the beginning of Hello.txtseek r, 0, 0;   print "\nWriting to File...";  # Writing to Hello.txt using printprint w "Content of this file is changed";  # Closing the FileHandleclose(w);   # Set r to the beginning of Hello.txtseek r, 0, 0;   # Print the current contents of Hello.txtprint("\nUpdated Content of Hello.txt: ".<r>);   # Close the FileHandleclose(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.#!/usr/bin/perl  # Opening File Hello.txt in Read modeopen(r, "<", "Hello.txt");   # Printing the existing content of the fileprint("Existing Content of Hello.txt: " . <r>);   # Opening the File in Append modeopen(A, ">>", "Hello.txt");   # Set r to the beginning of Hello.txtseek r, 0, 0;   print "\nAppending to File...";  # Appending to Hello.txt using printprint A " Hello Geeks!!!";   # close the FileHandleclose(A);   # Set r to the beginning of Hello.txtseek r, 0, 0;   # Print the current contents of Hello.txtprint("\nUpdated Content of Hello.txt: ".<r>);  # Close the FileHandleclose(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.#!/usr/bin/perl  # Open Hello.txt in Read-Write Modeopen(rw, "+<", "Hello.txt");   # Print original contents of the File. # rw is set to the end.print("Existing Content of Hello.txt: ".<rw>);   # The string is attached at the end # of the original contents of the file.print rw "Added using Read-Write Mode.";   # Set rw to the beginning of the File for reading.seek rw, 0, 0;   # Printing the Updated content of the Fileprint("\nUpdated contents of Hello.txt: ".<rw>);  # Close the FileHandleclose(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.#!/usr/bin/perl  # Opening File Hello.txt in Read modeopen(r, "<", "Hello.txt");   # Printing the existing content of the fileprint("Existing Content of Hello.txt: " . <r>);   # Closing the Fileclose(r);  # Open Hello.txt in Read-Write Modeopen(rw, "+>", "Hello.txt");   # Original contents of the File# are cleared when the File is openedprint("\nContents of Hello.txt gets cleared...");   # The string is written to the Fileprint rw "Hello!!! This is updated file.";   # Set rw to the beginning of the File for reading.seek rw, 0, 0;   print("\nUpdated Content of Hello.txt: " .<rw>);  # Closing the Fileclose(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.# Open Hello.txt in Read-Append Modeopen(ra, "+>>", "Hello.txt");   # Set ra to the beginning of the File for reading.seek ra, 0, 0;   # Original content of the File # is NOT cleared when the File is openedprint("Existing Content of the File: " . <ra>);  print "\nAppending to the File....";  # The string is appended to the Fileprint ra "Added using Read-Append Mode";   # Set ra to the beginning of the File for reading.seek ra, 0, 0;   # Printing the updated contentprint("\nUpdated content of the File: " . <ra>);  # Closing the Fileclose(rw);Output: Redirecting Output Output can be redirected away from the Console and into a file using the select function.Syntax: select FileHandle;Parameters:FileHandle – FileHandle of the File to be selected.Steps:Open a FileHandle to write i.e. “>”, “>>”, “+<“, “+>” or “+>>”.Select the FileHandle using select function.Now, anything that is printed using the print function is redirected to the File.Example:# Open a FileHandle in Write Mode.open(File, ">", "Hello.txt");   # This sets File as the default FileHandleselect File;    # Writes to Fileprint("This goes to the File.");   # Writes to Fileprint File "\nThis goes to the File too.";   # This sets STDOUT as default FileHandleselect STDOUT; print("This goes to the console.");  # Close the FileHandle.close(File); Output in the Console:Contents of Hello.txt:Original File:Updated File:My Personal Notes
arrow_drop_upSave
Mode = “<"This is read-only Mode. This mode is used to Read the content line by line from the file.#!/usr/bin/perl  # Opening a File in Read-only modeopen(r, "<", "Hello.txt");  # Printing content of the Fileprint(<r>);  # Closing the Fileclose(r);Output:
#!/usr/bin/perl  # Opening a File in Read-only modeopen(r, "<", "Hello.txt");  # Printing content of the Fileprint(<r>);  # Closing the Fileclose(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.#!/usr/bin/perl  # Opening File Hello.txt in Read modeopen(r, "<", "Hello.txt");   # Printing the existing content of the fileprint("Existing Content of Hello.txt: " . <r>);   # Opening File in Write modeopen(w, ">", "Hello.txt");   # Set r to the beginning of Hello.txtseek r, 0, 0;   print "\nWriting to File...";  # Writing to Hello.txt using printprint w "Content of this file is changed";  # Closing the FileHandleclose(w);   # Set r to the beginning of Hello.txtseek r, 0, 0;   # Print the current contents of Hello.txtprint("\nUpdated Content of Hello.txt: ".<r>);   # Close the FileHandleclose(r); Output:
#!/usr/bin/perl  # Opening File Hello.txt in Read modeopen(r, "<", "Hello.txt");   # Printing the existing content of the fileprint("Existing Content of Hello.txt: " . <r>);   # Opening File in Write modeopen(w, ">", "Hello.txt");   # Set r to the beginning of Hello.txtseek r, 0, 0;   print "\nWriting to File...";  # Writing to Hello.txt using printprint w "Content of this file is changed";  # Closing the FileHandleclose(w);   # Set r to the beginning of Hello.txtseek r, 0, 0;   # Print the current contents of Hello.txtprint("\nUpdated Content of Hello.txt: ".<r>);   # Close the FileHandleclose(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.#!/usr/bin/perl  # Opening File Hello.txt in Read modeopen(r, "<", "Hello.txt");   # Printing the existing content of the fileprint("Existing Content of Hello.txt: " . <r>);   # Opening the File in Append modeopen(A, ">>", "Hello.txt");   # Set r to the beginning of Hello.txtseek r, 0, 0;   print "\nAppending to File...";  # Appending to Hello.txt using printprint A " Hello Geeks!!!";   # close the FileHandleclose(A);   # Set r to the beginning of Hello.txtseek r, 0, 0;   # Print the current contents of Hello.txtprint("\nUpdated Content of Hello.txt: ".<r>);  # Close the FileHandleclose(r); Output:
#!/usr/bin/perl  # Opening File Hello.txt in Read modeopen(r, "<", "Hello.txt");   # Printing the existing content of the fileprint("Existing Content of Hello.txt: " . <r>);   # Opening the File in Append modeopen(A, ">>", "Hello.txt");   # Set r to the beginning of Hello.txtseek r, 0, 0;   print "\nAppending to File...";  # Appending to Hello.txt using printprint A " Hello Geeks!!!";   # close the FileHandleclose(A);   # Set r to the beginning of Hello.txtseek r, 0, 0;   # Print the current contents of Hello.txtprint("\nUpdated Content of Hello.txt: ".<r>);  # Close the FileHandleclose(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.#!/usr/bin/perl  # Open Hello.txt in Read-Write Modeopen(rw, "+<", "Hello.txt");   # Print original contents of the File. # rw is set to the end.print("Existing Content of Hello.txt: ".<rw>);   # The string is attached at the end # of the original contents of the file.print rw "Added using Read-Write Mode.";   # Set rw to the beginning of the File for reading.seek rw, 0, 0;   # Printing the Updated content of the Fileprint("\nUpdated contents of Hello.txt: ".<rw>);  # Close the FileHandleclose(rw); Output:
#!/usr/bin/perl  # Open Hello.txt in Read-Write Modeopen(rw, "+<", "Hello.txt");   # Print original contents of the File. # rw is set to the end.print("Existing Content of Hello.txt: ".<rw>);   # The string is attached at the end # of the original contents of the file.print rw "Added using Read-Write Mode.";   # Set rw to the beginning of the File for reading.seek rw, 0, 0;   # Printing the Updated content of the Fileprint("\nUpdated contents of Hello.txt: ".<rw>);  # Close the FileHandleclose(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.#!/usr/bin/perl  # Opening File Hello.txt in Read modeopen(r, "<", "Hello.txt");   # Printing the existing content of the fileprint("Existing Content of Hello.txt: " . <r>);   # Closing the Fileclose(r);  # Open Hello.txt in Read-Write Modeopen(rw, "+>", "Hello.txt");   # Original contents of the File# are cleared when the File is openedprint("\nContents of Hello.txt gets cleared...");   # The string is written to the Fileprint rw "Hello!!! This is updated file.";   # Set rw to the beginning of the File for reading.seek rw, 0, 0;   print("\nUpdated Content of Hello.txt: " .<rw>);  # Closing the Fileclose(rw);Output:
#!/usr/bin/perl  # Opening File Hello.txt in Read modeopen(r, "<", "Hello.txt");   # Printing the existing content of the fileprint("Existing Content of Hello.txt: " . <r>);   # Closing the Fileclose(r);  # Open Hello.txt in Read-Write Modeopen(rw, "+>", "Hello.txt");   # Original contents of the File# are cleared when the File is openedprint("\nContents of Hello.txt gets cleared...");   # The string is written to the Fileprint rw "Hello!!! This is updated file.";   # Set rw to the beginning of the File for reading.seek rw, 0, 0;   print("\nUpdated Content of Hello.txt: " .<rw>);  # Closing the Fileclose(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.# Open Hello.txt in Read-Append Modeopen(ra, "+>>", "Hello.txt");   # Set ra to the beginning of the File for reading.seek ra, 0, 0;   # Original content of the File # is NOT cleared when the File is openedprint("Existing Content of the File: " . <ra>);  print "\nAppending to the File....";  # The string is appended to the Fileprint ra "Added using Read-Append Mode";   # Set ra to the beginning of the File for reading.seek ra, 0, 0;   # Printing the updated contentprint("\nUpdated content of the File: " . <ra>);  # Closing the Fileclose(rw);Output: Redirecting Output Output can be redirected away from the Console and into a file using the select function.Syntax: select FileHandle;Parameters:FileHandle – FileHandle of the File to be selected.Steps:Open a FileHandle to write i.e. “>”, “>>”, “+<“, “+>” or “+>>”.Select the FileHandle using select function.Now, anything that is printed using the print function is redirected to the File.Example:# Open a FileHandle in Write Mode.open(File, ">", "Hello.txt");   # This sets File as the default FileHandleselect File;    # Writes to Fileprint("This goes to the File.");   # Writes to Fileprint File "\nThis goes to the File too.";   # This sets STDOUT as default FileHandleselect STDOUT; print("This goes to the console.");  # Close the FileHandle.close(File); Output in the Console:Contents of Hello.txt:Original File:Updated File:My Personal Notes
arrow_drop_upSave
# Open Hello.txt in Read-Append Modeopen(ra, "+>>", "Hello.txt");   # Set ra to the beginning of the File for reading.seek ra, 0, 0;   # Original content of the File # is NOT cleared when the File is openedprint("Existing Content of the File: " . <ra>);  print "\nAppending to the File....";  # The string is appended to the Fileprint ra "Added using Read-Append Mode";   # Set ra to the beginning of the File for reading.seek ra, 0, 0;   # Printing the updated contentprint("\nUpdated content of the File: " . <ra>);  # Closing the Fileclose(rw);
Output:
 
 Output can be redirected away from the Console and into a file using the select function.
Syntax: select FileHandle;Parameters:
FileHandle – FileHandle of the File to be selected.
Steps:
Open a FileHandle to write i.e. “>”, “>>”, “+<“, “+>” or “+>>”.
Select the FileHandle using select function.
Now, anything that is printed using the print function is redirected to the File.Example:
# Open a FileHandle in Write Mode.open(File, ">", "Hello.txt");   # This sets File as the default FileHandleselect File;    # Writes to Fileprint("This goes to the File.");   # Writes to Fileprint File "\nThis goes to the File too.";   # This sets STDOUT as default FileHandleselect STDOUT; print("This goes to the console.");  # Close the FileHandle.close(File); 
Output in the Console:Contents of Hello.txt:Original File:
Updated File:
Perl-files
Picked
Perl
Perl
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Perl | split() Function
Perl | push() Function
Perl | chomp() Function
Perl | grep() Function
Perl | substr() function
Perl | exists() Function
Perl Tutorial - Learn Perl With Examples
Perl | Removing leading and trailing white spaces (trim)
Use of print() and say() in Perl
Perl | length() Function