- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Opening and Closing Files in Perl
There are following two functions with multiple forms, which can be used to open any new or existing file in Perl.
open FILEHANDLE, EXPR open FILEHANDLE sysopen FILEHANDLE, FILENAME, MODE, PERMS sysopen FILEHANDLE, FILENAME, MODE
Here FILEHANDLE is the file handle returned by the open function and EXPR is the expression having file name and mode of opening the file.
Open Function
Following is the syntax to open file.txt in read-only mode. Here less than < sign indicates that file has to be opened in read-only mode.
open(DATA, "<file.txt");
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.
#!/usr/bin/perl open(DATA, "<file.txt") or die "Couldn't open file file.txt, $!"; while(<DATA>) { print "$_"; }
Following is the syntax to open file.txt in writing mode. Here less than > sign indicates that file has to be opened in the writing mode.
open(DATA, ">file.txt") or die "Couldn't open file file.txt, $!";
This example actually truncates (empties) the file before opening it for writing, which may not be 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 −
open(DATA, "+<file.txt"); or die "Couldn't open file file.txt, $!";
To truncate the file first −
open DATA, "+>file.txt" or die "Couldn't open file file.txt, $!";
You can open a file in the append mode. In this mode, writing point will be set to the end of the file.
open(DATA,">>file.txt") || die "Couldn't open file file.txt, $!";
A double >> opens the file for appending, placing the file pointer at the end, so that you can immediately start appending information. However, you can't read from it unless you also place a plus sign in front of it −
open(DATA,"+>>file.txt") || die "Couldn't open file file.txt, $!";
Following is the table, which gives the possible values of different modes
Sr.No | Entities & Definition |
---|---|
1 | < or r Read Only Access |
2 | < or w Creates, Writes, and Truncates |
3 | < or a Writes, Appends, and Creates |
4 | +< or r+ Reads and Writes |
5 | +> or w+ Read Only Access |
6 | +>> or a+ Read Only Access |
Sysopen Function
The sysopen function in Perl is similar to the main open function, except that it uses the system open() function, using the parameters supplied to it as the parameters for the system function −
For example, to open a file for updating, emulating the +<filename format from open −
sysopen(DATA, "file.txt", O_RDWR);
Or to truncate the file before updating −
sysopen(DATA, "file.txt", O_RDWR|O_TRUNC );
You can use O_CREAT to create a new file and O_WRONLY- to open file in write only mode and O_RDONLY - to open file in read only mode.
The PERMS argument specifies the file permissions for the file specified, if it has to be created. By default it takes 0x666.
Following is the table, which gives the possible values of MODE.
Sr.No | Entities & Definition |
---|---|
1 | O_RDWR Read and Write |
2 | O_RDONLY Read Only |
3 | O_WRONLY Write Only |
4 | O_CREAT Create the file |
5 | O_APPEND Append the file |
6 | O_TRUNC Truncate the file |
7 | O_EXCL Stops if file already exists |
8 | O_NONBLOCK Non-Blocking usability |
Close Function
To close a file handle, and therefore disassociate the file handle from the corresponding file, you use the close function. This flushes the file handle's buffers and closes the system's file descriptor.
close FILEHANDLE close
If no FILEHANDLE is specified, then it closes the currently selected filehandle. It returns true only if it could successfully flush the buffers and close the file.
close(DATA) || die "Couldn't close file properly";
- Related Articles
- Opening and Closing Files in Python
- Methods for Closing and Opening of Contactors in Railway
- Reading and Writing Files in Perl
- Copy, Rename and Delete Files in Perl
- How to clear filters when opening, saving or closing workbook in Excel?
- Display all the Files in Perl
- Find index of closing bracket for a given opening bracket in an expression in C++
- Is A Teared Paper Chemical Change And Inreversable Change And Is Opening And Closing Cupboard A Reversable And Physical Change
- How to find all the different combinations of opening and closing brackets from the given number k using C#?
- What is the name of those cells in the leaf of a plant which control the opening and closing of stomata?
- The opening and closing of the stomatal pores depends upon:(a) oxygen (b) water in guard cells (c) temperature (d) concentration of CO2 in stomata
- Opening and reading a file with askopenfilename in Tkinter?
- Single and Double Quotes in Perl
- Current Date and Time in Perl
- Format Date and Time in Perl
