Difference between revisions of "PERL/C3/Access-Modifiers-in-PERL/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script''':''' Access Modifiers in PERL''' '''Author: Nirmala Venkat''' '''Keywords: Scope of variables, Dynamically scoped variable, global variable, gedit, vide...")
 
 
Line 75: Line 75:
 
| style="border:1pt solid #000000;padding:0.097cm;"| You can declare a '''variable''' without giving it a value, like this:
 
| style="border:1pt solid #000000;padding:0.097cm;"| You can declare a '''variable''' without giving it a value, like this:
  
'''my''' $fvalue<nowiki>;</nowiki>
+
'''my $fvalue<nowiki>;</nowiki>'''
  
 
You can also declare a '''variable''' by assigning a value to it, as:
 
You can also declare a '''variable''' by assigning a value to it, as:
  
'''my''' $fValue = 1;
+
'''my $fValue = 1;'''
  
'''my''' $fname = "Rahul";
+
'''my $fname = "Rahul";'''
  
 
The syntax to declare several '''variables '''with the same''' my''' statement is as follows:  
 
The syntax to declare several '''variables '''with the same''' my''' statement is as follows:  
  
'''my''' ($fname, $lname, $age);
+
'''my ($fname, $lname, $age);'''
  
 
|-
 
|-
Line 98: Line 98:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to the Terminal and type
+
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to the Terminal >> type '''gedit scope-my.pl &''' >> press '''Enter'''
 
+
'''gedit scope-my.pl &'''
+
 
| style="border:1pt solid #000000;padding:0.097cm;"| Open the '''terminal '''and type''' gedit scope-my dot pl ampersand '''and press '''Enter'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Open the '''terminal '''and type''' gedit scope-my dot pl ampersand '''and press '''Enter'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Point the cursor
+
| style="border:1pt solid #000000;padding:0.097cm;"| Point to the code
  
 
'''scope-my.pl'''
 
'''scope-my.pl'''
Line 114: Line 112:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight in gedit
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code as per narration
  
 
| style="border:1pt solid #000000;padding:0.097cm;"| Here, I have declared a '''private variable $fname''' with''' my''' keyword.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Here, I have declared a '''private variable $fname''' with''' my''' keyword.
  
And assigned the value '''Raghu'''to it.
+
And assigned the value "'''Raghu'''" to it.
  
Within this block, the '''print''' '''statement''' prints the value in the '''fname variable''', i.e. '''Raghu'''.
+
Within this block, the '''print statement''' prints the value in the '''fname variable''', i.e. "'''Raghu'''".
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code as per narration
| style="border:1pt solid #000000;padding:0.097cm;"| In the next block, I have assigned the value '''Other'''to the same '''private variable $fname.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| In the next block, I have assigned the value "'''Other'''" to the same '''private variable $fname.'''
  
So, the '''print statement '''will print '''Other'''within this particular block.
+
So, the '''print statement '''will print "'''Other'''" within this particular block.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code as per narration
 
| style="border:1pt solid #000000;padding:0.097cm;"| The last '''print statement '''in this program, will not print any output.  
 
| style="border:1pt solid #000000;padding:0.097cm;"| The last '''print statement '''in this program, will not print any output.  
 +
  
 
This is because outside the scope of the blocks defined earlier, '''fname '''has no value assigned to it.
 
This is because outside the scope of the blocks defined earlier, '''fname '''has no value assigned to it.
Line 143: Line 142:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to terminal
+
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to terminal >> type
 
+
'''perl scope-my dot pl''' >> press '''Enter'''
'''perl scope-my dot pl'''
+
 
| style="border:1pt solid #000000;padding:0.097cm;"| Switch back to the '''terminal '''and type
 
| style="border:1pt solid #000000;padding:0.097cm;"| Switch back to the '''terminal '''and type
  
'''perl scope-my dot pl '''and press Enter
+
'''perl scope-my dot pl '''and press '''Enter'''.
  
 
|-
 
|-
Line 162: Line 160:
 
'''Outside Block: '''( There is no output)
 
'''Outside Block: '''( There is no output)
  
So, the scope of the '''my''' '''variable '''is accessed only within a particular block of code.
+
So, the scope of the '''my variable '''is accessed only within a particular block of code.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight in gedit
+
| style="border:1pt solid #000000;padding:0.097cm;"| Come back to gedit >> Add ''' my $fname = "John";''' outside the blocks, before the last '''print statement'''.
 +
 
 +
Press Ctrl+S
  
 
| style="border:1pt solid #000000;padding:0.097cm;"| Now let us change the existing program a little.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Now let us change the existing program a little.
  
Let us add''' my $fname<nowiki>= "</nowiki>John";''' outside the blocks, before the last '''print statement'''.
+
Let us add''' my $fname = "John";''' outside the blocks, before the last '''print statement'''.
  
 
Save the changes.
 
Save the changes.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| On the terminal
+
| style="border:1pt solid #000000;padding:0.097cm;"| On the terminal, press up-arrow key
 
| style="border:1pt solid #000000;padding:0.097cm;"| Switch back to the '''terminal '''and execute as before.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Switch back to the '''terminal '''and execute as before.
  
 
Analyse the output that is displayed.
 
Analyse the output that is displayed.
  
Hope you are able to understand the scope of using''' my''' '''variable '''within a block and outside a block.
+
Hope you are able to understand the scope of using''' my variable '''within a block and outside a block.
  
 
|-
 
|-
Line 192: Line 192:
 
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 9:
 
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 9:
  
| style="border:1pt solid #000000;padding:0.097cm;"| '''Local''' '''keyword''' gives a temporary scope to a '''global''' '''variable'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Local keyword''' gives a temporary scope to a '''global variable'''.
  
 
The '''variable''' is visible to any '''function''' called from the original block.
 
The '''variable''' is visible to any '''function''' called from the original block.
Line 201: Line 201:
 
| style="border:1pt solid #000000;padding:0.097cm;"| You can declare a '''local variable '''as,
 
| style="border:1pt solid #000000;padding:0.097cm;"| You can declare a '''local variable '''as,
  
'''local''' $fValue = 100;
+
'''local $fValue = 100;'''
  
'''local''' $fname = "Rakesh";
+
'''local $fname = "Rakesh";'''
  
 
|-
 
|-
Line 210: Line 210:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to the terminal  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to the terminal >> type gedit scope-local.pl & >> press '''Enter'''
| style="border:1pt solid #000000;padding:0.097cm;"| Open the '''terminal''' and type''' '''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Open the '''terminal''' and type  
  
'''gedit scope-local dot pl''' '''ampersand''' and press '''Enter'''.
+
'''gedit scope-local dot pl ampersand''' and press '''Enter'''.
  
 
|-
 
|-
Line 225: Line 225:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight in gedit
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code in gedit
  
 
| style="border:1pt solid #000000;padding:0.097cm;"| Here, in the first line we have declared a '''variable $fname '''and initialised it.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Here, in the first line we have declared a '''variable $fname '''and initialised it.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code in gedit
 
| style="border:1pt solid #000000;padding:0.097cm;"| Inside the '''function Welcome, '''we have declared a '''local variable '''by the same name,''' $fname.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Inside the '''function Welcome, '''we have declared a '''local variable '''by the same name,''' $fname.'''
  
Notice the '''local''' '''keyword''' before the '''variable '''name.
+
Notice the '''local keyword''' before the '''variable '''name.
  
And we have assigned the value '''Rakesh'''to this '''variable.'''
+
And we have assigned the value "'''Rakesh'''" to this '''variable.'''
  
So, basically, inside function '''Welcome(), $fname''' is modified as a new temporary '''local''' '''variable.'''
+
So, basically, inside function '''Welcome(), $fname''' is modified as a new temporary '''local variable.'''
  
 
Then, the '''function Hello '''is being '''called'''.
 
Then, the '''function Hello '''is being '''called'''.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code in gedit
 
| style="border:1pt solid #000000;padding:0.097cm;"| Here is the '''function definition '''of '''Hello.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Here is the '''function definition '''of '''Hello.'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code in gedit
 
| style="border:1pt solid #000000;padding:0.097cm;"| At the end of the program, we are calling both the '''functions Welcome '''and '''Hello'''.
 
| style="border:1pt solid #000000;padding:0.097cm;"| At the end of the program, we are calling both the '''functions Welcome '''and '''Hello'''.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Press''' Ctrl + S'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Now press''' Ctrl + S''' to save the program.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Now press''' Ctrl + S''' to save the program.
  
Line 258: Line 258:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to terminal
+
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to terminal >> type '''perl scope-local.pl''' >> press '''Enter'''
  
 
| style="border:1pt solid #000000;padding:0.097cm;"| Switch back to the '''terminal''' and type,
 
| style="border:1pt solid #000000;padding:0.097cm;"| Switch back to the '''terminal''' and type,
Line 280: Line 280:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to program
 
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to program
| style="border:1pt solid #000000;padding:0.097cm;"| When the function '''Welcome()''' is called, the '''function''' '''Hello()''' within it, accesses the '''local variable'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| When the function '''Welcome()''' is called, the '''function Hello()''' within it, accesses the '''local variable'''.
  
Within '''Welcome(), $fname''' has the value '''Rakesh'''.
+
Within '''Welcome(), $fname''' has the value "'''Rakesh'''".
  
After this, the '''function''' '''Hello() '''accesses the '''variable $fname '''once again'''.'''
+
After this, the '''function Hello() '''accesses the '''variable $fname '''once again'''.'''
  
But this time, it is the '''variable $fname '''which was initialized to''' “Welcome to spoken tutorials”.'''
+
But this time, it is the '''variable $fname '''which was initialized to''' "Welcome to spoken tutorials".'''
  
 
It does not access the '''local variable $fname '''within the function '''Welcome().'''
 
It does not access the '''local variable $fname '''within the function '''Welcome().'''
Line 298: Line 298:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Next we will see about '''global variables''' in '''Perl'''.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Next, we will see about '''global variables''' in '''Perl'''.
  
 
|-
 
|-
Line 308: Line 308:
 
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 10:
 
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 10:
  
| style="border:1pt solid #000000;padding:0.097cm;"| '''Global variables '''are declared with''' our''' '''keyword'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Global variables '''are declared with''' our keyword'''.
  
 
Here are some examples.
 
Here are some examples.
  
'''our''' $fvalue<nowiki>=100;</nowiki>
+
'''our $fvalue = 100;</nowiki>'''
  
'''our''' $fname =”Priya”;
+
'''our $fname ="Priya";'''
  
 
|-
 
|-
Line 321: Line 321:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to the Terminal  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to the Terminal >> type '''gedit scope-our.pl''' & >> press '''Enter'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Switch back to the '''terminal '''and type
 
| style="border:1pt solid #000000;padding:0.097cm;"| Switch back to the '''terminal '''and type
  
Line 334: Line 334:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight in gedit  
 
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight in gedit  
| style="border:1pt solid #000000;padding:0.097cm;"| I have declared '''package main '''and''' '''a '''global variable''' as''' our $i '''and I have initialised it to '''100;'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| I have declared '''package main '''and a '''global variable''' as''' our $i '''and I have initialised it to '''100;'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code
 
| style="border:1pt solid #000000;padding:0.097cm;"| Notice the''' package First''' declaration.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Notice the''' package First''' declaration.
  
A '''package''' is a collection of code, which has its own '''namespace'''.
+
*A '''package''' is a collection of code, which has its own '''namespace'''.
  
'''Namespace''' prevents '''variable name collisions''' between '''packages'''.
+
*'''Namespace''' prevents '''variable name collisions''' between '''packages'''.
  
We will see more about '''package''' and '''namespace''' in future tutorials.
+
*We will see more about '''package''' and '''namespace''' in future tutorials.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code
| style="border:1pt solid #000000;padding:0.097cm;"| Within '''package First,''' the '''global variable''' “'''i'''holds the value 10.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Within '''package First,''' the '''global variable "i"''' holds the value 10.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code
| style="border:1pt solid #000000;padding:0.097cm;"| In '''package Second,''' the '''global variable “i” '''is assigned the value 20.
+
| style="border:1pt solid #000000;padding:0.097cm;"| In '''package Second,''' the '''global variable "i" '''is assigned the value 20.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code
 
| style="border:1pt solid #000000;padding:0.097cm;"| The main '''package '''uses both '''package First variable '''and the '''package Second variable'''.
 
| style="border:1pt solid #000000;padding:0.097cm;"| The main '''package '''uses both '''package First variable '''and the '''package Second variable'''.
  
In my program, I have declared the same variable '''i'''in all the '''packages'''.
+
In my program, I have declared the same variable '''"i"''' in all the '''packages'''.
  
The '''package variable''' is referred by''' '''
+
The '''package variable''' is referred by '''package name::variable name'''
  
'''package name::variable name'''
+
In our example it is''' $First::i, $Second::i'''
 
+
In our example it is''' $First::i , $Second::i'''
+
  
 
We have multiple '''packages '''within one file, and the '''global variable '''will be accessed by all the '''packages'''.
 
We have multiple '''packages '''within one file, and the '''global variable '''will be accessed by all the '''packages'''.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Press Ctrl+S 
 
| style="border:1pt solid #000000;padding:0.097cm;"| Now, save the file and execute the program.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Now, save the file and execute the program.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to the Terminal  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to the Terminal >> type '''perl scope-our.pl''' & >> press '''Enter'''
  
 
| style="border:1pt solid #000000;padding:0.097cm;"| So, switch to '''terminal '''and type-
 
| style="border:1pt solid #000000;padding:0.097cm;"| So, switch to '''terminal '''and type-
Line 403: Line 401:
  
 
* scope of variables
 
* scope of variables
* declaration of private variables
+
* declaration of '''private variables'''
* dynamically scoped variables and
+
* '''dynamically scoped variables''' and
* global variables with examples.<br/>  
+
* '''global variables''' with examples.<br/>  
  
 
It is preferred to use''' my''' than''' local''' as the compilation is faster.
 
It is preferred to use''' my''' than''' local''' as the compilation is faster.
Line 423: Line 421:
 
# Declare a variable '''$age''' as '''our''' and assign the value '''42'''
 
# Declare a variable '''$age''' as '''our''' and assign the value '''42'''
 
# Declare another package as '''SecondModule'''
 
# Declare another package as '''SecondModule'''
# Declare a variable '''$ageword''' as '''our''' and assign the value '''“Forty-Two”'''
+
# Declare a variable '''$ageword''' as '''our''' and assign the value '''"Forty-Two"'''
# Declare''' '''a subroutine''' First()'''
+
# Declare a subroutine''' First()'''
# Inside the subroutine declare two variables with '''local''' and '''my''' keyword as below:
+
# Inside the subroutine, declare two variables with '''local''' and '''my''' keyword as below:
#:'''local''' '''$age<nowiki>=52</nowiki>'''<nowiki>;</nowiki>
+
#:'''local''' '''$age = 52''';
#:'''my''' '''$ageword<nowiki>="</nowiki>Fifty-two";'''
+
#:'''my''' '''$ageword ="Fifty-two";'''
 
# Call another subroutine as '''Result()'''
 
# Call another subroutine as '''Result()'''
 
# Print the values of '''$age''' and '''$ageword''' inside this function.
 
# Print the values of '''$age''' and '''$ageword''' inside this function.
Line 435: Line 433:
 
# End the subroutine
 
# End the subroutine
 
# Call the function '''First()'''
 
# Call the function '''First()'''
# Print the Package First and package Second as below:
+
# Print the '''Package First''' and '''Package Second''' as below:
 
#:'''print "Package First : $FirstModule::age\n";'''
 
#:'''print "Package First : $FirstModule::age\n";'''
 
#:'''print "Package Second : $SecondModule::ageword\n";'''
 
#:'''print "Package Second : $SecondModule::ageword\n";'''

Latest revision as of 07:29, 30 May 2015

Title of script: Access Modifiers in PERL

Author: Nirmala Venkat

Keywords: Scope of variables, Dynamically scoped variable, global variable, gedit, video tutorial

Visual Cue
Narration
Slide 1: Welcome to the Spoken Tutorial on Access Modifiers in PERL
Slide 2:

Learning objectives

In this tutorial we will learn about
  • Scope of variables
  • Private variables
  • Dynamically scoped variables
  • Global variables
Slide 3:

System requirements

For this tutorial, I am using
  • Ubuntu Linux 12.04 operating system
  • Perl 5.14.2
  • and the gedit Text Editor

You can use any text editor of your choice.

Slide 4:

Prerequisites

Prerequisites

You should have basic knowledge of Perl Programming.

If not, then go through the relevant Perl spoken tutorials on the spoken tutorial website.

Slide 5: Let us start with the introduction to the Scope of variables.

The scope of a variable is the region of code within which a variable can be accessed.

In other words, it refers to the visibility of variables.

Slide 6: First, we will discuss about my, local and our modifiers in Perl.
  • my means Private variables
  • local means Dynamically scoped variables
  • our means Global variables
Slide 7: Variables declared with my keyword will lose scope outside the block in which they are declared.
Slide 8: You can declare a variable without giving it a value, like this:

my $fvalue;

You can also declare a variable by assigning a value to it, as:

my $fValue = 1;

my $fname = "Rahul";

The syntax to declare several variables with the same my statement is as follows:

my ($fname, $lname, $age);

Let us understand private variables using a sample program.
Switch to the file in gedit. I already have a sample program.

Let me open it in gedit Text editor.

Switch to the Terminal >> type gedit scope-my.pl & >> press Enter Open the terminal and type gedit scope-my dot pl ampersand and press Enter
Point to the code

scope-my.pl

Scope-my dot pl file is now open in gedit.

Type the following code as displayed on the screen.

Let me explain the code now.

Highlight the code as per narration Here, I have declared a private variable $fname with my keyword.

And assigned the value "Raghu" to it.

Within this block, the print statement prints the value in the fname variable, i.e. "Raghu".

Highlight the code as per narration In the next block, I have assigned the value "Other" to the same private variable $fname.

So, the print statement will print "Other" within this particular block.

Highlight the code as per narration The last print statement in this program, will not print any output.


This is because outside the scope of the blocks defined earlier, fname has no value assigned to it.

Press Ctrl+S Now, press Ctrl+S to save the file.
Let us now execute the program.
Switch to terminal >> type

perl scope-my dot pl >> press Enter

Switch back to the terminal and type

perl scope-my dot pl and press Enter.

Highlight

Output

The output is displayed as

Block 1: Raghu

Block 2: Other

Outside Block: ( There is no output)

So, the scope of the my variable is accessed only within a particular block of code.

Come back to gedit >> Add my $fname = "John"; outside the blocks, before the last print statement.

Press Ctrl+S

Now let us change the existing program a little.

Let us add my $fname = "John"; outside the blocks, before the last print statement.

Save the changes.

On the terminal, press up-arrow key Switch back to the terminal and execute as before.

Analyse the output that is displayed.

Hope you are able to understand the scope of using my variable within a block and outside a block.

<<PAUSE>>
Next we will see about dynamically scoped variable in Perl.
Slide 9: Local keyword gives a temporary scope to a global variable.

The variable is visible to any function called from the original block.

Slide 9: You can declare a local variable as,

local $fValue = 100;

local $fname = "Rakesh";

Let us understand this using a sample program.
Switch to the terminal >> type gedit scope-local.pl & >> press Enter Open the terminal and type

gedit scope-local dot pl ampersand and press Enter.

Point the cursor This will open scope-local dot pl file in gedit.

Type the following code as displayed on the screen.

Let me explain the code now.

Highlight the code in gedit Here, in the first line we have declared a variable $fname and initialised it.
Highlight the code in gedit Inside the function Welcome, we have declared a local variable by the same name, $fname.

Notice the local keyword before the variable name.

And we have assigned the value "Rakesh" to this variable.

So, basically, inside function Welcome(), $fname is modified as a new temporary local variable.

Then, the function Hello is being called.

Highlight the code in gedit Here is the function definition of Hello.
Highlight the code in gedit At the end of the program, we are calling both the functions Welcome and Hello.
Press Ctrl + S Now press Ctrl + S to save the program.
Let us execute the program.
Switch to terminal >> type perl scope-local.pl >> press Enter Switch back to the terminal and type,

perl scope-local.pl and press Enter.

Highlight

Output

The output is displayed as

Hello, Rakesh !

Hello, Welcome to Spoken tutorials!

Let us understand the output.
Switch to program When the function Welcome() is called, the function Hello() within it, accesses the local variable.

Within Welcome(), $fname has the value "Rakesh".

After this, the function Hello() accesses the variable $fname once again.

But this time, it is the variable $fname which was initialized to "Welcome to spoken tutorials".

It does not access the local variable $fname within the function Welcome().

Which means that, the local variable restores the scope, after leaving the block Welcome().

<<PAUSE>>
Next, we will see about global variables in Perl.
Slide 10: A global variable can be accessed anywhere in the program.
Slide 10: Global variables are declared with our keyword.

Here are some examples.

our $fvalue = 100;</nowiki>

our $fname ="Priya";

Now let us look at a working example of global variables.
Switch to the Terminal >> type gedit scope-our.pl & >> press Enter Switch back to the terminal and type

gedit scope-our dot pl ampersand and press Enter

Point to the file name This will open the file scope-our.pl in gedit.

Let me explain the sample program which I have written.

Highlight in gedit I have declared package main and a global variable as our $i and I have initialised it to 100;
Highlight the code Notice the package First declaration.
  • A package is a collection of code, which has its own namespace.
  • Namespace prevents variable name collisions between packages.
  • We will see more about package and namespace in future tutorials.
Highlight the code Within package First, the global variable "i" holds the value 10.
Highlight the code In package Second, the global variable "i" is assigned the value 20.
Highlight the code The main package uses both package First variable and the package Second variable.

In my program, I have declared the same variable "i" in all the packages.

The package variable is referred by package name::variable name

In our example it is $First::i, $Second::i

We have multiple packages within one file, and the global variable will be accessed by all the packages.

Press Ctrl+S Now, save the file and execute the program.
Switch to the Terminal >> type perl scope-our.pl & >> press Enter So, switch to terminal and type-

perl scope-our dot pl

and press Enter.

The output is as displayed on the terminal.

<Pause>

Analyze the output by yourself to understand how the assignment to the variable i was done.
<<PAUSE>>
Slide 11:

Summary

This brings us to the end of this tutorial. Let us summarise.

In this tutorial, we learnt:

  • scope of variables
  • declaration of private variables
  • dynamically scoped variables and
  • global variables with examples.

It is preferred to use my than local as the compilation is faster.

Slide 12:

Assignment

Assignment

Here is an assignment for you.

Write the code for the following assignment and execute it.

  1. Declare a package as FirstModule
  2. Declare a variable $age as our and assign the value 42
  3. Declare another package as SecondModule
  4. Declare a variable $ageword as our and assign the value "Forty-Two"
  5. Declare a subroutine First()
  6. Inside the subroutine, declare two variables with local and my keyword as below:
    local $age = 52;
    my $ageword ="Fifty-two";
  7. Call another subroutine as Result()
  8. Print the values of $age and $ageword inside this function.
  9. End the subroutine
  10. Declare the subroutine Result()
  11. Again print the values of $age and $ageword
  12. End the subroutine
  13. Call the function First()
  14. Print the Package First and Package Second as below:
    print "Package First : $FirstModule::age\n";
    print "Package Second : $SecondModule::ageword\n";
Slide 12:

About the Spoken Tutorial Project

The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

Slide 13:

About workshops

The Spoken Tutorial Project Team conducts workshops and gives certificates for those who pass an online test.

For more details, please write to us.

Slide 14:

Acknowledgment

Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.

More information on this mission is available at

this link.

This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat