1 00:00:01 --> 00:00:05 Welcome to the spoken tutorial on Variables in Perl. 2 00:00:06 --> 00:00:11 In this tutorial, we will learn about Variables in Perl 3 00:00:12 --> 00:00:17 I am using Ubuntu Linux12.04 operating system and 4 00:00:18 --> 00:00:25 Perl 5.14.2 that is, Perl revision 5 version 14 and subversion 2 5 00:00:26 --> 00:00:29 I will also be using the gedit Text Editor. 6 00:00:30 --> 00:00:33 You can use any text editor of your choice. 7 00:00:34 --> 00:00:36 Variables in Perl: 8 00:00:37 --> 00:00:43 Variables are used for storing values, like text strings, numbers or arrays. 9 00:00:44 --> 00:00:49 Once a variable is declared, it can be used over and over again in the script. 10 00:00:50 --> 00:00:55 Scalar represents a single value and can store scalars only. 11 00:00:56 --> 00:00:59 Scalar variables are declared using $ (dollar) symbol. 12 00:01:00 --> 00:01:02 Let us look at Variable Declaration: 13 00:01:03 --> 00:01:08 A variable can be declared as follows: dollar priority semicolon 14 00:01:09 --> 00:01:17 Variable names in Perl can have several formats. Variables must begin with a letter or underscore 15 00:01:18 --> 00:01:23 And may contains letters, digits, underscores or a combination of above 3. 16 00:01:24 --> 00:01:29 Variables declared with CAPITAL letters have special meaning in Perl. 17 00:01:30 --> 00:01:33 So avoid declaring variables using Capital letters. 18 00:01:34 --> 00:01:43 Now open the Terminal and type gedit variables dot pl ampersand 19 00:01:44 --> 00:01:49 The ampersand will unlock the command prompt on the terminal. Now press Enter. 20 00:01:50 --> 00:01:55 This will open the variables.pl file in gedit text editor. 21 00:01:56 --> 00:02:00 dot pl is the default extension of a Perl file. 22 00:02:01 --> 00:02:09 Type the following in the file; dollar priority semicolon and press Enter. 23 00:02:10 --> 00:02:12 So we have declared the variable priority. 24 00:02:13 --> 00:02:17 You do not need to declare a variable before using it; 25 00:02:18 --> 00:02:20 you can just use it into your code. 26 00:02:21 --> 00:02:24 Now let us assign a numerical value to the variable priority. 27 00:02:25 --> 00:02:31 For this type dollar priority space equal to space one semicolon 28 00:02:32 --> 00:02:33 and press Enter. 29 00:02:34 --> 00:02:35 Next, type 30 00:02:36 --> 00:02:49 print space double quote Value of variable is: dollar priority slash n close double quote semicolon and press enter 31 00:02:50 --> 00:02:52 slash n is the new line character. 32 00:02:53 --> 00:03:01 Now save this file as variables.pl at any location. 33 00:03:02 --> 00:03:09 In my case, it will get saved in home/amol directory. Now save this file 34 00:03:10 --> 00:03:17 Now, let us change the permissions of variables.pl file, which we just now created. 35 00:03:18 --> 00:03:26 To do so, on the Terminal type, chmod 755 variables dot pl 36 00:03:27 --> 00:03:31 This will provide read, write & execute rights to the file. 37 00:03:32 --> 00:03:35 To compile this Perl script, on the Terminal 38 00:03:36 --> 00:03:41 type;perl hyphen c variables dot pl 39 00:03:42 --> 00:03:48 Hyphen c switch compiles the Perl script for any compilation/syntax error. 40 00:03:49 --> 00:03:50 Now press Enter 41 00:03:51 --> 00:03:55 This tells us that there is no syntax error in our script. 42 00:03:56 --> 00:04:05 Now let's execute the Perl script by typing perl variables dot pl and press Enter. 43 00:04:06 --> 00:04:09 The output displayed is as highlighted. 44 00:04:10 --> 00:04:14 We can also assign a string value to the variable we declared. 45 00:04:15 --> 00:04:17 Switch back to the Text editor window. 46 00:04:18 --> 00:04:21 Instead of dollar priority equal to one; type 47 00:04:22 --> 00:04:27 dollar priority equal to in single quote high 48 00:04:28 --> 00:04:33 Please note that the assignments are evaluated from right to left. 49 00:04:34 --> 00:04:37 A scalar can hold data of any type, be it a string, a number 50 00:04:38 --> 00:04:44 Save this file and compile the script once again by typing 51 00:04:45 --> 00:04:50 perl hyphen c variables dot pl now press Enter. 52 00:04:51 --> 00:04:54 This tells us that there is no syntax error. 53 00:04:55 --> 00:05:02 Execute the script by typing perl variables dot pl and press Enter. 54 00:05:03 --> 00:05:06 The output is as shown. 55 00:05:07 --> 00:05:09 Now switch back to the Text Editor window. 56 00:05:10 --> 00:05:14 You can also use scalars within a double-quoted strings as 57 00:05:15 --> 00:05:18 dollar priority in double quote String 58 00:05:19 --> 00:05:21 Save this file and close it. 59 00:05:22 --> 00:05:26 let us learn how to declare multiple variables. 60 00:05:27 --> 00:05:30 To do so open the new file in Text Editor. 61 00:05:31 --> 00:05:41 onTerminal type - gedit multivar dot pl space ampersand and press Enter. 62 00:05:42 --> 00:05:47 This will open multivar dot pl file in text editor 63 00:05:48 --> 00:05:49 Now type - 64 00:05:50 --> 00:05:59 dollar firstVar comma dollar secondVar semicolon and press Enter. 65 00:06:00 --> 00:06:06 To copy the value of variable dollar firstVar to dollar secondVar, type - 66 00:06:07 --> 00:06:18 dollar firstVar space equal to space dollar secondVar semicolon and press Enter. 67 00:06:19 --> 00:06:29 All mathematical operations like addition, subtraction, multiplication, division, can be done on these variables 68 00:06:30 --> 00:06:33 Let us see how we can achieve this using Perl. 69 00:06:34 --> 00:06:35 Switch to text editor. 70 00:06:36 --> 00:06:40 And now let us assign the value 10 to both of these variables by typing, 71 00:06:41 --> 00:06:50 dollar firstVar equal to dollar secondVar equal to ten semicolon And Press Enter. 72 00:06:51 --> 00:06:54 Now to print these values, type 73 00:06:55 --> 00:07:16 print double quote firstVar: dollar firstVar and secondVar: dollar secondVar slash n close double quote semicolon press Enter. 74 00:07:17 --> 00:07:18 Now save this file. 75 00:07:19 --> 00:07:22 Now let us add the values in the two variables. 76 00:07:23 --> 00:07:24 For this type 77 00:07:25 --> 00:07:42 dollar addition space equal to space dollar firstVar plus space dollar secondVar semicolonand Press Enter. 78 00:07:43 --> 00:07:46 Notice, we haven't declared the variable addition. 79 00:07:47 --> 00:07:52 Once again, to print the value of the variable addition, type 80 00:07:53 --> 00:08:04 print double quote Addition is dollar addition slash n close double quote semicolon 81 00:08:05 --> 00:08:06 Save this file 82 00:08:07 --> 00:08:11 To compile this file again on terminal type 83 00:08:12 --> 00:08:17 perl hyphen c multivar dot pl 84 00:08:18 --> 00:08:23 There is no syntax error so we can execute the script... 85 00:08:24 --> 00:08:29 by typing perl multivar dot pl 86 00:08:30 --> 00:08:33 This will produce an output as highlighted. 87 00:08:34 --> 00:08:37 Similarly, try subtraction, multiplication and division. 88 00:08:38 --> 00:08:40 I have written the code here. 89 00:08:41 --> 00:08:45 Now let us save this file and close it. 90 00:08:46 --> 00:08:47 Now compile the file by typing 91 00:08:48 --> 00:08:53 perl hyphen c multivar dot pl 92 00:08:54 --> 00:08:54 There is no syntax error. 93 00:08:55 --> 00:09:00 So we can execute the script as perl multivar dot pl 94 00:09:01 --> 00:09:05 On execution the output will look like this. 95 00:09:06 --> 00:09:10 This brings us to the end of this tutorial. 96 00:09:11 --> 00:09:13 In this tutorial we have learnt, 97 00:09:14 --> 00:09:17 To Declare and Use scalar variables in Perl 98 00:09:18 --> 00:09:19 Assignment 99 00:09:20 --> 00:09:21 Declare a number variable. 100 00:09:22 --> 00:09:23 Assign 10 to it. 101 00:09:24 --> 00:09:25 Print the variable declared. 102 00:09:26 --> 00:09:28 Declare 2 string variables. 103 00:09:29 --> 00:09:33 Assign these values to them - “Namaste ” and “India”. 104 00:09:34 --> 00:09:37 Print those 2 variables one after the other. 105 00:09:38 --> 00:09:41 Watch the video available at the following link 106 00:09:42 --> 00:09:44 It summarises the Spoken Tutorial project 107 00:09:45 --> 00:09:49 If you do not have good bandwidth, you can download and watch it 108 00:09:50 --> 00:09:52 The Spoken Tutorial Project Team 109 00:09:53 --> 00:09:55 Conducts workshops using spoken tutorials 110 00:09:56 --> 00:10:00 Gives certificates to those who pass an online test 111 00:10:01 --> 00:10:07 For more details, please write to contact at spoken tutorial dot org 112 00:10:08 --> 00:10:12 Spoken Tutorial Project is a part of the Talk to a Teacher project 113 00:10:13 --> 00:10:22 It is supported by the National Mission on Education through ICT, MHRD, Government of India 114 00:10:23 --> 00:10:28 More information on this Mission is available at the following link. 115 00:10:29 --> 00:10:33 Hope you enjoyed this Perl tutorial. This is Amol Brahmankar signing off. 116 00:10:34 --> 00:10:39 Thanks for joining.