PERL/C3/File-Handling/English
>>Title of script: File Handling
Author: Nirmala Venkat
Keywords: Open File, Close File, Read Mode, Write Mode, Append Mode, FILEHANDLE, video tutorial
Visual Cue | Narration |
Slide 1: | Welcome to the Spoken Tutorial on File Handling in PERL. |
Slide 2: | In this tutorial we will learn how to
|
Slide 3: | For this tutorial, I am using
You can use any text editor of your choice. |
Slide 4: | To follow this tutorial, you should have working knowledge of Perl programming.
If not, then go through the relevant Perl spoken tutorials on the spoken tutorial website. |
Slide 5: |
The basic operations we can do with files in Perl are:
|
Slide 6: | The default file handles are
|
Slide 6:
Opening a File: Syntax: open (FILEHANDLE, MODE, EXPR); Example: open (DATA, “<”, “First.txt”); open (DATA, “<First.txt”); |
This is the syntax for open function.
In the syntax, FILEHANDLE is the file handle returned by the open function. MODE represents the mode of opening the file. For eg: read, write, etc. EXPR is the physical filename used to read or write. In this case, “First.txt” is the filename. There is another way to write the open function, as shown here. |
Let us understand how to open an existing file and read the data from it. | |
Switch to the Terminal and type
gedit first.txt |
First we will create a text file and store some data in it.
Go to the terminal and type gedit first.txt and press Enter. |
Point to the filename first.txt in the Titlebar of gedit. | In the first dot txt file, type the following text:
Welcome to File Handling Chapter 1 Chapter 2 Chapter 3 Save the file and close gedit. |
Now we will look at a Perl program that opens the file first.txt and reads the content. | |
On the terminal >> type gedit openfile dot pl ampersand >> press Enter. | Let me open the sample program openfile.pl which I have already saved.
Type gedit openfile dot pl ampersand and press Enter. |
In the openfile dot pl file, type the following code as displayed on the screen.
Let us understand the code now. | |
#!/usr/bin/perl
open(DATA, "<” ,”first.txt") or die "Couldn't open file first.txt, $!"; |
The open function opens a file for reading.
The first parameter DATA is the filehandle which allows Perl to refer to the file in future. The second parameter “<” less than symbol denotes the READ mode. If you fail to specify the “Mode”, by default the file will be opened in “READ” mode. The third parameter first.txt is the filename from where the data has to be read. What will happen if the file first.txt does not exist? The script will die with the appropriate error message stored in the dollar exclamation ($!)variable. |
while(<DATA>){
print "$_"; } |
The while loop will:
Print dollar underscore ($_) variable will print the contents of the current line. |
close (DATA); | Lastly, close the file with the FILEHANDLE name, which we had given in the open statement. |
Closing a file prevents any accidental file changes or overwriting of the content. | |
Press Ctrl+S | Now, press Ctrl+S to save the file. |
Let us execute the program. | |
On the terminal type
perl openfile dot pl >> press Enter. |
Switch back to the terminal and type
perl openfile dot pl and press Enter. |
Highlight
Welcome to File Handling Chapter 1 Chapter 2 Chapter 3 |
The output is displayed as shown.
This is the same content that we saw earlier in first dot txt file. |
Next we will see how to write data into a file. | |
Slide 7:
Opening a file for writing: Syntax: open (FILEHANDLE, “> filename”) Example: open (DATA, “ >write.txt”); |
The open statement with greater than (>) symbol defines the WRITE mode.
Filename represents the name of the file where the data has to be written. |
Switch to the terminal. | Let me open the sample program writefile.pl which I have already saved.
Switch to the terminal. |
Type gedit writefile dot pl ampersand >>press Enter. | Now, type gedit writefile dot pl ampersand and press Enter. |
In the writefile dot pl file, type the following code as displayed on the screen.
Let me explain the code now. | |
#!/usr/bin/perl
open FILE1, ">second.txt" or die "Couldn't open file second.txt, $!"; print FILE1 "Working with files makes data storage and retrieval a simple task! \n"; |
The open function opens a file second.txt in write mode.
“>” Greater than symbol before the filename denotes the write mode. The first parameter FILE1 is the FILEHANDLE. The print function prints the given text to FILEHANDLE. i.e FILE1 |
Press Ctrl+S | Now, press Ctrl+S to save the file |
Let us execute the program. | |
Switch to terminal
perl writefile dot pl >> press Enter. |
Switch back to the terminal and type
perl writefile dot pl and press Enter. |
Now let us check whether the text has been written in second.txt file. | |
In terminal >> type gedit second.txt and press Enter. | Type gedit second.txt and press Enter. |
Highlight in second.txt file | We can see the text Working with files makes data storage and retrieval a simple task! In our second.txt file. |
Let us close the second.txt file. | |
What will happen if we open the same file again in write mode?
Let us see that. | |
Switch back to writefile dot pl. | Switch back to writefile dot pl. |
# print FILE1 "Working with files makes data storage and retrieval a simple task! \n";
print FILE1 " > overwrites the content of the file! \n"; |
Comment the previous print Statement.
Add the below print command. |
Press Ctrl+S | Now, press Ctrl+S to save the file. |
Let us execute the program. | |
On terminal >> type
perl writefile dot pl >> press Enter. |
Switch back to the terminal and type
perl writefile dot pl and press Enter. |
Now let us check the second.txt file once again. | |
In terminal, type gedit second.txt >> press Enter. | Type gedit second.txt and press Enter. |
Highlight in second.txt file | We can see the output “Greater than symbol (>) will overwrite the content of the file!"
The previous content of the second.txt file has been overwritten. This is because we had opened the file in the write mode again. |
Let us close the second.txt file. | |
Next, we will see how to append data to an existing file. | |
Slide 8:
Opening a file for Appending: Syntax: open (FILEHANDLE, “>> filename”) Example: open (DATA, “ >>write.txt”); |
The open statement with two greater than (>> ) symbols denotes the APPEND mode. |
Point to writefile.pl | Now I will open the writefile dot pl again in gedit. |
Type two greater (>>) than symbols. | In the open statement, type two greater (>>) than symbols.
This will denote that the file is in append mode. |
Comment | Comment the previous print statement as it is already executed. |
Add the line as per narration | Add the line
print FILE1 "Two greater than symbols >> opens the file in append mode"
|
Press Ctrl+S | Now, press Ctrl+S to save the file. |
Let us execute the program. | |
On the terminal >> type
perl writefile.pl& >> Press Enter |
Switch back to the terminal and type
perl writefile dot pl and press Enter. |
Now let us check whether the text has been appended to the second.txt file. | |
Type gedit second.txt >> press Enter. | Type gedit second.txt and press Enter. |
Highlight the appended text | We can see the text has been appended in our second.txt file. |
Let us close the second.txt file. | |
Similarly, there are other modes also. | |
Slide 9:
File Attributes: |
Try out these options on your own and understand what happens. |
This brings us to the end of this tutorial. Let us summarise. | |
Slide 10:
Summary |
In this tutorial we learnt to
|
Slide 11:
Assignment |
Here is an assignment for you.
|
Slide 12:
About Spoken Tutorial project
|
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it |
Slide 13:
Spoken Tutorial workshops The Spoken Tutorial Project Team |
The Spoken Tutorial Project Team
For more details, please write to us. |
Slide 14:
Acknowledgement |
Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.
More information on this mission is available at this link. |
This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching. |