Difference between revisions of "PERL/C3/File-Handling/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''>>Title of script''':''' File Handling''' '''Author: Nirmala Venkat''' '''Keywords: Open File, Close File, Read Mode, Write Mode, Append Mode, FILEHANDLE, video tutorial'...")
 
 
Line 61: Line 61:
 
* '''STDOUT'''  
 
* '''STDOUT'''  
 
* '''STDERR'''
 
* '''STDERR'''
 
i.e '''standard input, standard output '''and '''standard error.'''
 
  
 
|-
 
|-
Line 304: Line 302:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight in second.txt file
 
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight in second.txt file
| style="border:1pt solid #000000;padding:0.097cm;"| We can see “'''Greater than symbol (>) will overwrite the content of the file!" '''
+
| style="border:1pt solid #000000;padding:0.097cm;"| 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.
 
The previous content of the '''second.txt''' file has been overwritten.
Line 343: Line 341:
  
 
'''print FILE1 "Two greater than symbols >> opens the file in append mode"'''
 
'''print FILE1 "Two greater than symbols >> opens the file in append mode"'''
 +
 +
 
to append to the existing data.
 
to append to the existing data.
 
|-
 
|-

Latest revision as of 12:03, 25 July 2015

>>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
  • Open a file in read mode
  • Write to a file
  • Open a file in append mode
  • Close the file handle
Slide 3: For this tutorial, I am using
  • Ubuntu Linux 12.04 operating system
  • Perl 5.14.2
  • and the gedit Text Editor

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:

  • open a file
  • read from a file
  • write to a file and
  • close a file
Slide 6: The default file handles are
  • STDIN
  • STDOUT
  • STDERR
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:
  • read line by line
  • and loop through the <DATA> file
  • until all the lines have been read.

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"


to append to the existing data.

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:

  • +< file – READ, WRITE
  • +> file – READ, WRITE, CREATE, TRUNCATE,
  • +>> file - READ, WRITE, CREATE, APPEND

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
  • Open a file in read mode
  • Write to a file
  • Open a file in append mode and
  • Close the file handle
Slide 11:

Assignment

Here is an assignment for you.
  1. Change the file attribute to +> in the writefile.pl program.
  2. Save and execute the program.
  3. Open the second.txt file to see the output.
  4. Analyse the usage of file attribute +>
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
  • conducts workshops using spoken tutorials and
  • gives certificates on passing online tests.

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.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat