Welcome to the spoken tutorial on Variables in Perl. In this tutorial, we will learn about Variables in Perl I am using Ubuntu Linux12.04 operating system and Perl 5.14.2 that is, Perl revision 5 version 14 and subversion 2 I will also be using the gedit Text Editor. You can use any text editor of your choice. Variables in Perl: Variables are used for storing values, like text strings, numbers or arrays. Once a variable is declared, it can be used over and over again in the script. Scalar represents a single value and can store scalars only. Scalar variables are declared using $ (dollar) symbol. Let us look at Variable Declaration: A variable can be declared as follows: dollar priority semicolon Variable names in Perl can have several formats. Variables must begin with a letter or underscore And may contains letters, digits, underscores or a combination of above 3. Variables declared with CAPITAL letters have special meaning in Perl. So avoid declaring variables using Capital letters. Now open the Terminal and type gedit variables dot pl ampersand The ampersand will unlock the command prompt on the terminal. Now press Enter. This will open the variables.pl file in gedit text editor. dot pl is the default extension of a Perl file. Type the following in the file; dollar priority semicolon and press Enter. So we have declared the variable priority. You do not need to declare a variable before using it; you can just use it into your code. Now let us assign a numerical value to the variable priority. For this type dollar priority space equal to space one semicolon and press Enter. Next, type print space double quote Value of variable is: dollar priority slash n close double quote semicolon and press enter slash n is the new line character. Now save this file as variables.pl at any location. In my case, it will get saved in home/amol directory. Now save this file Now, let us change the permissions of variables.pl file, which we just now created. To do so, on the Terminal type, chmod 755 variables dot pl This will provide read, write & execute rights to the file. To compile this Perl script, on the Terminal type;perl hyphen c variables dot pl Hyphen c switch compiles the Perl script for any compilation/syntax error. Now press Enter This tells us that there is no syntax error in our script. Now let’s execute the Perl script by typing perl variables dot pl and press Enter. The output displayed is as highlighted. We can also assign a string value to the variable we declared. Switch back to the Text editor window. Instead of dollar priority equal to one; type dollar priority equal to in single quote high Please note that the assignments are evaluated from right to left. A scalar can hold data of any type, be it a string, a number Save this file and compile the script once again by typing perl hyphen c variables dot pl now press Enter. This tells us that there is no syntax error. Execute the script by typing perl variables dot pl and press Enter. The output is as shown. Now switch back to the Text Editor window. You can also use scalars within a double-quoted strings as dollar priority in double quote String Save this file and close it. let us learn how to declare multiple variables. To do so open the new file in Text Editor. onTerminal type – gedit multivar dot pl space ampersand and press Enter. This will open multivar dot pl file in text editor Now type – dollar firstVar comma dollar secondVar semicolon and press Enter. To copy the value of variable dollar firstVar to dollar secondVar, type – dollar firstVar space equal to space dollar secondVar semicolon and press Enter. All mathematical operations like addition, subtraction, multiplication, division, can be done on these variables Let us see how we can achieve this using Perl. Switch to text editor. And now let us assign the value 10 to both of these variables by typing, dollar firstVar equal to dollar secondVar equal to ten semicolon And Press Enter. Now to print these values, type print double quote firstVar: dollar firstVar and secondVar: dollar secondVar slash n close double quote semicolon press Enter. Now save this file. Now let us add the values in the two variables. For this type dollar addition space equal to space dollar firstVar plus space dollar secondVar semicolonand Press Enter. Notice, we haven’t declared the variable addition. Once again, to print the value of the variable addition, type print double quote Addition is dollar addition slash n close double quote semicolon Save this file To compile this file again on terminal type perl hyphen c multivar dot pl There is no syntax error so we can execute the script… by typing perl multivar dot pl This will produce an output as highlighted. Similarly, try subtraction, multiplication and division. I have written the code here. Now let us save this file and close it. Now compile the file by typing perl hyphen c multivar dot pl There is no syntax error. So we can execute the script as perl multivar dot pl On execution the output will look like this. This brings us to the end of this tutorial. In this tutorial we have learnt, To Declare and Use scalar variables in Perl Assignment Declare a number variable. Assign 10 to it. Print the variable declared. Declare 2 string variables. Assign these values to them – “Namaste ” and “India”. Print those 2 variables one after the other. Watch the video available at the following link It summarises the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it The Spoken Tutorial Project Team Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to contact at spoken tutorial dot org Spoken Tutorial Project is a part of the Talk to a Teacher project It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at the following link. Hope you enjoyed this Perl tutorial. This is Amol Brahmankar signing off. Thanks for joining.