PERL/C3/Access-Modifiers-in-PERL/English

From Script | Spoken-Tutorial
Revision as of 07:29, 30 May 2015 by Nancyvarkey (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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