You are reading the article Working Of Open() Function In Perl With Examples updated in October 2023 on the website Phuhoabeautyspa.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Working Of Open() Function In Perl With Examples
Introduction to Perl openWeb development, programming languages, Software testing & others
The syntax to declare open() function in Perl is as follows:
Open(FileHandle, mode, filepath);where FileHandle is the variable associated with the file that is to be opened,
mode specifies if the file is opened for reading, writing or appending and
filepath specifies the path to the location of the file that is to be opened.
Working of open() function in PerlWorking of open() function in Perl is as follows:
A variable that is associated with a file is called file handle through which the contents of the file can be read or data can be written to the file depending on how we open the file.
To open a file using a specified file handle, we make use of a function called open() function in Perl.
The open() function in Perl takes three arguments namely file handle, mode, and filepath.
FileHandle is the variable associated with the file that is to be opened.
mode specifies if the file is opened for reading, writing, or appending.
The operand < specifies the file is opened in read mode and the file can only be read and no changes can be made to the contents of the file.
ExamplesLet us discuss examples of Perl open.
Example #1Code:
use warnings; use strict; #The contents to be written to the file are stored in a string called datawrite my $datawrite = <<END; We are learning Perl END #a temporary file is created to write the data into the file #open function is used to open the file through file handler to write the data to the file print tow $datawrite; close(tow); print "Data has been written to the file successfullyn"; #open function is used to open the file through file handler to read the data from the file open(tor, '<', $pathtothefile) or die $!; #displaying the contents of the file print "The contents of the file are:n"; print $_; } close(tor);The output of the above program is as shown in the snapshot below:
In the above program, the data to be written to the file is stored in a string called datawrite. Then we are making use of open function to open the file in write mode to write the date stored in the string to the file. Then we are again making use of open function to open the file in read mode to read the data written to file and display the data as the output on the screen.
Example #2Perl program to demonstrate the working of open function to open a file in write mode to write the data to the file and then read the contents of the file by opening the file in read mode to display the contents of the file and then open the file in append mode to append the data to the file and then display the appended contents as the output on the screen:
use warnings; use strict; #The contents to be written to the file are stored in a string called datawrite my $datawrite = <<END; We are learning Perl END #a temporary file is created to write the data into the file #open function is used to open the file through file handler to write the data to the file print tow $datawrite; close(tow); print "Data has been written to the file successfullyn"; #open function is used to open the file through file handler to read the data from the file open(tor, '<', $pathtothefile) or die $!; #displaying the contents of the file print "The contents of the file are:n"; print $_; } close(tor); #The contents to be appended to the file are stored in a string called datawrite1 my $datawrite1 = <<END; Learning is fun END #open function is used to open the file through file handler to append the data to the file print tow $datawrite1; close(tow); print "Data has been appended to the file successfullyn"; #open function is used to open the file through file handler to read the data from the appended file open(tor, '<', $pathtothefile) or die $!; #displaying the contents of the file print "The contents of the file after appending are:n"; print $_; } close(tor);The output of the above program is as shown in the snapshot below:
In the above program, the data to be written to the file is stored in a string called datawrite. Then we are making use of open function to open the file in write mode to write the date stored in the string to the file. Then we are again making use of open function to open the file in read mode to read the data written to file and display the data as the output on the screen. Then again we are making use of open function in append mode to append the data to the file. Then the file is again opened using open function in read mode to read the appended contents of the file and display it on the screen.
ConclusionIn this article, we have learnt the concept of open() function in Perl through definition, syntax, and working of open() function through programming examples and their outputs.
Recommended ArticlesThis is a guide to Perl open. Here we discuss the introduction and Working of open() function in Perl with Examples for better understanding. You may also have a look at the following articles to learn more –
You're reading Working Of Open() Function In Perl With Examples
Update the detailed information about Working Of Open() Function In Perl With Examples on the Phuhoabeautyspa.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!