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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {| Border = 1 | '''Time''' | '''Narration''' |- | 00:01 | Welcome to the''' Spoken Tutorial''' on''' File Handling in PERL.''' |- | 00:06 | In this tutorial we will...")
 
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
   
 
   
 
 
{| Border = 1
 
{| Border = 1
 
|  '''Time'''
 
|  '''Time'''
Line 7: Line 6:
 
|-
 
|-
 
|  00:01
 
|  00:01
|  Welcome to the''' Spoken Tutorial''' on''' File Handling in PERL.'''
+
|  Welcome to the''' Spoken Tutorial''' on''' File Handling''' in '''PERL.'''
  
 
|-
 
|-
 
|  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'''
* Open a file in '''read mode'''
+
Write to a file
* Write to a file
+
Open a file in '''append mode'''
* Open a file in '''append mode'''
+
Close the '''file handle'''.
* 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
* '''Ubuntu Linux 12.04''' operating system
+
'''Perl 5.14.2''' and the
* '''Perl 5.14.2'''
+
''' gedit''' Text Editor.
* and the''' gedit''' Text Editor
+
  
 
|-
 
|-
 
| 00:28
 
| 00:28
|  You can use any text editor of your choice.
+
|  You can use any '''text editor''' of your choice.
  
 
|-
 
|-
Line 41: 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
+
open a file
* read from a file
+
read from a file
* write to a file and
+
write to a file and
* close a file
+
close a file.
  
 
|-
 
|-
 
|00:54
 
|00:54
|  The default file handles are  
+
|  The default '''file handle'''s are:
 
+
'''STDIN'''
* '''STDIN'''
+
'''STDOUT'''  
* '''STDOUT'''  
+
'''STDERR'''
* '''STDERR'''
+
  
 
|-
 
|-
Line 60: Line 56:
 
|-
 
|-
 
| 01:05
 
| 01:05
|  In the syntax''', FILEHANDLE''' is the''' file handle''' returned by the '''open '''function.
+
|  In the syntax, '''FILEHANDLE''' is the''' file handle''' returned by the '''open '''function.
  
 
|-
 
|-
Line 68: Line 64:
 
|-
 
|-
 
| 01:18
 
| 01:18
|  '''EXPR''' is the physical filename used to read or write. In this case, '''“First.txt” '''is the filename.
+
|  '''EXPR''' is the physical filename used to read or write. In this case, “First.txt” is the filename.
  
 
|-
 
|-
Line 80: Line 76:
 
|-
 
|-
 
|  01:38
 
|  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'''.
+
|  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'''.
  
 
|-
 
|-
Line 88: Line 84:
 
|-
 
|-
 
| 01:55
 
| 01:55
|  Save the file and close''' gedit'''.
+
'''Save''' the file and close''' gedit'''.
  
 
|-
 
|-
 
|  01:59
 
|  01:59
|  Now we will look at a '''Perl '''program that opens the file''' first.txt''' and reads the content.
+
|  Now we will look at a '''Perl '''program that opens the file 'first.txt' and reads the content.
  
 
|-
 
|-
 
|  02:07
 
|  02:07
|  Let me open the sample program''' openfile.pl''' which I have already saved.
+
|  Let me open the sample program 'openfile.pl' which I have already saved.
  
 
|-
 
|-
 
| 02:13
 
| 02:13
|  Type''' gedit openfile dot pl ampersand''' and press''' Enter'''.
+
|  Type: '''gedit openfile dot pl ampersand''' and press''' Enter'''.
  
 
|-
 
|-
Line 112: Line 108:
 
|-
 
|-
 
| 02:28
 
| 02:28
|  The''' open''' '''function''' opens a file for reading.
+
|  The''' open''' function opens a file for reading.
  
 
|-
 
|-
Line 124: Line 120:
 
|-
 
|-
 
| 02:44
 
| 02:44
|  If you fail to specify the '''Mode''', by default the file will be opened in “'''READ'''” mode.
+
|  If you fail to specify the '''Mode''', by default the file will be opened in “READ” mode.
  
 
|-
 
|-
 
| 02:50
 
| 02:50
|  The third parameter''' first.txt''' is the''' filename''' from where the data has to be read.
+
|  The third parameter 'first.txt' is the filename from where the data has to be read.
  
 
|-
 
|-
 
| 02:57
 
| 02:57
|  What will happen if the file''' first.txt''' does not exist?
+
|  What will happen if the file 'first.txt' does not exist?
  
 
|-
 
|-
 
| 03:02
 
| 03:02
|  The script will die with the appropriate error message stored in the''' dollar exclamation ($!)'''variable.
+
|  The script will die with the appropriate '''error message''', stored in the''' dollar exclamation ($!)'''variable.
  
 
|-
 
|-
 
|  03:08
 
|  03:08
|  The '''while '''loop will  read line by line  and loop through the'''<nowiki> <</nowiki>DATA>''' file
+
|  The '''while '''loop will  read line by line  and loop through the '''<nowiki> <</nowiki>DATA>''' file  until all the lines have been read.
  until all the lines have been read.
+
  
 
|-
 
|-
 
| 03:17
 
| 03:17
Print''' dollar underscore''' ('''$_)''' variable will print the contents of the current line.
+
|  '''Print dollar underscore''' ('$_') variable will print the contents of the current line.
  
 
|-
 
|-
 
|  03:22
 
|  03:22
|  Lastly, close the file with the''' FILEHANDLE''' name, which we had given in the '''open''' statement.
+
|  Lastly, close the file with the''' FILEHANDLE''' name which we had given in the '''open''' statement.
 
+
  
 
|-
 
|-
Line 158: Line 152:
 
|-
 
|-
 
|  03:36
 
|  03:36
|  Now, press''' Ctrl+S''' to save the file.
+
|  Now, press''' Ctrl+S''' to '''save''' the file.
  
 
|-
 
|-
 
|  03:40
 
|  03:40
|  Let us execute the program.
+
|  Let us '''execute''' the program.
  
 
|-
 
|-
 
| 03:42
 
| 03:42
|  Switch back to the''' terminal''' and type '''perl openfile dot pl''' and press''' Enter'''.
+
|  Switch back to the terminal and type '''perl openfile dot pl''' and press''' Enter'''.
  
 
|-
 
|-
Line 182: Line 176:
 
|-
 
|-
 
| 04:03
 
| 04:03
|  The '''open '''statement with greater than''' (>)''' symbol defines the '''WRITE''' mode.
+
|  The '''open '''statement with '''greater than''' (>) symbol defines the '''WRITE''' mode.
  
 
|-
 
|-
Line 190: Line 184:
 
|-
 
|-
 
|  04:13
 
|  04:13
|  Let me open the sample program''' writefile.pl''' which I have already saved.
+
|  Let me open the sample program 'writefile.pl' which I have already saved.
  
 
|-
 
|-
Line 198: Line 192:
 
|-
 
|-
 
|  04:21
 
|  04:21
|  Now, type''' gedit writefile dot pl ampersand''' and press '''Enter.'''
+
|  Now, type:''' gedit writefile dot pl ampersand''' and press '''Enter.'''
  
 
|-
 
|-
Line 210: Line 204:
 
|-
 
|-
 
| 04:37
 
| 04:37
|  The '''open function '''opens a file '''second.txt''' in '''write mode.'''
+
|  The '''open''' function opens a file 'second.txt' in "write" mode.
  
 
|-
 
|-
 
| 04:44
 
| 04:44
|  “>” Greater than symbol before the filename denotes the '''write mode.'''
+
|  “>” - ''' Greater than'''  symbol before the filename denotes the "write" mode.
  
 
|-
 
|-
 
| 04:49
 
| 04:49
|  The first parameter '''FILE1''' is the''' FILEHANDLE'''.
+
|  The first parameter "FILE1" is the''' FILEHANDLE'''.
  
 
|-
 
|-
 
| 04:53
 
| 04:53
|  The '''print function''' prints the given text to''' FILEHANDLE'''. i.e''' FILE1'''
+
|  The '''print''' function prints the given text to''' FILEHANDLE'''. i.e 'FILE1'.
  
 
|-
 
|-
 
| 04:59
 
| 04:59
|  Now, press''' Ctrl+S''' to save the file
+
|  Now, press''' Ctrl+S''' to '''save'''  the file.
  
 
|-
 
|-
 
|  05:03
 
|  05:03
|  Let us execute the program.
+
|  Let us ''' execute'''  the program.
  
 
|-
 
|-
 
| 05:05
 
| 05:05
|  Switch back to the '''terminal''' and type '''perl writefile dot pl''' and press''' Enter'''.
+
|  Switch back to the terminal and type: '''perl writefile dot pl''' and press''' Enter'''.
  
 
|-
 
|-
 
|  05:12
 
|  05:12
|  Now let us check whether the text has been written in '''second.txt''' file.
+
|  Now, let us check whether the text has been written in 'second.txt' file.
  
 
|-
 
|-
 
| 05:18
 
| 05:18
|  Type''' gedit second.txt''' and press '''Enter'''.
+
|  Type:''' gedit second.txt''' and press '''Enter'''.
  
 
|-
 
|-
 
|  05:23
 
|  05:23
|  We can see the text''' Working with files makes data storage and retrieval a simple task! '''In our second.txt file.
+
|  We can see the text: "Working with files makes data storage and retrieval a simple task!" in our 'second.txt' file.
  
 
|-
 
|-
 
|05:32
 
|05:32
|  Let us close the '''second.txt '''file.
+
|  Let us close the 'second.txt' file.
  
 
|-
 
|-
 
|  05:35
 
|  05:35
|  What will happen if we open the same file again in '''write mode'''?  Let us see that.
+
|  What will happen if we open the same file again in "write" mode?  Let us see that.
 
|-
 
|-
 
|  05:41
 
|  05:41
| In the '''writefile.pl''', Comment the previous '''print''' '''Statement'''.
+
| In the 'writefile.pl', ''' comment''' the previous '''print''' statement.
  
 
|-
 
|-
 
| 05:46
 
| 05:46
| Add the below '''print''' command.  
+
| Add the '''print''' command shown below.  
  
 
|-
 
|-
 
|  05:48
 
|  05:48
|  Now, press''' Ctrl+S''' to save the file.  Let us execute the program.
+
|  Now, press''' Ctrl+S''' to ''' save'''  the file.  Let us execute the program.
  
 
|-
 
|-
 
|  05:54
 
|  05:54
|  Switch back to the '''terminal''' and type '''perl writefile dot pl''' and press''' Enter'''.
+
|  Switch back to the terminal and type '''perl writefile dot pl''' and press''' Enter'''.
  
 
|-
 
|-
 
|  06:00
 
|  06:00
|  Now let us check the''' second.txt''' file once again.
+
|  Now, let us check the 'second.txt' file once again.
  
 
|-
 
|-
 
|  06:04
 
|  06:04
|  Type''' gedit second.txt''' and press '''Enter'''.
+
|  Type: "gedit second.txt" and press '''Enter'''.
  
 
|-
 
|-
 
| 06:09
 
| 06:09
|  We can see the output “'''Greater than symbol (>) will overwrite the content of the file!" '''
+
|  We can see the output: “Greater than symbol (>) overwrites the content of the file!"  
  
 
|-
 
|-
 
| 06:14
 
| 06:14
|  The previous content of the '''second.txt''' file has been overwritten.
+
|  The previous contents of the 'second.txt' file has been overwritten.
  
 
|-
 
|-
 
| 06:19
 
| 06:19
|  This is because we had opened the file in the '''write mode''' again.
+
|  This is because, we had opened the file in the "write" mode again.
  
 
|-
 
|-
 
| 06:24
 
| 06:24
| Let us close the '''second.txt''' file.
+
| Let us close the 'second.txt' file.
  
 
|-
 
|-
Line 301: Line 295:
 
|-
 
|-
 
|  06:32
 
|  06:32
|  The '''open '''statement with two '''greater than''' (>> ) symbols denotes the '''APPEND''' mode.
+
|  The '''open '''statement with two '''greater than''' (>>) symbols denotes the "APPEND" mode.
  
 
|-
 
|-
 
|  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 314: Line 307:
 
|-
 
|-
 
|  06:52
 
|  06:52
|Comment the previous '''print''' '''statement''' as it is already executed.
+
|''' Comment''' the previous '''print''' statement, as it is already executed.
  
 
|-
 
|-
 
|  06:57
 
|  06:57
|  Add the line '''print FILE1''' within double quotes ''' Two greater than symbols >> opens the file in append mode''' to append to the existing data.
+
|  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
 
|  07:07
|  Now, press''' Ctrl+S''' to save the file.
+
|  Now, press''' Ctrl+S''' to '''save''' the file.
  
 
|-
 
|-
 
|  07:11
 
|  07:11
|  Let us execute the program.
+
|  Let us '''execute''' the program.
  
 
|-
 
|-
 
|  07:14
 
|  07:14
|  Switch back to the '''terminal''' and type '''perl writefile dot pl''' and press''' Enter'''.
+
|  Switch back to the '''terminal''' and type: '''perl writefile dot pl''' and press''' Enter'''.
  
 
|-
 
|-
 
|  07:20
 
|  07:20
|  Now let us check whether the text has been appended to the''' second.txt''' file.
+
|  Now, let us check whether the text has been appended to the 'second.txt' file.
  
 
|-
 
|-
 
| 07:26
 
| 07:26
|  Type''' gedit second.txt''' and press '''Enter'''.
+
|  Type: ''' gedit second.txt''' and press '''Enter'''.
  
 
|-
 
|-
 
|  07:31
 
|  07:31
|  We can see the text has been appended in our''' second.txt''' file.
+
|  We can see the text has been appended in our 'second.txt' file.
  
 
|-
 
|-
 
| 07:36
 
| 07:36
| Let us close the '''second.txt''' file.
+
| Let us close the 'second.txt' file.
  
 
|-
 
|-
Line 358: Line 351:
 
|-
 
|-
 
|  07:49
 
|  07:49
|  This brings us to the end of this tutorial. Let us summarise.
+
|  This brings us to the end of this tutorial. Let us summarize.
  
 
|-
 
|-
 
|  07:53
 
|  07:53
|  In this tutorial we learnt to  
+
|  In this tutorial, we learnt to:
* Open a file in read mode
+
Open a file in "read" mode
* Write to a file
+
Write to a file
* Open a file in append mode and
+
Open a file in "append" mode and
* Close the file handle
+
Close the '''file handle'''.
  
 
|-
 
|-
 
|  08:03
 
|  08:03
|  Here is an assignment for you.
+
|  Here is an assignment for you. Change the '''file attribute''' to "+>" in the 'writefile.pl' program.
Change the file attribute to''' +>''' in the''' writefile.pl''' program.
+
  
 
|-
 
|-
 
| 08:11
 
| 08:11
|   Save and execute the program.
+
| '''Save''' and '''execute''' the program.
  
 
|-
 
|-
 
| 08:14
 
| 08:14
|  Open the''' second.txt''' file to see the output.
+
|  Open the 'second.txt' file to see the output.
  
 
|-
 
|-
 
| 08:17
 
| 08:17
|   Analyse the usage of file attribute''' +>'''
+
| Analyze the usage of file attribute "+>".
  
 
|-
 
|-
 
|  08:22
 
|  08:22
|  The video at the following link summarises the Spoken Tutorial project.
+
|  The video at the following link summarizes the '''Spoken Tutorial''' project. Please download and watch it.
Please download and watch it
+
  
 
|-
 
|-
 
|  08:29
 
|  08:29
|  The''' Spoken Tutorial Project''' Team
+
|  The''' Spoken Tutorial Project''' team:
* conducts workshops using spoken tutorials and
+
conducts workshops using spoken tutorials and
* gives certificates on passing online tests.
+
gives certificates on passing online tests.
  
 
|-
 
|-
Line 409: Line 400:
 
|-
 
|-
 
|  08:53
 
|  08:53
|  This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching.
+
|  This is Nirmala Venkat from '''IIT Bombay''', signing off. Thanks for watching.
  
 
|}
 
|}

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.

Contributors and Content Editors

PoojaMoolya, Sandhya.np14