Difference between revisions of "PERL/C2/Variables-in-Perl/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 7: Line 7:
  
  
{| style="border-spacing:0;"
+
{| border=1
| style="border:0.5pt solid #000000;padding:0.106cm;"| <center>'''Visual Cue'''</center>
+
<center>'''Visual Cue'''</center>
| style="border:0.5pt solid #000000;padding:0.106cm;"| <center>'''Narration'''</center>
+
<center>'''Narration'''</center>
  
 
|-
 
|-
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide
+
| Slide
| style="border:0.5pt solid #000000;padding:0.106cm;"| Welcome to the spoken tutorial on Variables in '''Perl'''.  
+
| Welcome to the spoken tutorial on Variables in '''Perl'''.  
  
 
|-
 
|-
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide:Learning Objectives
+
| Slide:Learning Objectives
| style="border:0.5pt solid #000000;padding:0.106cm;"| In this tutorial, we will learn about
+
| In this tutorial, we will learn about
  
 
Variables in '''Perl'''
 
Variables in '''Perl'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide:System Requirements
+
| Slide:System Requirements
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| I am using '''Ubuntu Linux12.04''' operating system and  
+
| I am using '''Ubuntu Linux12.04''' operating system and  
  
 
'''Perl''' '''5.14.2 '''
 
'''Perl''' '''5.14.2 '''
Line 35: Line 35:
  
 
|-
 
|-
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide
+
| Slide
| style="border:0.5pt solid #000000;padding:0.106cm;"| Variables in '''Perl''':
+
| '''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.
  
 
|-
 
|-
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide
+
| Slide
| style="border:0.5pt solid #000000;padding:0.106cm;"| Scalar represents a single value and can store scalars only.
+
| '''Scalar''' represents a single value and can store '''scalars''' only.
  
Scalar variables are declared using $ (dollar) symbol.
+
'''Scalar variables''' are declared using $ ('''dollar''') symbol.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide
+
| Slide
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Let us look at Variable Declaration:
+
| 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'''
 
'''$priority;'''
 
  
  
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'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide
+
| Slide
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| And may contains letters, digits, underscores or a combination of above 3.
+
| And may contains letters, digits, '''underscores''' or a combination of above 3.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide
+
| Slide
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Variables declared with CAPITAL letters have special meaning in '''Perl.'''  
+
| '''Variables''' declared with CAPITAL letters have special meaning in '''Perl.'''  
  
So avoid declaring variables using Capital letters.
+
So avoid declaring '''variables''' using Capital letters.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type
+
| Switch to the Terminal and type
  
 
'''gedit variables.pl &'''
 
'''gedit variables.pl &'''
  
 
and press '''Enter'''.
 
and press '''Enter'''.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Open the '''Terminal''' and type  
+
| Now open the '''Terminal''' and type  
  
'''gedit variables.pl & '''
+
'''gedit variables dot pl ampersand '''
  
  
'''The ampersand will unlock the command prompt on the terminal)'''
+
The '''ampersand''' will unlock the command prompt on the '''terminal'''.
  
 
Now press '''Enter'''.
 
Now press '''Enter'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point to the filename '''variables.pl''' in the Titlebar of '''gedit'''.
+
| Point to the filename '''variables.pl''' in the Titlebar of '''gedit'''.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| This will open the '''variables.pl '''file in gedit text editor.
+
| 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.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type the following
+
| Type the following
  
 
'''$priority;'''
 
'''$priority;'''
  
 
and press '''Enter'''.
 
and press '''Enter'''.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type the following in the file;
+
| Type the following in the file;
  
 
'''dollar priority semicolon'''
 
'''dollar priority semicolon'''
 
'''$priority;'''
 
  
 
and press '''Enter'''.
 
and press '''Enter'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight '''$priority; '''with the cursor
+
| Highlight '''$priority; '''with the cursor
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| So we have declared the variable '''priority'''.
+
| 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.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type
+
| Type
  
 
'''$priority = 1;'''
 
'''$priority = 1;'''
 +
|  Now let us assign a numerical value to the '''variable priority'''.
  
 
+
For this type
 
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| 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'''
 
'''$priority = 1;'''
 
  
 
and press '''Enter'''.
 
and press '''Enter'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type
+
| Type
  
 
'''print “Value of variable is: $priority”;'''
 
'''print “Value of variable is: $priority”;'''
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Next, type
+
| 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'''
  
'''slash n is the new line character'''
+
'''slash n''' is the new line character.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Save this file as '''variables.pl '''at any location of your choice.
+
| 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.'''
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Save this file as '''variables.pl''' at any location.
+
| 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
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type  
+
| Switch to the Terminal and type  
  
 
'''chmod 755 variables.pl '''
 
'''chmod 755 variables.pl '''
  
explain the need of above line and then press '''Enter'''.
+
and then press '''Enter'''.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now, let us change the permissions of '''variables.pl''' '''Perl '''file which we just created.
+
| 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.pl'''
+
'''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.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type
+
| 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.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| To compile this '''Perl''' script, on the '''Terminal'''.
+
| To compile this '''Perl''' script, on the '''Terminal'''
  
Type the following command;
+
type the following command;
  
 
'''perl hyphen c variables dot pl'''
 
'''perl hyphen c variables dot pl'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight '''-c''' using the cursor.
+
| Highlight '''-c''' using the cursor.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| '''Hyphen c '''switch compiles the '''Perl''' script for any compilation/syntax error.
+
| '''Hyphen c '''switch compiles the '''Perl''' script for any compilation/syntax error.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now press '''Enter'''  
+
| Press '''Enter'''  
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now 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.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight '''variables.pl syntax OK'''
+
| Highlight '''variables.pl syntax OK'''
 
+
  
 
Type
 
Type
Line 195: Line 184:
  
 
and press '''Enter'''.
 
and press '''Enter'''.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Let's execute the '''Perl''' script by typing
+
| Now let's execute the '''Perl''' script by typing
  
 
'''perl variables dot pl'''
 
'''perl variables dot pl'''
Line 202: Line 191:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point with the cursor to
+
| Point with the cursor to
  
 
'''Value of variable is: 1'''
 
'''Value of variable is: 1'''
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| The output displayed is as highlighted
+
| The output displayed is as highlighted.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch back to the Text editor window.
+
| Switch back to the Text editor window.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| We can also assign a string value to the variable we declared.
+
| 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.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight '''1'''<nowiki>; with the cursor</nowiki>
+
| Highlight '''1'''with the cursor
  
Instead of '''1''' type
+
Instead of '''1''', type ''''high''''
 
+
| Instead of '''dollar priority equal to one;'''  
''''high''''
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Instead of '''dollar priority equal to one;'''  
+
  
 
type
 
type
Line 226: Line 213:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point from right of '''<nowiki>= </nowiki>'''to left.
+
| Point from right of '''<nowiki>= </nowiki>'''to left.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Please note that assignments are evaluated from right 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  
  
 
|-
 
|-
| style="border:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type
+
| Switch to the '''Terminal''' and type
  
 
'''perl -c variables.pl'''
 
'''perl -c variables.pl'''
  
 
and press '''Enter'''.
 
and press '''Enter'''.
| style="border:0.5pt solid #000000;padding:0.106cm;"| Save this file and compile the script once again by typing
+
| Save this file and compile the script once again by typing
  
  
Line 245: Line 232:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type
+
| Type
  
 
'''perl variables.pl'''
 
'''perl variables.pl'''
  
 
and press '''Enter'''.
 
and press '''Enter'''.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| This tells us that there is no syntax error.
+
| This tells us that there is no syntax error.
  
  
Line 260: Line 247:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point with the cursor to
+
| Point with the cursor to
  
 
'''Value of variable is: high'''
 
'''Value of variable is: high'''
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| The output is as shown
+
| The output is as shown.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch back to the Text editor window.
+
| Switch back to the Text editor window.
  
 
Go to the last line and press '''Enter.'''
 
Go to the last line and press '''Enter.'''
Line 273: Line 260:
  
 
'''$priority = “String”;'''
 
'''$priority = “String”;'''
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Switch back to the Text Editor window.
+
| 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.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"|
+
|
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now let us learn how to declare multiple variables.
+
| Now 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.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the '''Terminal''' and type  
+
| Switch to the '''Terminal''' and type  
  
 
'''gedit multivar.pl &'''
 
'''gedit multivar.pl &'''
  
 
and press '''Enter'''.
 
and press '''Enter'''.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| on'''Terminal''' type -
+
| 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
  
 
|-
 
|-
| style="border:0.5pt solid #000000;padding:0.106cm;"| Type
+
| Type
  
 
'''$firstVar, $secondVar;'''
 
'''$firstVar, $secondVar;'''
  
 
and press '''Enter'''.
 
and press '''Enter'''.
| style="border:0.5pt solid #000000;padding:0.106cm;"| Now will type -
+
| Now will type -
  
 
'''dollar firstVar comma dollar secondVar semicolon'''
 
'''dollar firstVar comma dollar secondVar semicolon'''
Line 317: Line 304:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type
+
| Type
  
 
'''$firstVar = $secondVar;'''
 
'''$firstVar = $secondVar;'''
  
 
and press '''Enter'''.
 
and press '''Enter'''.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| To copy the value of variable '''dollar firstVar''' to '''dollar secondVar''', type -
+
| 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 329: Line 316:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to slide
+
| Switch to slide
  
 
+
| All mathematical operations like addition, subtraction, multiplication, division, can be done on these variables  
 
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| 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'''.
  
 
|-
 
|-
| style="border:0.5pt solid #000000;padding:0.106cm;"| '''$firstVar = $secondVar =10;'''
+
| '''$firstVar = $secondVar =10;'''
  
 
Press '''Enter'''.
 
Press '''Enter'''.
| style="border:0.5pt solid #000000;padding:0.106cm;"| 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'''.
+
| 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'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type
+
| Type
  
 
'''print “firstVar: $firstVar and secondVar: $secondVar”;'''
 
'''print “firstVar: $firstVar and secondVar: $secondVar”;'''
  
 
and press '''Enter'''.
 
and press '''Enter'''.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now To print the values, type
+
| 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 358: Line 347:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type
+
| Type
  
 
'''$addition = $firstVar + $secondVar;'''
 
'''$addition = $firstVar + $secondVar;'''
  
 
Press '''Enter'''.
 
Press '''Enter'''.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now let us add the values in the two variables.
+
| Now let us add the values in the two variables.
  
 
For this type
 
For this type
Line 372: Line 361:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight the variable '''addition '''with the cursor  
+
| Highlight the variable '''addition '''with the cursor  
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Notice, we haven't declared the variable '''addition'''.
+
| Notice, we haven't declared the variable '''addition'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type
+
| Type
  
 
'''print “Addition: $addition”;'''
 
'''print “Addition: $addition”;'''
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Once again to print the value of the addition, type
+
| Once again, to print the value of the '''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 386: Line 375:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type
+
| Switch to the Terminal and type
  
 
'''perl -c variables.pl'''
 
'''perl -c variables.pl'''
  
 
and press '''Enter'''.
 
and press '''Enter'''.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| To compile this file again on terminal type
+
| 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
+
There is no syntax error so we can execute the script...
 
+
we can execute the script by typing
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type
+
| Type
  
 
'''perl multivar.pl'''
 
'''perl multivar.pl'''
  
 
and press '''Enter'''.
 
and press '''Enter'''.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| '''perl multivar dot pl'''
+
|   ...by typing '''perl multivar dot pl'''
 
+
 
+
  
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point with the cursor to the output
+
| Point with the cursor to the output
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| This will produce an output as highlighted
+
| This will produce an output as highlighted.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Pause the video recording here
+
| Pause the video recording here
  
  
Line 421: 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'''.
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"|
+
|
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to '''gedit'''
+
| Switch to '''gedit'''
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Similarly, try subtraction, multiplication and division.
+
| Similarly, try subtraction, multiplication and division.
  
  
Line 435: Line 420:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Close this file in '''gedit'''
+
| Close this file in '''gedit'''
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Let us save this file and close it.
+
| Now let us save this file and close it.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Show the output in the '''terminal'''
+
| Show the output in the '''terminal'''
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now compile the file by typing
+
| Now compile the file by typing
  
perl hyphen c multivar dot pl
+
'''perl hyphen c multivar dot pl'''
  
There is no syntax error so we can execute the script as \
+
There is no syntax error.
  
perl multivar dot pl
+
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.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Summary
+
| Summary
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| This brings us to the end of this tutorial.
+
| 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'''
 
+
 
+
 
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Assignment
+
| Assignment
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| '''Assignment'''
+
| '''Assignment'''
  
# Declare a number variable.  
+
# Declare a number '''variable'''.  
 
# Assign the value 10 to it.  
 
# Assign the value 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 string variables one after the other.
+
# Print those '''string variables''' one after the other.
  
  
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| About the Project
+
| About the Project
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Watch the video available at the following link  
+
| Watch the video available at the following link  
  
 
It summarises the Spoken Tutorial project  
 
It summarises the Spoken Tutorial project  
Line 486: Line 470:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Spoken Tutorial Workshops  
+
| Spoken Tutorial Workshops  
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| The Spoken Tutorial Project Team  
+
| The Spoken Tutorial Project Team  
  
 
Conducts workshops using spoken tutorials  
 
Conducts workshops using spoken tutorials  
Line 500: Line 484:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Acknowledgment
+
| Acknowledgment
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Spoken Tutorial Project is a part of the Talk to a  
+
| Spoken Tutorial Project is a part of the Talk to a  
  
 
Teacher project  
 
Teacher project  
Line 516: Line 500:
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"|
+
|
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Hope you enjoyed this '''Perl''' tutorial. This is Amol Brahmankar signing off.
+
| Hope you enjoyed this '''Perl''' tutorial. This is Amol Brahmankar signing off.
  
 
Thanks for joining.
 
Thanks for joining.
  
 
|}
 
|}

Revision as of 13:29, 19 March 2013

Title of script: Variables in Perl

Author: Amol

Keywords: Variables in Perl, Video Tutorial, Scalars in Perl


Visual Cue
Narration
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


I will also be using the gedit Text Editor.

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


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.

Switch to the Terminal and type

gedit variables.pl &

and press Enter.

Now open the Terminal and type

gedit variables dot pl ampersand


The ampersand will unlock the command prompt on the terminal.

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

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 the following command;

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


perl hyphen c variables dot pl

now press Enter.

Type

perl variables.pl

and press Enter.

This tells us that there is no syntax error.


Execute the script by typing

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.


You can also use scalars within a double-quoted strings as

dollar priority in double quote String


Save this file and close it.

Now 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 -

gedit multivar dot pl space ampersand

and press Enter.


This will open multivar dot pl file in text editor

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 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


Type out the code for subtraction, multiplication and division in the multivar.pl file.


Resume the video recording now.


Also, compile the code so that the output is displayed on the Terminal.

Switch to gedit Similarly, try subtraction, multiplication and division.


I have written the code here.

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


On execution the output will look like this.

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
  1. Declare a number variable.
  2. Assign the value 10 to it.
  3. Print the variable declared.
  4. Declare 2 string variables.
  5. Assign these values to them - “Namaste ” and “India”.
  6. Print those string variables one after the other.


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.

Contributors and Content Editors

AmolBrahmankar, Nancyvarkey, Sneha