1 00:00:01 --> 00:00:06 Welcome to the spoken tutorial on if and if-else conditional statements in Perl. 2 00:00:07 --> 00:00:08 In this tutorial, we will learn about; 3 00:00:09 --> 00:00:10 if statement and 4 00:00:11 --> 00:00:11 if-else statement in Perl 5 00:00:12 --> 00:00:19 I am using Ubuntu Linux12.04 operating system and Perl 5.14.2 6 00:00:20 --> 00:00:23 I will also be using the gedit Text Editor. 7 00:00:24 --> 00:00:27 You can use any text editor of your choice. 8 00:00:28 --> 00:00:32 You should have basic knowledge of Variables and Comments in Perl. 9 00:00:33 --> 00:00:39 Knowledge of for, foreach , while and do-while loops in Perl will be an added advantage. 10 00:00:40 --> 00:00:44 Please go through the relevant spoken tutorials on the spoken tutorial website. 11 00:00:45 --> 00:00:48 Perl provides the following conditional statements - 12 00:00:49 --> 00:00:49 if 13 00:00:50 --> 00:00:50 if-else 14 00:00:51 --> 00:00:52 if-elsif-else and 15 00:00:53 --> 00:00:53 switch 16 00:00:54 --> 00:00:58 In this tutorial, we'll cover if and If-else statements 17 00:00:59 --> 00:01:00 if statement in Perl can be used 18 00:01:01 --> 00:01:06 to execute a piece of code only when a specified condition is satisfied. 19 00:01:07 --> 00:01:10 The syntax of if conditional statement is as follows 20 00:01:11 --> 00:01:18 if space open bracket condition close bracket space Open curly bracket 21 00:01:19 --> 00:01:19 Enter 22 00:01:20 --> 00:01:24 Piece of code semicolon, to be executed when the condition is true 23 00:01:25 --> 00:01:28 Enter, Close curly bracket 24 00:01:29 --> 00:01:35 The code inside the if statement will be executed only when the condition is true. 25 00:01:36 --> 00:01:39 Now let us look at an example of if statement. 26 00:01:40 --> 00:01:42 Open the Terminal and type 27 00:01:43 --> 00:01:48 gedit conditionalBlocks dot pl space &(ampersand ) 28 00:01:49 --> 00:01:51 and press Enter 29 00:01:52 --> 00:01:56 This will open the conditionalBlocks.pl file in gedit. 30 00:01:57 --> 00:02:01 Type the following piece of code as displayed on the screen. 31 00:02:02 --> 00:02:08 Here we have specified a condition for if which checks the value of variable count. 32 00:02:09 --> 00:02:14 Note the equal to equal to sign here. This is the comparison operator. 33 00:02:15 --> 00:02:22 The condition $count equal to equal to 5 is checked against the value of variable count. 34 00:02:23 --> 00:02:27 When it is equal to 5, the code within the if block will get executed. 35 00:02:28 --> 00:02:31 Now, press ctrl+s to save the file. 36 00:02:32 --> 00:02:35 Then switch to the terminal. 37 00:02:36 --> 00:02:40 Make sure that you are in the directory in which you have saved your file. 38 00:02:41 --> 00:02:45 Type the following to check for any compilation or syntax error - 39 00:02:46 --> 00:02:52 perl hyphen c conditionalBlocks dot pl 40 00:02:53 --> 00:02:54 and press Enter. 41 00:02:55 --> 00:02:58 The following line will be displayed on the terminal window 42 00:02:59 --> 00:03:03 conditionalBlocks.pl syntax OK 43 00:03:04 --> 00:03:09 As there is no compilation or syntax error, we will execute the Perl script by typing - 44 00:03:10 --> 00:03:13 perl conditionalBlocks dot pl 45 00:03:14 --> 00:03:15 and press Enter 46 00:03:16 --> 00:03:18 The following output will be shown on terminal. 47 00:03:19 --> 00:03:22 I am inside if statement 48 00:03:23 --> 00:03:25 Switch back to gedit 49 00:03:26 --> 00:03:30 Alternately, we can write the above if statement as- 50 00:03:31 --> 00:03:56 print space double quotes I am inside if statement slash n close double quotes space if open bracket dollar count space equal to equal to space 5 close bracket semicolon. 51 00:03:57 --> 00:04:00 Now, let us look at if-else statement. 52 00:04:01 --> 00:04:05 This statement is used when user wants to execute 53 00:04:06 --> 00:04:08 one piece of code when the condition is true and 54 00:04:09 --> 00:04:12 another piece of code when the condition is false 55 00:04:13 --> 00:04:16 The syntax for if-else condition is as follows - 56 00:04:17 --> 00:04:26 if space open bracket condition close bracket space open curly bracket Press Enter. 57 00:04:27 --> 00:04:28 piece of code semicolon 58 00:04:29 --> 00:04:31 to be executed when if condition is true, 59 00:04:32 --> 00:04:33 Press Enter 60 00:04:34 --> 00:04:40 close curly bracket space else space open curly bracket Enter 61 00:04:41 --> 00:04:42 another piece of code semicolon 62 00:04:43 --> 00:04:46 to be executed when if condition is false 63 00:04:47 --> 00:04:50 Press Enter close curly bracket 64 00:04:51 --> 00:04:57 Now again, go to the conditionalBlocks.pl file which we have already created in gedit. 65 00:04:58 --> 00:05:06 Assign 4 to count variable then at the end of the if block type space 66 00:05:07 --> 00:05:08 else 67 00:05:09 --> 00:05:13 space open curly bracket press Enter 68 00:05:14 --> 00:05:29 print space double quotes I am inside else statement slash n close double quotes semicolon 69 00:05:30 --> 00:05:33 Press Enter and close curly bracket. 70 00:05:34 --> 00:05:37 Here, 4 is assigned to variable $count. 71 00:05:38 --> 00:05:42 As the value of count variable does not match 5, 72 00:05:43 --> 00:05:46 the code within the if block will not get execute 73 00:05:47 --> 00:05:51 instead the code within the else block will get execute. 74 00:05:52 --> 00:05:55 Now press Ctrl+S to save the file. 75 00:05:56 --> 00:05:58 Now switch to terminal. 76 00:05:59 --> 00:06:10 and type perl hyphen c conditionalBlocks dot pl to check for any compilation or syntax error 77 00:06:11 --> 00:06:12 now, press Enter 78 00:06:13 --> 00:06:16 The following line will be displayed on the terminal 79 00:06:17 --> 00:06:19 conditionalBlocks.pl syntax OK 80 00:06:20 --> 00:06:26 As there is no compilation or syntax error, we will now execute the Perl script. 81 00:06:27 --> 00:06:32 Type perl conditionalBlocks dot pl 82 00:06:33 --> 00:06:34 and press Enter 83 00:06:35 --> 00:06:38 The following output will be shown on terminal. 84 00:06:39 --> 00:06:43 I am inside else statement 85 00:06:44 --> 00:06:45 Let us summarize. 86 00:06:46 --> 00:06:48 In this tutorial, we have learnt - 87 00:06:49 --> 00:06:49 if and 88 00:06:50 --> 00:06:52 if-else conditional statements in Perl 89 00:06:53 --> 00:06:54 using sample programs. 90 00:06:55 --> 00:06:56 Here is assignment for you - 91 00:06:57 --> 00:07:00 Print “It is an open source language” 92 00:07:01 --> 00:07:03 when the variable declared has value 'Perl' 93 00:07:04 --> 00:07:07 otherwise print “It's a proprietary language” 94 00:07:08 --> 00:07:10 Watch the video available at the following link 95 00:07:11 --> 00:07:14 It summaries the Spoken Tutorial project 96 00:07:15 --> 00:07:19 If you do not have good bandwidth, you can download and watch it 97 00:07:20 --> 00:07:21 The Spoken Tutorial Project Team 98 00:07:22 --> 00:07:25 Conducts workshops using spoken tutorials 99 00:07:26 --> 00:07:30 Gives certificates to those who pass an online test 100 00:07:31 --> 00:07:36 For more details, please write to contact at spoken hyphen tutorial dot org 101 00:07:37 --> 00:07:41 Spoken Tutorial Project is a part of the Talk to a Teacher project 102 00:07:42 --> 00:07:49 It is supported by the National Mission on Education through ICT, MHRD, Government of India. 103 00:07:50 --> 00:07:59 More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro 104 00:08:00 --> 00:08:03 Hope you enjoyed this Perl tutorial. 105 00:08:04 --> 00:08:05 This is Amol Brahmankar signing off. 106 00:08:06 --> 00:08:11 Thanks for joining.