Perl functions references-open Function - Perl Scripting

What is open function?


This function opens a file using the specified file handle. The file handle may be an expression with the resulting value which is used as the handle. If the file name is not mentioned, a variable with the same name as the file handle is used (this should be a scalar variable with a string value referring to the file name). The special file name '-' refers to STDIN and '>-' refers to STDOUT.


Following is the simple syntax for this function −

Return Value

This function returns o on failure and 1 on success.


Following is the syntax to open file.txt in read-only mode. Here less than < sign indicates that file has to be opend in read-only mode.
Here DATA is the file handle which will be used to read the file. Here is the example which will open a file and will print its content over the screen.
Following is the syntax to open file.txt in writing mode. Here less than > sign indicates that file has to be opened in writing mode −
This example actually truncates (empties) the file before opening it for writing, which may not be considered as the desired effect. If you want to open a file for reading and writing, you can put a plus sign before the > or < characters.
For example, to open a file for updating without truncating it −
To truncate the file first −
You can open a file in append mode. In this mode writing point will be set to the end of the file.
A double >> opens the file for appending, placing the file pointer at the end, so that the user can immediately start appending information. However, you can’t read from it unless you put a plus sign in front of it −
Following is the table which gives the possible values of different modes.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Perl Scripting Topics