1 00:00:01 --> 00:00:05 Welcome to the spoken tutorial on BLOCKS in Perl. 2 00:00:06 --> 00:00:12 In this tutorial, we will learn about the various BLOCKS available in Perl 3 00:00:13 --> 00:00:20 I am using Ubuntu Linux 12.04 operating system and Perl 5.14.2 4 00:00:21 --> 00:00:25 I will also be using the gedit Text Editor. 5 00:00:26 --> 00:00:30 You can use any text editor of your choice. 6 00:00:31 --> 00:00:37 As a pre-requisite, you should have basic knowledge of Variables, Comments in Perl 7 00:00:38 --> 00:00:43 Knowledge of Data structures in PERL will be an added advantage. 8 00:00:44 --> 00:00:49 Please go through the relevant spoken tutorials on the spoken tutorial website. 9 00:00:50 --> 00:00:52 Perl provides 5 special blocks. 10 00:00:53 --> 00:00:58 These blocks get executed at various stages of a Perl program. 11 00:00:59 --> 00:01:00 These blocks are: 12 00:01:01 --> 00:01:01 BEGIN 13 00:01:02 --> 00:01:02 END 14 00:01:03 --> 00:01:03 UNITCHECK 15 00:01:04 --> 00:01:04 CHECK. 16 00:01:05 --> 00:01:05 INIT 17 00:01:06 --> 00:01:09 Let us start with understanding the BEGIN block. 18 00:01:10 --> 00:01:14 BEGIN block get executed at the time of compilation. 19 00:01:15 --> 00:01:21 So, any code written inside this block gets executed first during compilation. 20 00:01:22 --> 00:01:25 We can have several BEGIN blocks inside a Perl script. 21 00:01:26 --> 00:01:30 These blocks will get executed in the order of declaration. 22 00:01:31 --> 00:01:34 That is in the First define First execute pattern 23 00:01:35 --> 00:01:39 The syntax for BEGIN block is as follows 24 00:01:40 --> 00:01:44 BEGIN in capital letters space open curly bracket 25 00:01:45 --> 00:01:46 Press Enter. 26 00:01:47 --> 00:01:50 Piece of code to be executed at the time of compilation 27 00:01:51 --> 00:01:51 Press Enter 28 00:01:52 --> 00:01:54 Close curly bracket 29 00:01:55 --> 00:01:58 Now, let us look at an example of BEGIN blocks. 30 00:01:59 --> 00:02:01 Open the Terminal and type 31 00:02:02 --> 00:02:07 gedit beginBlock dot pl space ampersand 32 00:02:08 --> 00:02:09 and press Enter. 33 00:02:10 --> 00:02:14 This will open the beginBlock dot pl file in gedit. 34 00:02:15 --> 00:02:19 Type the following piece of code as displayed on the screen. 35 00:02:20 --> 00:02:23 Let us look at what I have written inside the script. 36 00:02:24 --> 00:02:30 Here, we have printed some text before and after BEGIN blocks. 37 00:02:31 --> 00:02:36 Similarly, I have written one print statement in each BEGIN block. 38 00:02:37 --> 00:02:41 Please note, I have not given the semicolon after the BEGIN blocks. 39 00:02:42 --> 00:02:48 Putting a semicolon, will result in a syntax error, on execution of the program. 40 00:02:49 --> 00:02:52 Now, press Ctrl+s to save the file. 41 00:02:53 --> 00:02:57 Then switch to terminal and execute the script by typing, 42 00:02:58 --> 00:03:00 perl beginBlock dot pl 43 00:03:01 --> 00:03:03 and press Enter. 44 00:03:04 --> 00:03:08 You will get the output as displayed on the terminal. 45 00:03:09 --> 00:03:15 Notice that The line written inside the first BEGIN block gets printed first and 46 00:03:16 --> 00:03:24 The first print statement in the script actually gets printed after the BEGIN block statements. 47 00:03:25 --> 00:03:30 BEGIN blocks gets executed in the order of their declaration. 48 00:03:31 --> 00:03:33 From this example, it is evident that: 49 00:03:34 --> 00:03:39 The code written inside the BEGIN blocks gets executed first. 50 00:03:40 --> 00:03:45 This is irrespective of the location of the BEGIN block inside PERL script. 51 00:03:46 --> 00:03:51 BEGIN blocks always get executed in the First In First Out manner. 52 00:03:52 --> 00:04:00 So one of the use of this block is to include files inside a Perl script, before actual execution starts. 53 00:04:01 --> 00:04:03 Now, let us understand the END block 54 00:04:04 --> 00:04:08 END block get executed at the end of PERL program 55 00:04:09 --> 00:04:16 Code written inside this block gets executed after PERL has finished executing the program. 56 00:04:17 --> 00:04:20 We can have several END blocks inside a Perl script. 57 00:04:21 --> 00:04:25 These blocks will get executed in reverse order of declaration 58 00:04:26 --> 00:04:29 That is, in Last define First execute pattern. 59 00:04:30 --> 00:04:34 The syntax for END block is as follows 60 00:04:35 --> 00:04:38 END in capital letters open curly bracket 61 00:04:39 --> 00:04:39 Press Enter 62 00:04:40 --> 00:04:44 Piece of code to be executed at the end of the PERL script 63 00:04:45 --> 00:04:45 Press Enter 64 00:04:46 --> 00:04:48 Close curly bracket 65 00:04:49 --> 00:04:52 Now let us look at an example of END blocks. 66 00:04:53 --> 00:04:55 Open the Terminal and type 67 00:04:56 --> 00:04:59 gedit endBlock dot pl space ampersand 68 00:05:00 --> 00:05:02 and press Enter 69 00:05:03 --> 00:05:07 This will open the endBlock dot pl file in gedit. 70 00:05:08 --> 00:05:12 Type the following piece of code as displayed on the screen. 71 00:05:13 --> 00:05:16 Let us look at what I have written inside this script. 72 00:05:17 --> 00:05:22 Here we have printed some text before and after END blocks. 73 00:05:23 --> 00:05:28 Similarly, we have written one print statement in each END block. 74 00:05:29 --> 00:05:33 Please note, I have not given the semicolon after the END block. 75 00:05:34 --> 00:05:40 If we give the semicolon, there will be a syntax error on compilation. 76 00:05:41 --> 00:05:44 Now, press Ctrl+s to save the file. 77 00:05:45 --> 00:05:49 Then switch to terminal and execute the script by typing, 78 00:05:50 --> 00:05:52 perl endBlock dot pl 79 00:05:53 --> 00:05:54 and press Enter. 80 00:05:55 --> 00:05:59 You will get the output as displayed on the terminal. 81 00:06:00 --> 00:06:05 Notice that: The line written inside the END block is printed last. 82 00:06:06 --> 00:06:12 The last print statement in the script actually gets printed before the END block statements and 83 00:06:13 --> 00:06:19 END blocks gets executed in the reverse order of their declaration. 84 00:06:20 --> 00:06:22 From the example, it is evident that 85 00:06:23 --> 00:06:28 The code written inside the END blocks get executed at the end. 86 00:06:29 --> 00:06:35 This is irrespective of the location of the END block inside the PERL script and 87 00:06:36 --> 00:06:40 END blocks gets executed in the Last In First Out manner. 88 00:06:41 --> 00:06:48 So, one use of END block is to destroy objects created in the program, before exiting. 89 00:06:49 --> 00:06:54 Similarly, PERL has UNITCHECK, CHECK and INIT blocks. 90 00:06:55 --> 00:07:01 These blocks are used rarely by developers and are a bit difficult to understand. 91 00:07:02 --> 00:07:05 So, I will be just briefing you about these blocks. 92 00:07:06 --> 00:07:09 UNITCHECK, CHECK and INIT blocks are useful- 93 00:07:10 --> 00:07:17 to catch the transition between compilation and execution phase of the main program and 94 00:07:18 --> 00:07:23 to perform some checks or initialisation, after compilation and before execution 95 00:07:24 --> 00:07:30 UNITCHECK and CHECK blocks runs in Last in First out manner 96 00:07:31 --> 00:07:36 whereas INIT block runs in First In First Out manner. 97 00:07:37 --> 00:07:40 The syntax for UNITCHECK block is as follows 98 00:07:41 --> 00:07:45 UNITCHECK in capital letters space open curly bracket 99 00:07:46 --> 00:07:47 Press Enter 100 00:07:48 --> 00:07:49 Piece of code to be executed 101 00:07:50 --> 00:07:51 Press Enter 102 00:07:52 --> 00:07:53 Close curly bracket 103 00:07:54 --> 00:07:57 The syntax for CHECK block is as follows 104 00:07:58 --> 00:08:02 CHECK in capital letters space open curly bracket 105 00:08:03 --> 00:08:03 Press Enter 106 00:08:04 --> 00:08:06 Piece of code to be executed 107 00:08:07 --> 00:08:07 Press Enter 108 00:08:08 --> 00:08:10 Close curly bracket 109 00:08:11 --> 00:08:14 The syntax for INIT block is as follows 110 00:08:15 --> 00:08:19 INIT in capital letters space open curly bracket 111 00:08:20 --> 00:08:20 Press Enter 112 00:08:21 --> 00:08:23 Piece of code to be initialised 113 00:08:24 --> 00:08:25 Press Enter 114 00:08:26 --> 00:08:27 Close curly bracket 115 00:08:28 --> 00:08:35 For better understanding, I recommend that you experiment with these blocks in your Perl scripts. 116 00:08:36 --> 00:08:36 Let us summarize. 117 00:08:37 --> 00:08:39 In this tutorial, we have learnt - 118 00:08:40 --> 00:08:43 BEGIN and END blocks in detail and 119 00:08:44 --> 00:08:47 Introduction to UNITCHECK, CHECK and INIT blocks 120 00:08:48 --> 00:08:51 using sample programs 121 00:08:52 --> 00:08:53 Here is assignment for you - 122 00:08:54 --> 00:08:57 Type the below code inside a PERL script; 123 00:08:58 --> 00:09:01 Execute the script and observe the output. 124 00:09:02 --> 00:09:05 Watch the video available at the following link 125 00:09:06 --> 00:09:08 It summaries the Spoken Tutorial project 126 00:09:09 --> 00:09:13 If you do not have good bandwidth, you can download and watch it 127 00:09:14 --> 00:09:19 The Spoken Tutorial Project Team Conducts workshops using spoken tutorials 128 00:09:20 --> 00:09:23 Gives certificates to those who pass an online test 129 00:09:24 --> 00:09:31 For more details, please write to contact at spoken hyphen tutorial dot org 130 00:09:32 --> 00:09:36 Spoken Tutorial Project is a part of the Talk to a Teacher project 131 00:09:37 --> 00:09:44 It is supported by the National Mission on Education through ICT, MHRD, Government of India. 132 00:09:45 --> 00:09:56 More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro 133 00:09:57 --> 00:09:59 Hope you enjoyed this Perl tutorial. 134 00:10:00 --> 00:10:01 This is Amol signing off. 135 00:10:02 --> 00:10:07 Thanks for joining.