Updated April 6, 2023
Introduction to Perl open file
The Perl open file is a part of file management to interact with external files helping with their file path. The open file is the function using to operate the external file with input and output operation. The open file is useful for reading, writing, and modifying the external file as per the user’s requirement. The open file is open the external file and handles the file input and output function as per necessity. The open file is useful for creating, append, and truncating the file using open file modes. The open file is a function required to connect the exterior file data and manage and manipulate the file data using modes.
Syntax
The three functions are useful for working on open file syntax.
open(file_handle, mode_of_file, file_name with path);
Description:
- The file_handle is associating with the file for open and read the file automatically.
- The file_name is the actual path of a Perl external file with the file name.
- Users can either use a direct path or make a variable of the file path and place it inside an open function.
- The mode can use for different operations and modifications of the external file.
- The different modes of the open file are below.
- The “<” symbol is utilized for only reading access with an external file.
- The “>” symbol is utilized for only writing access with an external file.
- The “>>” symbol is utilized to create, write, and appends the external file.
- The “+<” symbol is utilized for both readings and write with an external file.
- The “>+” symbol is utilized to create, read, write, and truncate the external file.
- The “+>>” symbol is utilized to create, read, write, and append the external file.
The open file Syntax with their value is below.
open(DATA, '<', 'C:\Users\fake\temp\perl_openfile.txt');
The open file with the error message Syntax is below.
open(file_handle, mode_of_file, file_name with path) or error message $! ;
How to open the file in Perl?
The download the Perl software and install it in your operating system of the device for the open file.
https://www.Perl.org/ or http://strawberryPerl.com/ are commonly using Perl software websites.
Make a file with the Perl extension in the device and save the file in the command line path to get an open file.
Example: helloo.pl or first pearl.pl
Method 1: The first method to get the open file.
- Use the open() function with file handler, required mode, and external file path in the open file.
open(DATA, '<', 'C:\Users\fake\temp\perl_openfile.txt');
Method 2: The second method to get the open file.
- Create the Perl hash variable and Initialize with the external file path and file name.
- Use the open() function with the required mode in the open file.
$perl_open_file_name = 'C: \Users\temp\perl_openfile.txt';
open(DATA, '<', $perl_open_file_name);
The open file required information return in the output screen.
print(" Perl Open File is available here! \n");
When you open the external file in the Perl technology, then the file must be close to the file handler.
close(DATA);
Combine the above procedure to get an open file.
$perl_open_file_name = 'C: \Users\temp\perl_openfile.txt';
open(DATA, '<', $perl_open_file_name);
print(" Perl Open File is available here! \n");
close(DATA);
Examples
Here are the following examples mention below
Example #1
The basic Perl open file example and output.
#perl syntax is using here…
open(DATA, '<', 'C: \Users\temp\perl_openfile.txt');
print(" Perl Open File using method1 is available here! \n");
close(DATA);
$perl_file = 'C: \Users\temp\perl_openfile1.txt';
open(DATA, '<', $perl_file);
print(" Perl Open File using method2 is available here! \n");
close(DATA);
Output:
Description:
- The open file uses the open() function with different modes.
- Users can use either the direct file path with the file name or the file path variable.
Example #2
The Perl open file with basic three modes example and output.
# Perl open file is active reading mode…
$perl_file = 'C: \Users\temp\perl_openfile.txt';
open(DATA, '<', $perl_file);
print(" Perl Open File using method2 is available here! \n");
close(DATA);
# Perl open file is active writing mode…
$perl_file1 = 'C: \Users\temp\perl_openfile1.txt';
open(DATA, '>', $perl_file1);
print(" Perl Open File is available here for writing! \n");
close(DATA);
# Perl open file is active append mode…
$perl_file2 = 'C: \Users\temp\perl_openfile2.txt';
open(DATA, '>>', $perl_file2);
print(" Perl Open File is available here for append! \n");
close(DATA);
Output:
Description:
- The read mode is used for only reading the external file, but the user cannot change file information.
- The write mode is using for writing the information.
- When the file is not available in the system, then the user can create a new external file.
- The append mode is useful for modifying the available information or adding new information to the file.
Example #3
The open file with other modes example and output.
$perl_file = 'C: \Users\temp\perl_openfile.txt';
open(DATA, '+<', $perl_file);
print(" Perl Open File is available here for read and write! \n");
close(DATA);
$perl_file1 = 'C: \Users\temp\perl_openfile1.txt';
open(DATA, '+>', $perl_file1);
print(" Perl Open File is available here for create, read, write, and truncate! \n");
close(DATA);
$perl_file2 = 'C: \Users\temp\perl_openfile2.txt';
open(DATA, '+>>', $perl_file2);
print(" Perl Open File is available here for create, read, write, and append! \n");
close(DATA);
Output:
Description:
- The modes are using for multiple operations in the external files.
Example #4
The open file with error message example and output.
my $file_name = 'C: \Users\temp\perl_openfile.txt';
open(DATA, '<', $file_name) or die $!;
print(" Perl File is opened successfully here !\n");
close(DATA);
my $file_name1 = 'c:\fake\Path\openfile.txt';
open(DATA, '<', $file_name1) or die $!;
print(" Perl File is opened successfully here !\n");
close(DATA);
Output:
Description:
- The “$!” symbol is useful to display the error message of the open file.
Example #5
The open file with exist operator example and output.
$perl_file = 'C:\path\fake\temp\perl_openfile.txt';
open(DATA, '+<', $perl_file);
if( -e $perl_file){
print(" Perl Open File is exist here! $perl_file \n");
}
else{
print(" Perl Open File is not exist here! $perl_file \n");
}
close(DATA);
Output:
Conclusion
- The Perl open file is useful for connection between Perl application users and other files.
- The Perl open file is user friendly and modifying external required files as per the user’s necessity.
- It is useful, easy to access for users and advance in the Perl language.
Recommended Articles
This is a guide to Perl open file. Here we discuss How to open the file in Perl with methods and Examples along with the outputs and descriptions. You may also have a look at the following articles to learn more –