PERL/C2/Variables-in-Perl/English-timed
From Script | Spoken-Tutorial
Revision as of 12:22, 7 May 2015 by Sandhya.np14 (Talk | contribs)
Time | Narration |
00:01 | Welcome to the spoken tutorial on Variables in Perl. |
00:06 | In this tutorial, we will learn about variables in Perl |
00:12 | I am using Ubuntu Linux12.04 operating system and |
00:18 | Perl 5.14.2 that is: Perl revision 5, version 14, and subversion 2. |
00:26 | I will also be using the gedit Text Editor. |
00:30 | You can use any text editor of your choice. |
00:34 | Variables in Perl: |
00:37 | Variables are used for storing values like text strings, numbers or arrays. |
00:44 | Once a variable is declared, it can be used over and over again in the script. |
00:50 | Scalar represents a single value and can store scalars only. |
00:56 | Scalar variables are declared using $ (dollar) symbol. |
01:00 | Let us look at Variable Declaration: |
01:03 | A variable can be declared as follows: dollar priority semicolon |
01:09 | Variable names in Perl can have several formats. Variables must begin with a letter or underscore (_). |
01:18 | And may contain letters, digits, underscores or a combination of above three. |
01:24 | Variables declared with CAPITAL letters have special meaning in Perl. |
01:30 | So, avoid declaring variables using capital letters. |
01:34 | Now open the Terminal and type: gedit variables dot pl ampersand(&) |
01:44 | The ampersand will unlock the command prompt on the terminal. Now press Enter. |
01:50 | This will open the variables.pl file in gedit text editor. |
01:56 | dot pl (.pl) is the default extension of a Perl file. |
02:01 | Type the following in the file: dollar priority semicolon and press Enter. |
02:10 | So we have declared the variable priority. |
02:13 | You do not need to declare a variable before using it; |
02:18 | you can just use it into your code. |
02:21 | Now let us assign a numerical value to the variable priority. |
02:25 | For this, type: dollar priority space equal to space one semicolon |
02:32 | and press Enter. |
02:34 | Next, type: |
02:36 | print space double quote Value of variable is: dollar priority slash n close double quote semicolon and press Enter. |
02:50 | slash n is the "new line" character. |
02:53 | Now save this file as variables.pl at any location. |
03:02 | In my case, it will get saved in home/amol directory. Now save this file. |
03:10 | Now, let us change the permissions of variables.pl file which we just now created. |
03:18 | To do so, on the Terminal type, chmod 755 variables dot pl |
03:27 | This will provide "read", "write" & "execute" rights to the file. |
03:32 | To compile this Perl script, on the Terminal, |
03:36 | type: perl hyphen c variables dot pl |
03:42 | Hyphen c switch compiles the Perl script for any compilation/syntax error. |
03:49 | Now press Enter. |
03:51 | This tells us that there is no syntax error in our script. |
03:56 | Now let us execute the Perl script by typing perl variables dot pl and press Enter. |
04:06 | The output displayed is as highlighted. |
04:10 | We can also assign a string value to the variable we declared. |
04:15 | Switch back to the Text editor window. |
04:18 | Instead of dollar priority equal to one; type: |
04:22 | dollar priority equal to in single quotes high; |
04:28 | Please note that the assignments are evaluated from right to left. |
04:34 | A scalar can hold data of any type, be it a string, a number. |
04:38 | Save this file and compile the script once again by typing: |
04:45 | perl hyphen c variables dot pl now press Enter. |
04:51 | This tells us that there is no syntax error. |
04:55 | Execute the script by typing perl variables dot pl and press Enter. |
05:03 | The output is as shown. |
05:07 | Now switch back to the Text Editor window. |
05:10 | You can also use scalars within a double-quoted string as |
05:15 | dollar priority in double quotes string; |
05:19 | Save this file and close it. |
05:22 | Let us learn how to declare multiple variables. |
05:27 | To do so, open the new file in Text Editor. |
05:31 | OnTerminal, type: gedit multivar dot pl space ampersand and press Enter. |
05:42 | This will open "multivar dot pl" file in text editor. |
05:48 | Now, type: |
05:50 | dollar firstVar comma dollar secondVar semicolon and press Enter. |
06:00 | To copy the value of variable dollar firstVar to dollar secondVar, type: |
06:07 | dollar firstVar space equal to space dollar secondVar semicolon and press Enter. |
06:19 | All mathematical operations like addition, subtraction, multiplication, division, can be done on these variables. |
06:30 | Let us see how we can achieve this using Perl. |
06:34 | Switch to text editor. |
06:36 | And now let us assign the value 10 to both of these variables by typing, |
06:41 | dollar firstVar equal to dollar secondVar equal to ten semicolon And Press Enter. |
06:51 | Now to print these values, type: |
06:55 | print double quote firstVar: dollar firstVar and secondVar: dollar secondVar slash n close double quotes semicolon press Enter. |
07:17 | Now save this file. |
07:19 | Now let us add the values in the two variables. |
07:23 | For this, type: |
07:25 | dollar addition space equal to space dollar firstVar space plus space dollar secondVar semicolon and Press Enter. |
07:43 | Notice, we haven't declared the variable addition. |
07:47 | Once again, to print the value of the variable addition, type: |
07:53 | print double quote Addition is dollar addition slash n close double quote semicolon. |
08:05 | Save this file |
08:07 | To compile this file, again on terminal type: |
08:12 | perl hyphen c multivar dot pl |
08:18 | There is no syntax error, so we can execute the script |
08:24 | by typing perl multivar dot pl |
08:30 | This will produce an output as highlighted. |
08:34 | Similarly, try subtraction, multiplication and division. |
08:38 | I have written the code here. |
08:41 | Now let us save this file and close it. |
08:46 | Now compile the file by typing |
08:48 | perl hyphen c multivar dot pl |
08:54 | There is no syntax error. |
08:55 | So, we can execute the script as perl multivar dot pl |
09:01 | On execution the output will look like this. |
09:06 | This brings us to the end of this tutorial. |
09:11 | In this tutorial, we have learnt |
09:14 | to declare and use scalar variables in Perl. |
09:18 | Assignment: |
09:20 | Declare a number variable. |
09:22 | Assign 10 to it. |
09:24 | Print the variable declared. |
09:26 | Declare 2 string variables. |
09:29 | Assign these values to them - “Namaste ” and “India”. |
09:34 | Print those two variables one after the other. |
09:38 | Watch the video available at the following link. |
09:42 | It summarizes the Spoken Tutorial project. |
09:45 | If you do not have good bandwidth, you can download and watch it. |
09:50 | The Spoken Tutorial Project Team: |
09:53 | Conducts workshops using spoken tutorials. |
09:56 | Gives certificates to those who pass an online test. |
10:01 | For more details, please write to contact at spoken tutorial dot org. |
10:08 | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
10:13 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
10:23 | More information on this mission is available at the following link. |
10:29 | Hope you enjoyed this Perl tutorial. This is Amol Brahmankar, signing off. |
10:34 | Thanks for joining. |