Difference between revisions of "PERL/C2/Blocks-in-Perl/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) (Created page with '{| border=1 ! <center>'''Visual Cue'''</center> ! <center>'''Narration'''</center> |- | 00.01 |Welcome to the spoken tutorial on ''' BLOCKS''' in Perl. |- | 00.06 | In this…') |
PoojaMoolya (Talk | contribs) |
||
(12 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{| border=1 | {| border=1 | ||
− | + | |'''Time''' | |
− | + | |'''Narration''' | |
|- | |- | ||
− | | 00 | + | | 00:01 |
− | |Welcome to the spoken tutorial on ''' | + | |Welcome to the spoken tutorial on ''' Blocks in Perl'''. |
|- | |- | ||
− | | 00 | + | | 00:06 |
− | | In this tutorial, we will learn about the various ''' | + | | In this tutorial, we will learn about the various ''' blocks''' available in '''Perl'''. |
|- | |- | ||
− | |00 | + | |00:13 |
− | |I am using ''' Ubuntu Linux 12.04''' operating system and ''' Perl 5.14.2''' | + | |I am using ''' Ubuntu Linux 12.04''' operating system and ''' Perl 5.14.2'''. |
|- | |- | ||
− | |00 | + | |00:21 |
− | | I will also be using the ''' gedit''' Text Editor. | + | | I will also be using the '''gedit''' Text Editor. |
|- | |- | ||
− | |00 | + | |00:26 |
|You can use any text editor of your choice. | |You can use any text editor of your choice. | ||
− | |||
|- | |- | ||
− | |00 | + | |00:31 |
− | |As a pre-requisite, you should have basic knowledge of ''' | + | |As a pre-requisite, you should have basic knowledge of '''variables, comments''' in ''' Perl.''' |
|- | |- | ||
− | | 00 | + | | 00:38 |
− | | Knowledge of ''' | + | | Knowledge of '''data structures''' in '''PERL''' will be an added advantage. |
|- | |- | ||
− | | 00 | + | | 00:44 |
− | |Please go through the relevant spoken tutorials on the ''' | + | |Please go through the relevant spoken tutorials on the '''Spoken Tutorial''' website. |
|- | |- | ||
− | | 00 | + | | 00:50 |
− | |''' Perl''' provides 5 special '''blocks.''' | + | |'''Perl''' provides 5 special '''blocks.''' |
|- | |- | ||
− | |00 | + | |00:53 |
|These '''blocks''' get executed at various stages of a '''Perl''' program. | |These '''blocks''' get executed at various stages of a '''Perl''' program. | ||
|- | |- | ||
− | |00 | + | |00:59 |
|These blocks are: | |These blocks are: | ||
|- | |- | ||
− | | 01 | + | | 01:01 |
− | | '''BEGIN''' | + | |'''BEGIN''' , '''END''' |
|- | |- | ||
− | |01 | + | |01:03 |
− | |''' | + | |'''UNITCHECK''', '''CHECK''' |
− | |||
|- | |- | ||
− | |01 | + | |01:05 |
− | + | | '''INIT''', Let us start with understanding the ''' BEGIN''' block. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |''' INIT ''' | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | |01 | + | |01:10 |
− | |''' BEGIN''' block | + | |'''BEGIN''' block gets executed at the time of compilation. |
|- | |- | ||
− | |01 | + | |01:15 |
|So, any code written inside this block gets executed first during compilation. | |So, any code written inside this block gets executed first during compilation. | ||
|- | |- | ||
− | |01 | + | |01:22 |
|We can have several ''' BEGIN ''' blocks inside a ''' Perl''' script. | |We can have several ''' BEGIN ''' blocks inside a ''' Perl''' script. | ||
− | |||
|- | |- | ||
− | |01 | + | |01:26 |
| These blocks will get executed in the order of declaration. | | These blocks will get executed in the order of declaration. | ||
− | |||
|- | |- | ||
− | | 01 | + | | 01:31 |
− | |That is in the ''' First define First execute ''' pattern | + | |That is, in the '''First define First execute''' pattern. |
|- | |- | ||
− | |01 | + | |01:35 |
− | |The syntax for ''' BEGIN | + | |The syntax for ''' BEGIN''' block is as follows: |
|- | |- | ||
− | |01 | + | |01:40 |
| '''BEGIN''' in capital letters space open curly bracket | | '''BEGIN''' in capital letters space open curly bracket | ||
|- | |- | ||
− | | 01 | + | | 01:45 |
| Press ''' Enter.''' | | Press ''' Enter.''' | ||
− | |||
|- | |- | ||
− | |01 | + | |01:47 |
− | |Piece of code to be executed at the time of compilation | + | |Piece of code to be executed at the time of compilation, |
− | + | ||
− | + | ||
|- | |- | ||
− | |01 | + | |01:51 |
− | | Press '''Enter ''' | + | | Press '''Enter '''.Close curly bracket. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | |01 | + | |01:55 |
− | |Now, let us look at an example of '''BEGIN''' | + | |Now, let us look at an example of '''BEGIN''' block. |
− | + | ||
|- | |- | ||
− | |01 | + | |01:59 |
− | |Open the Terminal and type | + | |Open the Terminal and type: |
|- | |- | ||
− | |02 | + | |02:02 |
− | | ''' gedit beginBlock dot pl | + | | '''gedit beginBlock dot pl space ampersand''' |
|- | |- | ||
− | |02 | + | |02:08 |
|and press '''Enter.''' | |and press '''Enter.''' | ||
|- | |- | ||
− | |02 | + | |02:10 |
|This will open the ''' beginBlock dot pl ''' file in '''gedit. ''' | |This will open the ''' beginBlock dot pl ''' file in '''gedit. ''' | ||
|- | |- | ||
− | |02 | + | |02:15 |
|Type the following piece of code as displayed on the screen. | |Type the following piece of code as displayed on the screen. | ||
|- | |- | ||
− | |02 | + | |02:20 |
|Let us look at what I have written inside the script. | |Let us look at what I have written inside the script. | ||
− | |||
|- | |- | ||
− | |02 | + | |02:24 |
− | |Here, we have printed some text before and after | + | |Here, we have printed some text before and after '''BEGIN''' blocks. |
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:31 |
− | | Similarly, I have written one ''' print | + | | Similarly, I have written one '''print''' statement in each '''BEGIN''' block. |
|- | |- | ||
− | | 02 | + | | 02:37 |
|Please note, I have not given the semicolon after the ''' BEGIN''' blocks. | |Please note, I have not given the semicolon after the ''' BEGIN''' blocks. | ||
− | |||
|- | |- | ||
− | | 02 | + | | 02:42 |
− | |Putting a semicolon | + | |Putting a semicolon will result in a syntax error on execution of the program. |
− | + | ||
|- | |- | ||
− | |02 | + | |02:49 |
− | |Now, press ''' Ctrl+s''' to save the file. | + | |Now, press ''' Ctrl+s''' to '''save''' the file. |
|- | |- | ||
− | | 02 | + | | 02:53 |
− | | Then switch to terminal and execute the script by typing | + | | Then switch to terminal and execute the script by typing: |
|- | |- | ||
− | | 02 | + | | 02:58 |
|''' perl beginBlock dot pl''' | |''' perl beginBlock dot pl''' | ||
− | |||
|- | |- | ||
− | |03 | + | |03:01 |
|and press '''Enter.''' | |and press '''Enter.''' | ||
|- | |- | ||
− | |03 | + | |03:04 |
|You will get the output as displayed on the terminal. | |You will get the output as displayed on the terminal. | ||
− | |||
|- | |- | ||
− | | 03 | + | | 03:09 |
− | | Notice that | + | | Notice that the line written inside the first '''BEGIN''' block gets printed first and |
− | + | ||
|- | |- | ||
− | |03 | + | |03:16 |
− | | | + | | the first '''print''' statement in the script actually gets printed after the '''BEGIN''' block statements. |
− | + | ||
− | + | ||
|- | |- | ||
− | | 03 | + | | 03:25 |
− | | '''BEGIN | + | | '''BEGIN''' blocks get executed in the order of their declaration. |
|- | |- | ||
− | | | + | | 03:31 |
− | | From this example, it is evident that | + | | From this example, it is evident that |
− | + | ||
|- | |- | ||
− | | 03 | + | | 03:34 |
− | | | + | |the code written inside the '''BEGIN''' block gets executed first. |
− | + | ||
|- | |- | ||
− | | 03 | + | | 03:40 |
− | | This is irrespective of the location of the ''' BEGIN''' block inside | + | | This is irrespective of the location of the ''' BEGIN''' block inside PERL script. |
|- | |- | ||
− | | 03 | + | | 03:46 |
− | | '''BEGIN | + | | '''BEGIN''' blocks always get executed in the '''First In First Out''' manner. |
− | + | ||
|- | |- | ||
− | |03 | + | |03:52 |
− | |So one of the use of this '''block''' is to include files inside a Perl script, before actual execution starts. | + | |So, one of the use of this '''block''' is to include files inside a Perl script, before actual execution starts. |
− | + | ||
|- | |- | ||
− | |04 | + | |04:01 |
− | |Now, let us understand the '''END | + | |Now, let us understand the '''END''' block. |
|- | |- | ||
− | |04 | + | |04:04 |
− | |''' END | + | |'''END''' block gets executed at the end of PERL program. |
|- | |- | ||
− | |04 | + | |04:09 |
|Code written inside this block gets executed after PERL has finished executing the program. | |Code written inside this block gets executed after PERL has finished executing the program. | ||
|- | |- | ||
− | |04 | + | |04:17 |
− | |We can have several ''' END | + | |We can have several ''' END''' blocks inside a '''Perl script.''' |
− | + | ||
|- | |- | ||
− | |04 | + | |04:21 |
− | |These blocks will get executed in reverse order of declaration | + | |These blocks will get executed in reverse order of declaration. |
|- | |- | ||
− | | 04 | + | | 04:26 |
| That is, in '''Last define First execute''' pattern. | | That is, in '''Last define First execute''' pattern. | ||
− | |||
|- | |- | ||
− | |04 | + | |04:30 |
− | |The syntax for ''' END | + | |The syntax for '''END''' block is as follows: |
|- | |- | ||
− | |04 | + | |04:35 |
|''' END ''' in capital letters open curly bracket | |''' END ''' in capital letters open curly bracket | ||
|- | |- | ||
− | |04 | + | |04:39 |
− | | | + | |press ''' Enter'''. Piece of code to be executed at the end of the ''' PERL ''' script, |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | |04 | + | |04:45 |
− | | | + | |press '''Enter'''.Close curly bracket. |
|- | |- | ||
− | |04 | + | |04:49 |
− | | | + | |Now, let us look at an example of '''END''' blocks. |
− | + | ||
|- | |- | ||
− | |04 | + | |04:53 |
− | | | + | |Open the Terminal and type: |
− | |||
|- | |- | ||
− | |04 | + | | 04:56 |
− | | | + | |'''gedit endBlock dot pl space ampersand''' |
|- | |- | ||
− | | | + | | 05:00 |
− | |''' | + | |and press '''Enter'''. |
|- | |- | ||
− | | | + | |05:03 |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
| This will open the ''' endBlock dot pl''' file in '''gedit.''' | | This will open the ''' endBlock dot pl''' file in '''gedit.''' | ||
− | |||
|- | |- | ||
− | | 05 | + | | 05:08 |
| Type the following piece of code as displayed on the screen. | | Type the following piece of code as displayed on the screen. | ||
− | |||
|- | |- | ||
− | | 05 | + | | 05:13 |
| Let us look at what I have written inside this script. | | Let us look at what I have written inside this script. | ||
|- | |- | ||
− | | 05 | + | | 05:17 |
− | | Here we have printed some text before and after ''' END''' blocks. | + | | Here, we have printed some text before and after ''' END''' blocks. |
|- | |- | ||
− | | 05 | + | | 05:23 |
− | |Similarly, we have written one print statement in each ''' END''' block. | + | |Similarly, we have written one '''print''' statement in each ''' END''' block. |
|- | |- | ||
− | | 05 | + | | 05:29 |
− | | Please note, I have not given the | + | | Please note, I have not given the semicolon after the '''END''' block. |
− | + | ||
|- | |- | ||
− | |05 | + | |05:34 |
|If we give the semicolon, there will be a syntax error on compilation. | |If we give the semicolon, there will be a syntax error on compilation. | ||
− | |||
|- | |- | ||
− | | 05 | + | | 05:41 |
− | | Now, press ''' Ctrl+s''' to save the file. | + | | Now, press ''' Ctrl+s''' to '''save''' the file. |
|- | |- | ||
− | | 05 | + | | 05:45 |
− | | Then switch to terminal and execute the script by typing | + | | Then switch to terminal and execute the script by typing: |
|- | |- | ||
− | |05 | + | |05:50 |
|''' perl endBlock dot pl''' | |''' perl endBlock dot pl''' | ||
− | + | |- | |
− | | 05 | + | | 05:53 |
− | |and ''' | + | |and press '''Enter.''' |
− | + | ||
|- | |- | ||
− | |05 | + | |05:55 |
| You will get the output as displayed on the terminal. | | You will get the output as displayed on the terminal. | ||
− | |||
|- | |- | ||
− | |06 | + | |06:00 |
| Notice that : The line written inside the ''' END''' block is printed last. | | Notice that : The line written inside the ''' END''' block is printed last. | ||
|- | |- | ||
− | | 06 | + | | 06:06 |
− | | The last ''' print | + | | The last '''print''' statement in the script actually gets printed before the '''END''' block statements and |
|- | |- | ||
− | | 06 | + | | 06:13 |
− | | ''' END''' blocks | + | | '''END''' blocks get executed in the reverse order of their declaration. |
|- | |- | ||
− | | 06 | + | | 06:20 |
| From the example, it is evident that | | From the example, it is evident that | ||
|- | |- | ||
− | | 06 | + | | 06:23 |
− | | | + | |the code written inside the '''END''' blocks get executed at the end. |
− | + | ||
|- | |- | ||
− | |06 | + | |06:29 |
− | | This is irrespective of the location of the ''' END''' block inside the | + | | This is irrespective of the location of the '''END''' block inside the PERL script and |
|- | |- | ||
− | |06 | + | |06:36 |
− | |'''END''' blocks | + | |'''END''' blocks get executed in the '''Last In First Out''' manner. |
|- | |- | ||
− | |06 | + | |06:41 |
− | |So, one use of '''END''' block is to destroy objects created in the program | + | |So, one use of '''END''' block is to destroy objects created in the program before exiting. |
− | + | ||
|- | |- | ||
− | |06 | + | |06:49 |
− | | Similarly, | + | | Similarly, PERL has '''UNITCHECK, CHECK''' and '''INIT''' blocks. |
|- | |- | ||
− | |06 | + | |06:55 |
|These blocks are used rarely by developers and are a bit difficult to understand. | |These blocks are used rarely by developers and are a bit difficult to understand. | ||
|- | |- | ||
− | |07 | + | |07:02 |
| So, I will be just briefing you about these blocks. | | So, I will be just briefing you about these blocks. | ||
|- | |- | ||
− | |07 | + | |07:06 |
− | |''' UNITCHECK, CHECK''' and '''INIT''' blocks are useful | + | |''' UNITCHECK, CHECK''' and '''INIT''' blocks are useful: |
|- | |- | ||
− | |07 | + | |07:10 |
|to catch the transition between compilation and execution phase of the main program and | |to catch the transition between compilation and execution phase of the main program and | ||
|- | |- | ||
− | |07 | + | |07:18 |
− | |to perform some checks or | + | |to perform some checks or initialization, after compilation and before execution. |
|- | |- | ||
− | |07 | + | |07:24 |
− | |''' UNITCHECK''' and '''CHECK''' blocks | + | |''' UNITCHECK''' and '''CHECK''' blocks run in '''Last in First out''' manner |
|- | |- | ||
− | |07 | + | |07:31 |
− | |whereas '''INIT''' | + | |whereas '''INIT''' block runs in '''First In First Out''' manner. |
|- | |- | ||
− | |07 | + | |07:37 |
− | |The syntax for '''UNITCHECK''' block is as follows | + | |The syntax for '''UNITCHECK''' block is as follows: |
|- | |- | ||
− | |07 | + | |07:41 |
|''' UNITCHECK''' in capital letters space open curly bracket | |''' UNITCHECK''' in capital letters space open curly bracket | ||
|- | |- | ||
− | |07 | + | |07:46 |
− | | | + | |press '''Enter'''. |
|- | |- | ||
− | |07 | + | |07:48 |
− | |Piece of code to be executed | + | |Piece of code to be executed, |
|- | |- | ||
− | |07 | + | |07:50 |
− | | | + | |press '''Enter'''. |
|- | |- | ||
− | |07 | + | |07:52 |
− | |Close curly bracket | + | |Close curly bracket. |
|- | |- | ||
− | |07 | + | |07:54 |
− | |The syntax for '''CHECK''' block is as follows | + | |The syntax for '''CHECK''' block is as follows: |
|- | |- | ||
− | |07 | + | |07:58 |
|'''CHECK''' in capital letters space open curly bracket | |'''CHECK''' in capital letters space open curly bracket | ||
|- | |- | ||
− | |08 | + | |08:03 |
− | | | + | |press '''Enter'''. Piece of code to be executed, |
|- | |- | ||
− | |08 | + | |08:07 |
− | | | + | |press '''Enter'''.Close curly bracket. |
|- | |- | ||
− | |08 | + | |08:11 |
− | | | + | |The syntax for '''INIT''' block is as follows: |
− | + | ||
|- | |- | ||
− | |08 | + | |08:15 |
− | | | + | |'''INIT''' in capital letters space open curly bracket |
|- | |- | ||
− | |08 | + | |08:20 |
− | | | + | |press '''Enter'''.Piece of code to be initialized, |
|- | |- | ||
− | |08 | + | |08:24 |
− | | | + | |press '''Enter '''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | |08 | + | |08:26 |
− | |Close curly bracket | + | |Close curly bracket. |
|- | |- | ||
− | |08 | + | |08:28 |
|For better understanding, I recommend that you experiment with these blocks in your '''Perl''' scripts. | |For better understanding, I recommend that you experiment with these blocks in your '''Perl''' scripts. | ||
|- | |- | ||
− | |08 | + | |08:36 |
− | |Let us summarize. | + | |Let us summarize. In this tutorial, we have learnt: |
|- | |- | ||
− | |08 | + | |08:40 |
− | | | + | |'''BEGIN''' and '''END''' blocks in detail and |
− | + | ||
|- | |- | ||
− | |08 | + | |08:44 |
− | |''' | + | |introduction to '''UNITCHECK, CHECK''' and '''INIT''' blocks |
|- | |- | ||
− | |08 | + | |08:48 |
− | | | + | |using sample programs. |
|- | |- | ||
− | |08 | + | |08:52 |
− | | | + | |Here is an assignment for you: |
− | + | ||
|- | |- | ||
− | |08 | + | |08:54 |
− | | | + | |Type the below code inside a '''PERL script'''; |
|- | |- | ||
− | |08 | + | |08:58 |
− | | | + | |'''execute''' the script and observe the output. |
|- | |- | ||
− | | | + | |09:02 |
− | | | + | |Watch the video available at the following link. |
− | + | ||
|- | |- | ||
− | |09 | + | |09:06 |
− | | | + | |It summarizes the Spoken Tutorial project. |
− | + | ||
|- | |- | ||
− | |09 | + | |09:09 |
− | | | + | |If you do not have good bandwidth, you can download and watch it. |
− | + | ||
|- | |- | ||
− | |09 | + | | 09:14 |
− | | | + | | The Spoken Tutorial project team: Conducts workshops using spoken tutorials. |
− | + | ||
|- | |- | ||
− | | 09 | + | |09:20 |
− | | | + | |Gives certificates to those who pass an online test. |
− | + | ||
|- | |- | ||
− | |09 | + | |09:24 |
− | | | + | |For more details, please write to: contact at spoken hyphen tutorial dot org. |
|- | |- | ||
− | |09 | + | | 09:32 |
− | | | + | | "Spoken Tutorial" project is a part of the "Talk to a Teacher" project. |
|- | |- | ||
− | | 09 | + | |09:37 |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|It is supported by the National Mission on Education through ICT, MHRD, Government of India. | |It is supported by the National Mission on Education through ICT, MHRD, Government of India. | ||
− | |||
|- | |- | ||
− | |09 | + | |09:45 |
− | |More information on this | + | |More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
|- | |- | ||
− | | 09 | + | | 09:57 |
− | | | + | |Hope you enjoyed this '''Perl''' tutorial. |
|- | |- | ||
− | |10 | + | |10:00 |
− | |This is Amol signing off. | + | |This is Amol, signing off. |
− | + | ||
− | + | ||
|- | |- | ||
− | |10 | + | |10:02 |
|Thanks for joining. | |Thanks for joining. | ||
|} | |} |
Latest revision as of 15:54, 10 March 2017
Time | Narration |
00:01 | Welcome to the spoken tutorial on Blocks in Perl. |
00:06 | In this tutorial, we will learn about the various blocks available in Perl. |
00:13 | I am using Ubuntu Linux 12.04 operating system and Perl 5.14.2. |
00:21 | I will also be using the gedit Text Editor. |
00:26 | You can use any text editor of your choice. |
00:31 | As a pre-requisite, you should have basic knowledge of variables, comments in Perl. |
00:38 | Knowledge of data structures in PERL will be an added advantage. |
00:44 | Please go through the relevant spoken tutorials on the Spoken Tutorial website. |
00:50 | Perl provides 5 special blocks. |
00:53 | These blocks get executed at various stages of a Perl program. |
00:59 | These blocks are: |
01:01 | BEGIN , END |
01:03 | UNITCHECK, CHECK |
01:05 | INIT, Let us start with understanding the BEGIN block. |
01:10 | BEGIN block gets executed at the time of compilation. |
01:15 | So, any code written inside this block gets executed first during compilation. |
01:22 | We can have several BEGIN blocks inside a Perl script. |
01:26 | These blocks will get executed in the order of declaration. |
01:31 | That is, in the First define First execute pattern. |
01:35 | The syntax for BEGIN block is as follows: |
01:40 | BEGIN in capital letters space open curly bracket |
01:45 | Press Enter. |
01:47 | Piece of code to be executed at the time of compilation, |
01:51 | Press Enter .Close curly bracket. |
01:55 | Now, let us look at an example of BEGIN block. |
01:59 | Open the Terminal and type: |
02:02 | gedit beginBlock dot pl space ampersand |
02:08 | and press Enter. |
02:10 | This will open the beginBlock dot pl file in gedit. |
02:15 | Type the following piece of code as displayed on the screen. |
02:20 | Let us look at what I have written inside the script. |
02:24 | Here, we have printed some text before and after BEGIN blocks. |
02:31 | Similarly, I have written one print statement in each BEGIN block. |
02:37 | Please note, I have not given the semicolon after the BEGIN blocks. |
02:42 | Putting a semicolon will result in a syntax error on execution of the program. |
02:49 | Now, press Ctrl+s to save the file. |
02:53 | Then switch to terminal and execute the script by typing: |
02:58 | perl beginBlock dot pl |
03:01 | and press Enter. |
03:04 | You will get the output as displayed on the terminal. |
03:09 | Notice that the line written inside the first BEGIN block gets printed first and |
03:16 | the first print statement in the script actually gets printed after the BEGIN block statements. |
03:25 | BEGIN blocks get executed in the order of their declaration. |
03:31 | From this example, it is evident that |
03:34 | the code written inside the BEGIN block gets executed first. |
03:40 | This is irrespective of the location of the BEGIN block inside PERL script. |
03:46 | BEGIN blocks always get executed in the First In First Out manner. |
03:52 | So, one of the use of this block is to include files inside a Perl script, before actual execution starts. |
04:01 | Now, let us understand the END block. |
04:04 | END block gets executed at the end of PERL program. |
04:09 | Code written inside this block gets executed after PERL has finished executing the program. |
04:17 | We can have several END blocks inside a Perl script. |
04:21 | These blocks will get executed in reverse order of declaration. |
04:26 | That is, in Last define First execute pattern. |
04:30 | The syntax for END block is as follows: |
04:35 | END in capital letters open curly bracket |
04:39 | press Enter. Piece of code to be executed at the end of the PERL script, |
04:45 | press Enter.Close curly bracket. |
04:49 | Now, let us look at an example of END blocks. |
04:53 | Open the Terminal and type: |
04:56 | gedit endBlock dot pl space ampersand |
05:00 | and press Enter. |
05:03 | This will open the endBlock dot pl file in gedit. |
05:08 | Type the following piece of code as displayed on the screen. |
05:13 | Let us look at what I have written inside this script. |
05:17 | Here, we have printed some text before and after END blocks. |
05:23 | Similarly, we have written one print statement in each END block. |
05:29 | Please note, I have not given the semicolon after the END block. |
05:34 | If we give the semicolon, there will be a syntax error on compilation. |
05:41 | Now, press Ctrl+s to save the file. |
05:45 | Then switch to terminal and execute the script by typing: |
05:50 | perl endBlock dot pl |
05:53 | and press Enter. |
05:55 | You will get the output as displayed on the terminal. |
06:00 | Notice that : The line written inside the END block is printed last. |
06:06 | The last print statement in the script actually gets printed before the END block statements and |
06:13 | END blocks get executed in the reverse order of their declaration. |
06:20 | From the example, it is evident that |
06:23 | the code written inside the END blocks get executed at the end. |
06:29 | This is irrespective of the location of the END block inside the PERL script and |
06:36 | END blocks get executed in the Last In First Out manner. |
06:41 | So, one use of END block is to destroy objects created in the program before exiting. |
06:49 | Similarly, PERL has UNITCHECK, CHECK and INIT blocks. |
06:55 | These blocks are used rarely by developers and are a bit difficult to understand. |
07:02 | So, I will be just briefing you about these blocks. |
07:06 | UNITCHECK, CHECK and INIT blocks are useful: |
07:10 | to catch the transition between compilation and execution phase of the main program and |
07:18 | to perform some checks or initialization, after compilation and before execution. |
07:24 | UNITCHECK and CHECK blocks run in Last in First out manner |
07:31 | whereas INIT block runs in First In First Out manner. |
07:37 | The syntax for UNITCHECK block is as follows: |
07:41 | UNITCHECK in capital letters space open curly bracket |
07:46 | press Enter. |
07:48 | Piece of code to be executed, |
07:50 | press Enter. |
07:52 | Close curly bracket. |
07:54 | The syntax for CHECK block is as follows: |
07:58 | CHECK in capital letters space open curly bracket |
08:03 | press Enter. Piece of code to be executed, |
08:07 | press Enter.Close curly bracket. |
08:11 | The syntax for INIT block is as follows: |
08:15 | INIT in capital letters space open curly bracket |
08:20 | press Enter.Piece of code to be initialized, |
08:24 | press Enter . |
08:26 | Close curly bracket. |
08:28 | For better understanding, I recommend that you experiment with these blocks in your Perl scripts. |
08:36 | Let us summarize. In this tutorial, we have learnt: |
08:40 | BEGIN and END blocks in detail and |
08:44 | introduction to UNITCHECK, CHECK and INIT blocks |
08:48 | using sample programs. |
08:52 | Here is an assignment for you: |
08:54 | Type the below code inside a PERL script; |
08:58 | execute the script and observe the output. |
09:02 | Watch the video available at the following link. |
09:06 | It summarizes the Spoken Tutorial project. |
09:09 | If you do not have good bandwidth, you can download and watch it. |
09:14 | The Spoken Tutorial project team: Conducts workshops using spoken tutorials. |
09:20 | Gives certificates to those who pass an online test. |
09:24 | For more details, please write to: contact at spoken hyphen tutorial dot org. |
09:32 | "Spoken Tutorial" project is a part of the "Talk to a Teacher" project. |
09:37 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
09:45 | More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
09:57 | Hope you enjoyed this Perl tutorial. |
10:00 | This is Amol, signing off. |
10:02 | Thanks for joining. |