Difference between revisions of "PERL/C3/File-Handling/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
| (One intermediate revision by one other user not shown) | |||
| Line 11: | Line 11: | ||
| 00:06 | | 00:06 | ||
| In this tutorial, we will learn how to: | | 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'''. | |
|- | |- | ||
| 00:17 | | 00:17 | ||
| For this tutorial, I am using: | | For this tutorial, I am using: | ||
| − | + | '''Ubuntu Linux 12.04''' operating system | |
| − | + | '''Perl 5.14.2''' and the | |
| − | + | ''' gedit''' Text Editor. | |
|- | |- | ||
| Line 38: | Line 38: | ||
| 00:43 | | 00:43 | ||
| The basic operations we can do with files in '''Perl '''are: | | 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. | |
|- | |- | ||
|00:54 | |00:54 | ||
| The default '''file handle'''s are: | | The default '''file handle'''s are: | ||
| − | + | '''STDIN''' | |
| − | + | '''STDOUT''' | |
| − | + | '''STDERR''' | |
|- | |- | ||
| Line 251: | Line 251: | ||
|- | |- | ||
| 05:41 | | 05:41 | ||
| − | | In the 'writefile.pl', ''' comment''' the previous '''print''' | + | | In the 'writefile.pl', ''' comment''' the previous '''print''' statement. |
|- | |- | ||
| 05:46 | | 05:46 | ||
| − | | | + | | Add the '''print''' command shown below. |
|- | |- | ||
| Line 271: | Line 271: | ||
|- | |- | ||
| 06:04 | | 06:04 | ||
| − | | Type: | + | | Type: "gedit second.txt" and press '''Enter'''. |
|- | |- | ||
| Line 299: | Line 299: | ||
|- | |- | ||
| 06:38 | | 06:38 | ||
| − | | Now I will open the '''writefile dot pl''' again in '''gedit'''. | + | | Now, I will open the '''writefile dot pl''' again in '''gedit'''. |
|- | |- | ||
| Line 356: | Line 356: | ||
| 07:53 | | 07:53 | ||
| In this tutorial, we learnt to: | | 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'''. | |
|- | |- | ||
| Line 384: | Line 384: | ||
| 08:29 | | 08:29 | ||
| The''' Spoken Tutorial Project''' team: | | The''' Spoken Tutorial Project''' team: | ||
| − | + | conducts workshops using spoken tutorials and | |
| − | + | gives certificates on passing online tests. | |
|- | |- | ||
Latest revision as of 18:16, 20 February 2017
| Time | Narration |
| 00:01 | Welcome to the Spoken Tutorial on File Handling in PERL. |
| 00:06 | 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. |
| 00:17 | For this tutorial, I am using:
Ubuntu Linux 12.04 operating system Perl 5.14.2 and the gedit Text Editor. |
| 00:28 | You can use any text editor of your choice. |
| 00:32 | To follow this tutorial, you should have working knowledge of Perl programming. |
| 00:37 | If not, then go through the relevant Perl spoken tutorials on the spoken tutorial website. |
| 00:43 | 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. |
| 00:54 | The default file handles are:
STDIN STDOUT STDERR |
| 01:02 | This is the syntax for open function. |
| 01:05 | In the syntax, FILEHANDLE is the file handle returned by the open function. |
| 01:11 | MODE represents the mode of opening the file. For eg: read, write, etc. |
| 01:18 | EXPR is the physical filename used to read or write. In this case, “First.txt” is the filename. |
| 01:27 | There is another way to write the open function, as shown here. |
| 01:32 | Let us understand how to open an existing file and read the data from it. |
| 01:38 | 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. |
| 01:51 | In the first dot txt file, type the following text: |
| 01:55 | Save the file and close gedit. |
| 01:59 | Now we will look at a Perl program that opens the file 'first.txt' and reads the content. |
| 02:07 | Let me open the sample program 'openfile.pl' which I have already saved. |
| 02:13 | Type: gedit openfile dot pl ampersand and press Enter. |
| 02:19 | In the openfile dot pl file, type the following code as displayed on the screen. |
| 02:25 | Let us understand the code now. |
| 02:28 | The open function opens a file for reading. |
| 02:33 | The first parameter DATA is the filehandle which allows Perl to refer to the file in future. |
| 02:40 | The second parameter “<” less than symbol denotes the READ mode. |
| 02:44 | If you fail to specify the Mode, by default the file will be opened in “READ” mode. |
| 02:50 | The third parameter 'first.txt' is the filename from where the data has to be read. |
| 02:57 | What will happen if the file 'first.txt' does not exist? |
| 03:02 | The script will die with the appropriate error message, stored in the dollar exclamation ($!)variable. |
| 03:08 | The while loop will read line by line and loop through the <DATA> file until all the lines have been read. |
| 03:17 | Print dollar underscore ('$_') variable will print the contents of the current line. |
| 03:22 | Lastly, close the file with the FILEHANDLE name which we had given in the open statement. |
| 03:29 | Closing a file prevents any accidental file changes or overwriting of the content. |
| 03:36 | Now, press Ctrl+S to save the file. |
| 03:40 | Let us execute the program. |
| 03:42 | Switch back to the terminal and type perl openfile dot pl and press Enter. |
| 03:51 | The output is displayed as shown. |
| 03:54 | This is the same content that we saw earlier in first dot txt file. |
| 03:59 | Next we will see how to write data into a file. |
| 04:03 | The open statement with greater than (>) symbol defines the WRITE mode. |
| 04:08 | Filename represents the name of the file where the data has to be written. |
| 04:13 | Let me open the sample program 'writefile.pl' which I have already saved. |
| 04:19 | Switch to the terminal. |
| 04:21 | Now, type: gedit writefile dot pl ampersand and press Enter. |
| 04:29 | In the writefile dot pl file, type the following code as displayed on the screen. |
| 04:34 | Let me explain the code now. |
| 04:37 | The open function opens a file 'second.txt' in "write" mode. |
| 04:44 | “>” - Greater than symbol before the filename denotes the "write" mode. |
| 04:49 | The first parameter "FILE1" is the FILEHANDLE. |
| 04:53 | The print function prints the given text to FILEHANDLE. i.e 'FILE1'. |
| 04:59 | Now, press Ctrl+S to save the file. |
| 05:03 | Let us execute the program. |
| 05:05 | Switch back to the terminal and type: perl writefile dot pl and press Enter. |
| 05:12 | Now, let us check whether the text has been written in 'second.txt' file. |
| 05:18 | Type: gedit second.txt and press Enter. |
| 05:23 | We can see the text: "Working with files makes data storage and retrieval a simple task!" in our 'second.txt' file. |
| 05:32 | Let us close the 'second.txt' file. |
| 05:35 | What will happen if we open the same file again in "write" mode? Let us see that. |
| 05:41 | In the 'writefile.pl', comment the previous print statement. |
| 05:46 | Add the print command shown below. |
| 05:48 | Now, press Ctrl+S to save the file. Let us execute the program. |
| 05:54 | Switch back to the terminal and type perl writefile dot pl and press Enter. |
| 06:00 | Now, let us check the 'second.txt' file once again. |
| 06:04 | Type: "gedit second.txt" and press Enter. |
| 06:09 | We can see the output: “Greater than symbol (>) overwrites the content of the file!" |
| 06:14 | The previous contents of the 'second.txt' file has been overwritten. |
| 06:19 | This is because, we had opened the file in the "write" mode again. |
| 06:24 | Let us close the 'second.txt' file. |
| 06:27 | Next, we will see how to append data to an existing file. |
| 06:32 | The open statement with two greater than (>>) symbols denotes the "APPEND" mode. |
| 06:38 | Now, I will open the writefile dot pl again in gedit. |
| 06:44 | In the open statement, type two greater (>>) than symbols. This will denote that the file is in append mode. |
| 06:52 | Comment the previous print statement, as it is already executed. |
| 06:57 | Add the line: print FILE1 within double quotes "Two greater than symbols (>>) open the file in append mode", to append to the existing data. |
| 07:07 | Now, press Ctrl+S to save the file. |
| 07:11 | Let us execute the program. |
| 07:14 | Switch back to the terminal and type: perl writefile dot pl and press Enter. |
| 07:20 | Now, let us check whether the text has been appended to the 'second.txt' file. |
| 07:26 | Type: gedit second.txt and press Enter. |
| 07:31 | We can see the text has been appended in our 'second.txt' file. |
| 07:36 | Let us close the 'second.txt' file. |
| 07:39 | Similarly, there are other modes also. |
| 07:42 | Try out these options on your own and understand what happens. |
| 07:49 | This brings us to the end of this tutorial. Let us summarize. |
| 07:53 | 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. |
| 08:03 | Here is an assignment for you. Change the file attribute to "+>" in the 'writefile.pl' program. |
| 08:11 | Save and execute the program. |
| 08:14 | Open the 'second.txt' file to see the output. |
| 08:17 | Analyze the usage of file attribute "+>". |
| 08:22 | The video at the following link summarizes the Spoken Tutorial project. Please download and watch it. |
| 08:29 | The Spoken Tutorial Project team:
conducts workshops using spoken tutorials and gives certificates on passing online tests. |
| 08:37 | For more details, please write to us. |
| 08:41 | Spoken Tutorial project is funded by NMEICT, MHRD, Government of India. |
| 08:48 | More information on this mission is available at this link. |
| 08:53 | This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching. |