- 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
Reading and Writing Files in Perl
Once you have an open file handle in Perl, you need to be able to read and write information. There are a number of different ways of reading and writing data into the file.
The <FILEHANDL> Operator
The main method of reading the information from an open filehandle is the <FILEHANDLE> operator. In a scalar context, it returns a single line from the filehandle. For example −
#!/usr/bin/perl print "What is your name?\n"; $name = <STDIN>; print "Hello $name\n";
When you use the <FILEHANDLE> operator in a list context, it returns a list of lines from the specified filehandle. For example, to import all the lines from a file into an array −
#!/usr/bin/perl open(DATA,"<import.txt") or die "Can't open data"; @lines = <DATA>; close(DATA);
getc Function
The getc function returns a single character from the specified FILEHANDLE, or STDIN if none is specified −
getc FILEHANDLE getc
If there was an error, or the filehandle is at end of file, then undef is returned instead.
read Function
The read function reads a block of information from the buffered filehandle: This function is used to read binary data from the file.
read FILEHANDLE, SCALAR, LENGTH, OFFSET read FILEHANDLE, SCALAR, LENGTH
The length of the data read is defined by LENGTH, and the data is placed at the start of SCALAR if no OFFSET is specified. Otherwise data is placed after OFFSET bytes in SCALAR. The function returns the number of bytes read on success, zero at end of file, or undef if there was an error.
print Function
For all the different methods used for reading information from filehandles, the main function for writing information back is the print function.
print FILEHANDLE LIST print LIST print
The print function prints the evaluated value of LIST to FILEHANDLE, or to the current output filehandle (STDOUT by default). For example −
print "Hello World!\n";
- Related Articles
- Reading and Writing Files in Python
- Reading and Writing to text files in C#
- Reading and Writing to text files in Python
- Reading and Writing to text files in Python Program
- Reading and writing Excel files using the openpyxl module in Python
- Reading and writing binary file in C/C++
- Reading and Writing CSV File using Python
- What are reading and writing characters in C language?
- Opening and Closing Files in Perl
- Copy, Rename and Delete Files in Perl
- Reading/Writing a MS Word file in PHP
- Writing files in background in Python
- Writing files in the background in Python
- Display all the Files in Perl
- How to open an Excel file with PHPExcel for both reading and writing?
