Difference between revisions of "PERL/C2/Variables-in-Perl/English"
(Created page with ''''Title of script''': Variables in Perl '''Author: Amol''' '''Keywords: Variables in Perl, Video Tutorial, Scalars in Perl''' {| style="border-spacing:0;" | style="border:0…') |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 7: | Line 7: | ||
− | {| | + | {| border=1 |
− | + | ! <center>'''Visual Cue'''</center> | |
− | + | ! <center>'''Narration'''</center> | |
|- | |- | ||
− | | | + | | Slide |
− | | | + | | Welcome to the spoken tutorial on Variables in '''Perl'''. |
|- | |- | ||
− | | | + | | Slide:Learning Objectives |
− | | | + | | In this tutorial, we will learn about |
Variables in '''Perl''' | Variables in '''Perl''' | ||
− | |||
− | |||
|- | |- | ||
− | | | + | | Slide:System Requirements |
− | | | + | | I am using '''Ubuntu Linux12.04''' operating system and |
'''Perl''' '''5.14.2 ''' | '''Perl''' '''5.14.2 ''' | ||
Line 37: | Line 35: | ||
|- | |- | ||
− | | | + | | Slide |
− | | | + | | '''Variables''' in '''Perl''': |
− | Variables are used for storing values, like text strings, numbers or arrays. | + | '''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. | + | Once a '''variable''' is declared, it can be used over and over again in the script. |
|- | |- | ||
− | | | + | | Slide |
− | | | + | | '''Scalar''' represents a single value and can store '''scalars''' only. |
− | Scalar variables are declared using $ (dollar) symbol. | + | '''Scalar variables''' are declared using $ ('''dollar''') symbol. |
|- | |- | ||
− | | | + | | Slide |
− | | | + | | Let us look at '''Variable Declaration''': |
− | A variable can be declared as follows: | + | A '''variable''' can be declared as follows: |
'''dollar priority semicolon''' | '''dollar priority semicolon''' | ||
− | |||
− | |||
− | Variable names in '''Perl''' can have several formats. Variables must begin with a letter or underscore | + | '''Variable''' names in '''Perl''' can have several formats. '''Variables''' must begin with a letter or '''underscore''' |
|- | |- | ||
− | | | + | | Slide |
− | | | + | | And may contains letters, digits, '''underscores''' or a combination of above 3. |
|- | |- | ||
− | | | + | | Slide |
− | | | + | | '''Variables''' declared with CAPITAL letters have special meaning in '''Perl.''' |
− | So avoid declaring variables using Capital letters. | + | So avoid declaring '''variables''' using Capital letters. |
|- | |- | ||
− | | | + | | Switch to the Terminal and type |
'''gedit variables.pl &''' | '''gedit variables.pl &''' | ||
and press '''Enter'''. | and press '''Enter'''. | ||
− | | | + | | Now open the '''Terminal''' and type |
− | '''gedit variables | + | '''gedit variables dot pl ampersand ''' |
− | ''' | + | The '''ampersand''' will unlock the command prompt on the '''terminal'''. |
Now press '''Enter'''. | Now press '''Enter'''. | ||
|- | |- | ||
− | | | + | | Point to the filename '''variables.pl''' in the Titlebar of '''gedit'''. |
− | | | + | | This will open the '''variables.pl '''file in '''gedit''' text editor. |
'''dot pl '''is the default extension of a '''Perl''' file. | '''dot pl '''is the default extension of a '''Perl''' file. | ||
|- | |- | ||
− | | | + | | Type the following |
'''$priority;''' | '''$priority;''' | ||
and press '''Enter'''. | and press '''Enter'''. | ||
− | | | + | | Type the following in the file; |
'''dollar priority semicolon''' | '''dollar priority semicolon''' | ||
− | |||
− | |||
and press '''Enter'''. | and press '''Enter'''. | ||
|- | |- | ||
− | | | + | | Highlight '''$priority; '''with the cursor |
− | | | + | | So we have declared the '''variable priority'''. |
− | You do not need to declare a variable before using it; | + | You do not need to declare a '''variable''' before using it; |
you can just use it into your code. | you can just use it into your code. | ||
|- | |- | ||
− | | | + | | Type |
'''$priority = 1;''' | '''$priority = 1;''' | ||
+ | | Now let us assign a numerical value to the '''variable priority'''. | ||
− | + | For this type | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
'''dollar priority space equal to space one semicolon''' | '''dollar priority space equal to space one semicolon''' | ||
− | |||
− | |||
and press '''Enter'''. | and press '''Enter'''. | ||
|- | |- | ||
− | | | + | | Type |
'''print “Value of variable is: $priority”;''' | '''print “Value of variable is: $priority”;''' | ||
− | | | + | | Next, type |
− | '''print space double quote Value of variable is: dollar priority slash n double quote complete semicolon''' | + | '''print space double quote Value of variable is: dollar priority slash n double quote complete semicolon''' and press enter |
− | '''slash n is the new line character | + | '''slash n''' is the new line character. |
|- | |- | ||
− | | | + | | Save this file as '''variables.pl '''at any location of your choice. |
− | I am storing it in my home directory''' /home/amol.''' | + | I am storing it in my '''home''' directory''' /home/amol.''' |
− | | | + | | 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 | In my case, it will get saved in '''/home/amol '''directory. Now save this file | ||
|- | |- | ||
− | | | + | | Switch to the Terminal and type |
'''chmod 755 variables.pl ''' | '''chmod 755 variables.pl ''' | ||
− | + | and then press '''Enter'''. | |
− | | | + | | Now, let us change the permissions of '''variables.pl''' '''Perl '''file, which we just created. |
To do so, on the '''Terminal''' type, | To do so, on the '''Terminal''' type, | ||
− | '''chmod 755 variables | + | '''chmod 755 variables dot pl''' |
This will provide read, write & execute rights to the file. | This will provide read, write & execute rights to the file. | ||
|- | |- | ||
− | | | + | | Switch to the Terminal and type |
'''perl -c variables.pl''' | '''perl -c variables.pl''' | ||
but don't press '''Enter''' yet. | but don't press '''Enter''' yet. | ||
− | | | + | | To compile this '''Perl''' script, on the '''Terminal''' |
− | + | ||
− | + | ||
+ | type: | ||
'''perl hyphen c variables dot pl''' | '''perl hyphen c variables dot pl''' | ||
|- | |- | ||
− | | | + | | Highlight '''-c''' using the cursor. |
− | | | + | | '''Hyphen c '''switch compiles the '''Perl''' script for any compilation/syntax error. |
|- | |- | ||
− | | | + | | Press '''Enter''' |
− | + | | Now press '''Enter''' | |
− | This tells us that there is no syntax error in our script | + | This tells us that there is no syntax error in our script. |
|- | |- | ||
− | | | + | | Highlight '''variables.pl syntax OK''' |
− | + | ||
Type | Type | ||
Line 197: | Line 183: | ||
and press '''Enter'''. | and press '''Enter'''. | ||
− | | | + | | Now let's execute the '''Perl''' script by typing |
'''perl variables dot pl''' | '''perl variables dot pl''' | ||
Line 204: | Line 190: | ||
|- | |- | ||
− | | | + | | Point with the cursor to |
'''Value of variable is: 1''' | '''Value of variable is: 1''' | ||
− | | | + | | The output displayed is as highlighted. |
|- | |- | ||
− | | | + | | Switch back to the Text editor window. |
− | | | + | | We can also assign a '''string''' value to the '''variable''' we declared. |
Switch back to the Text editor window. | Switch back to the Text editor window. | ||
|- | |- | ||
− | | | + | | Highlight '''1'''with the cursor |
− | Instead of '''1''' type | + | Instead of '''1''', type ''''high'''' |
− | + | | Instead of '''dollar priority equal to one;''' | |
− | ''''high'''' | + | |
− | | | + | |
type | type | ||
Line 228: | Line 212: | ||
|- | |- | ||
− | | | + | | Point from right of '''<nowiki>= </nowiki>'''to left. |
− | | | + | | Please note that assignments are evaluated from right to left. |
− | A '''scalar''' can hold data of any type, be it a string, a number | + | A '''scalar''' can hold data of any type, be it a '''string''', a number |
|- | |- | ||
− | | | + | | Switch to the '''Terminal''' and type |
'''perl -c variables.pl''' | '''perl -c variables.pl''' | ||
and press '''Enter'''. | and press '''Enter'''. | ||
− | | | + | | Save this file and compile the script once again by typing |
Line 247: | Line 231: | ||
|- | |- | ||
− | | | + | | Type |
'''perl variables.pl''' | '''perl variables.pl''' | ||
and press '''Enter'''. | and press '''Enter'''. | ||
− | | | + | | This tells us that there is no syntax error. |
Line 262: | Line 246: | ||
|- | |- | ||
− | | | + | | Point with the cursor to |
'''Value of variable is: high''' | '''Value of variable is: high''' | ||
− | | | + | | The output is as shown. |
|- | |- | ||
− | | | + | | Switch back to the Text editor window. |
Go to the last line and press '''Enter.''' | Go to the last line and press '''Enter.''' | ||
Line 275: | Line 259: | ||
'''$priority = “String”;''' | '''$priority = “String”;''' | ||
− | | | + | | Now switch back to the Text Editor window. |
− | You can also use scalars within a double-quoted strings as | + | You can also use '''scalars''' within a double-quoted '''strings''' as |
'''dollar priority in double quote String''' | '''dollar priority in double quote String''' | ||
− | Save this file and close it | + | Save this file and close it. |
|- | |- | ||
− | | | + | | |
− | | | + | | Let us learn how to declare multiple '''variables'''. |
To do so open the new file in Text Editor. | To do so open the new file in Text Editor. | ||
|- | |- | ||
− | | | + | | Switch to the '''Terminal''' and type |
'''gedit multivar.pl &''' | '''gedit multivar.pl &''' | ||
and press '''Enter'''. | and press '''Enter'''. | ||
− | | | + | | on'''Terminal''' type - |
− | '''gedit multivar dot pl space | + | |
+ | '''gedit multivar dot pl space ampersand''' | ||
and press '''Enter'''. | and press '''Enter'''. | ||
− | This will open multivar dot pl file in text editor | + | This will open '''multivar dot pl''' file in text editor |
|- | |- | ||
− | | | + | | Type |
'''$firstVar, $secondVar;''' | '''$firstVar, $secondVar;''' | ||
and press '''Enter'''. | and press '''Enter'''. | ||
− | | | + | | Now will type - |
'''dollar firstVar comma dollar secondVar semicolon''' | '''dollar firstVar comma dollar secondVar semicolon''' | ||
Line 319: | Line 304: | ||
|- | |- | ||
− | | | + | | Type |
'''$firstVar = $secondVar;''' | '''$firstVar = $secondVar;''' | ||
and press '''Enter'''. | and press '''Enter'''. | ||
− | | | + | | To copy the value of variable '''dollar firstVar''' to '''dollar secondVar''', type - |
'''dollar firstVar space equal to space dollar secondVar semicolon''' | '''dollar firstVar space equal to space dollar secondVar semicolon''' | ||
Line 331: | Line 316: | ||
|- | |- | ||
− | | | + | | Switch to slide |
− | + | | All mathematical operations like addition, subtraction, multiplication, division, can be done on these variables | |
− | + | ||
− | + | ||
Let us see how we can achieve this using '''Perl'''. | Let us see how we can achieve this using '''Perl'''. | ||
|- | |- | ||
− | | | + | | '''$firstVar = $secondVar =10;''' |
Press '''Enter'''. | Press '''Enter'''. | ||
− | | | + | | Switch to text editor. |
+ | |||
+ | And now let us assign the value '''10''' to both these variables by typing, | ||
+ | |||
+ | '''dollar firstVar equal to dollar secondVar equal to ten semicolon''' | ||
And Press '''Enter'''. | And Press '''Enter'''. | ||
|- | |- | ||
− | | | + | | Type |
'''print “firstVar: $firstVar and secondVar: $secondVar”;''' | '''print “firstVar: $firstVar and secondVar: $secondVar”;''' | ||
and press '''Enter'''. | and press '''Enter'''. | ||
− | | | + | | Now to print the values, type |
'''print double quote firstVar: dollar firstVar and secondVar: dollar secondVar slash n double quote complete semicolon''' press '''Enter'''. | '''print double quote firstVar: dollar firstVar and secondVar: dollar secondVar slash n double quote complete semicolon''' press '''Enter'''. | ||
Line 360: | Line 347: | ||
|- | |- | ||
− | | | + | | Type |
'''$addition = $firstVar + $secondVar;''' | '''$addition = $firstVar + $secondVar;''' | ||
Press '''Enter'''. | Press '''Enter'''. | ||
− | | | + | | Now let us add the values in the two variables. |
For this type | For this type | ||
Line 374: | Line 361: | ||
|- | |- | ||
− | | | + | | Highlight the variable '''addition '''with the cursor |
− | | | + | | Notice, we haven't declared the variable '''addition'''. |
|- | |- | ||
− | | | + | | Type |
'''print “Addition: $addition”;''' | '''print “Addition: $addition”;''' | ||
− | | | + | | Once again, to print the value of the variable '''addition''', type |
'''print double quote Addition is dollar addition slash n double quote complete semicolon''' | '''print double quote Addition is dollar addition slash n double quote complete semicolon''' | ||
Line 388: | Line 375: | ||
|- | |- | ||
− | | | + | | Switch to the Terminal and type |
'''perl -c variables.pl''' | '''perl -c variables.pl''' | ||
and press '''Enter'''. | and press '''Enter'''. | ||
− | | | + | | To compile this file again on terminal type |
'''perl hyphen c multivar dot pl''' | '''perl hyphen c multivar dot pl''' | ||
− | + | There is no syntax error so we can execute the script... | |
− | + | ||
− | we can execute the script | + | |
|- | |- | ||
− | | | + | | Type |
'''perl multivar.pl''' | '''perl multivar.pl''' | ||
and press '''Enter'''. | and press '''Enter'''. | ||
− | | | + | | ...by typing '''perl multivar dot pl''' |
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | | Point with the cursor to the output |
− | | | + | | This will produce an output as highlighted. |
|- | |- | ||
− | | | + | | Pause the video recording here |
Line 423: | Line 406: | ||
− | Resume the video recording now | + | Resume the video recording now. |
Also, compile the code so that the output is displayed on the '''Terminal'''. | Also, compile the code so that the output is displayed on the '''Terminal'''. | ||
− | | | + | | |
|- | |- | ||
− | | | + | | Switch to '''gedit''' |
− | | | + | | Similarly, try subtraction, multiplication and division. |
Line 437: | Line 420: | ||
|- | |- | ||
− | | | + | | Close this file in '''gedit''' |
− | | | + | | Now let us save this file and close it. |
|- | |- | ||
− | | | + | | Show the output in the '''terminal''' |
− | | | + | | Now compile the file by typing |
− | perl hyphen c multivar dot pl | + | '''perl hyphen c multivar dot pl''' |
− | There is no syntax error | + | There is no syntax error. |
− | + | So we can execute the script as | |
+ | '''perl multivar dot pl''' | ||
− | On execution the output will look like this | + | |
+ | On execution the output will look like this. | ||
|- | |- | ||
− | | | + | | Summary |
− | | | + | | This brings us to the end of this tutorial. |
In this tutorial we have learnt, | In this tutorial we have learnt, | ||
− | To Declare and Use scalar variables in '''Perl''' | + | To Declare and Use '''scalar variables''' in '''Perl''' |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | | Assignment |
− | | | + | | '''Assignment''' |
− | # Declare a number variable. | + | # Declare a number '''variable'''. |
− | # Assign | + | # Assign 10 to it. |
− | # Print the variable declared. | + | # Print the '''variable''' declared. |
− | # Declare 2 string variables. | + | # Declare 2 '''string variables'''. |
# Assign these values to them - “Namaste ” and “India”. | # Assign these values to them - “Namaste ” and “India”. | ||
− | # Print those | + | # Print those ''2 variables''' one after the other. |
|- | |- | ||
− | | | + | | About the Project |
− | | | + | | Watch the video available at the following link |
It summarises the Spoken Tutorial project | It summarises the Spoken Tutorial project | ||
Line 488: | Line 470: | ||
|- | |- | ||
− | | | + | | Spoken Tutorial Workshops |
− | | | + | | The Spoken Tutorial Project Team |
Conducts workshops using spoken tutorials | Conducts workshops using spoken tutorials | ||
Line 502: | Line 484: | ||
|- | |- | ||
− | | | + | | Acknowledgment |
− | | | + | | Spoken Tutorial Project is a part of the Talk to a |
Teacher project | Teacher project | ||
Line 518: | Line 500: | ||
|- | |- | ||
− | | | + | | |
− | | | + | | Hope you enjoyed this '''Perl''' tutorial. This is Amol Brahmankar signing off. |
Thanks for joining. | Thanks for joining. | ||
|} | |} |
Latest revision as of 10:48, 15 April 2013
Title of script: Variables in Perl
Author: Amol
Keywords: Variables in Perl, Video Tutorial, Scalars in Perl
|
|
---|---|
Slide | Welcome to the spoken tutorial on Variables in Perl. |
Slide:Learning Objectives | In this tutorial, we will learn about
Variables in Perl |
Slide:System Requirements | I am using Ubuntu Linux12.04 operating system and
Perl 5.14.2 that is, Perl revision 5 version 14 and subversion 2
You can use any text editor of your choice. |
Slide | 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. |
Slide | Scalar represents a single value and can store scalars only.
Scalar variables are declared using $ (dollar) symbol. |
Slide | Let us look at Variable Declaration:
A variable can be declared as follows: dollar priority semicolon
|
Slide | And may contains letters, digits, underscores or a combination of above 3. |
Slide | Variables declared with CAPITAL letters have special meaning in Perl.
So avoid declaring variables using Capital letters. |
Switch to the Terminal and type
gedit variables.pl & and press Enter. |
Now open the Terminal and type
gedit variables dot pl ampersand
Now press Enter. |
Point to the filename variables.pl in the Titlebar of gedit. | This will open the variables.pl file in gedit text editor.
dot pl is the default extension of a Perl file. |
Type the following
$priority; and press Enter. |
Type the following in the file;
dollar priority semicolon and press Enter. |
Highlight $priority; with the cursor | 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. |
Type
$priority = 1; |
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. |
Type
print “Value of variable is: $priority”; |
Next, type
print space double quote Value of variable is: dollar priority slash n double quote complete semicolon and press enter slash n is the new line character. |
Save this file as variables.pl at any location of your choice.
I am storing it in my home directory /home/amol. |
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 |
Switch to the Terminal and type
chmod 755 variables.pl and then press Enter. |
Now, let us change the permissions of variables.pl Perl file, which we just created.
To do so, on the Terminal type, chmod 755 variables dot pl This will provide read, write & execute rights to the file. |
Switch to the Terminal and type
perl -c variables.pl but don't press Enter yet. |
To compile this Perl script, on the Terminal
type: perl hyphen c variables dot pl |
Highlight -c using the cursor. | Hyphen c switch compiles the Perl script for any compilation/syntax error. |
Press Enter | Now press Enter
This tells us that there is no syntax error in our script. |
Highlight variables.pl syntax OK
Type perl variables.pl and press Enter. |
Now let's execute the Perl script by typing
perl variables dot pl and press Enter. |
Point with the cursor to
Value of variable is: 1 |
The output displayed is as highlighted. |
Switch back to the Text editor window. | We can also assign a string value to the variable we declared.
Switch back to the Text editor window. |
Highlight 1with the cursor
Instead of 1, type 'high' |
Instead of dollar priority equal to one;
type dollar priority equal to in single quote high |
Point from right of = to left. | Please note that assignments are evaluated from right to left.
A scalar can hold data of any type, be it a string, a number |
Switch to the Terminal and type
perl -c variables.pl and press Enter. |
Save this file and compile the script once again by typing
now press Enter. |
Type
perl variables.pl and press Enter. |
This tells us that there is no syntax error.
perl variables dot pl and press Enter. |
Point with the cursor to
Value of variable is: high |
The output is as shown. |
Switch back to the Text editor window.
Go to the last line and press Enter. Type $priority = “String”; |
Now switch back to the Text Editor window.
dollar priority in double quote String
|
Let us learn how to declare multiple variables.
To do so open the new file in Text Editor. | |
Switch to the Terminal and type
gedit multivar.pl & and press Enter. |
onTerminal type -
and press Enter.
|
Type
$firstVar, $secondVar; and press Enter. |
Now will type -
dollar firstVar comma dollar secondVar semicolon and press Enter. |
Type
$firstVar = $secondVar; 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. |
Switch to slide | All mathematical operations like addition, subtraction, multiplication, division, can be done on these variables
Let us see how we can achieve this using Perl. |
$firstVar = $secondVar =10;
Press Enter. |
Switch to text editor.
And now let us assign the value 10 to both these variables by typing, dollar firstVar equal to dollar secondVar equal to ten semicolon And Press Enter. |
Type
print “firstVar: $firstVar and secondVar: $secondVar”; and press Enter. |
Now to print the values, type
print double quote firstVar: dollar firstVar and secondVar: dollar secondVar slash n double quote complete semicolon press Enter. Now save this file. |
Type
$addition = $firstVar + $secondVar; Press Enter. |
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 semicolon and Press Enter. |
Highlight the variable addition with the cursor | Notice, we haven't declared the variable addition. |
Type
print “Addition: $addition”; |
Once again, to print the value of the variable addition, type
print double quote Addition is dollar addition slash n double quote complete semicolon Save this file |
Switch to the Terminal and type
perl -c variables.pl and press Enter. |
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... |
Type
perl multivar.pl and press Enter. |
...by typing perl multivar dot pl
|
Point with the cursor to the output | This will produce an output as highlighted. |
Pause the video recording here
|
|
Switch to gedit | Similarly, try subtraction, multiplication and division.
|
Close this file in gedit | Now let us save this file and close it. |
Show the output in the terminal | 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
|
Summary | This brings us to the end of this tutorial.
In this tutorial we have learnt, To Declare and Use scalar variables in Perl |
Assignment | Assignment
|
About the Project | 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 |
Spoken Tutorial Workshops | 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 |
Acknowledgment | 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. |