PERL/C3/Access-Modifiers-in-PERL/English-timed
From Script | Spoken-Tutorial
| |
|
| 00:01 | Welcome to the Spoken Tutorial on Access Modifiers in PERL. |
| 00:07 | In this tutorial, we will learn about:
Scope of variables Private variables Dynamically scoped variables Global variables |
| 00:19 | For this tutorial, I am using:
Ubuntu Linux 12.04 operating system Perl 5.14.2 and the gedit Text Editor. |
| 00:32 | You can use any text editor of your choice. |
| 00:36 | You should have basic knowledge of Perl programming. |
| 00:40 | If not, then go through the relevant Perl spoken tutorials on the spoken tutorial website. |
| 00:47 | Let us start with the introduction to the scope of variables . |
| 00:51 | The scope of a variable is the region of code within which a variable can be accessed. |
| 00:58 | In other words, it refers to the visibility of variables. |
| 01:03 | First, we will discuss about my, local and our modifiers in Perl. |
| 01:10 | my means Private variables, |
| 01:13 | local means Dynamically scoped variables, |
| 01:17 | our means Global variables. |
| 01:20 | Variables declared with my keyword will lose scope outside the block in which they are declared. |
| 01:28 | You can declare a variable without giving it a value, like this:
my $fvalue semicolon |
| 01:37 | You can also declare a variable by assigning a value to it, as: |
| 01:43 | my $fValue = 1 semicolon |
| 01:48 | my $fname = within double quotes Rahul semicolon |
| 01:55 | The syntax to declare several variables with the same my statement is as follows: |
| 02:02 | my open bracket $fname comma $lname comma $age close bracket semicolon |
| 02:12 | Let us understand private variables using a sample program. |
| 02:17 | I already have a sample program. Let me open it in 'gedit' Text editor. |
| 02:24 | Open the terminal and type: gedit scope hyphen my dot pl ampersand and press Enter. |
| 02:34 | Scope-my dot pl file is now open in gedit. |
| 02:39 | Type the following code as displayed on the screen. Let me explain the code now. |
| 02:46 | Here, I have declared a private variable $fname with 'my' keyword |
| 02:52 | and assigned the value "Raghu" to it. |
| 02:56 | Within this block, the print statement prints the value in the fname variable i.e. "Raghu". |
| 03:04 | In the next block, I have assigned the value "Other" to the same private variable $fname. |
| 03:11 | So, the print statement will print "Other", within this particular block. |
| 03:17 | The last print statement in this program will not print any output. |
| 03:23 | This is because, outside the scope of the blocks defined earlier, fname has no value assigned to it. |
| 03:32 | Now, press Ctrl+S to save the file. |
| 03:37 | Let us now execute the program. |
| 03:40 | Switch back to the terminal and type: perl scope hyphen my dot pl and press Enter. |
| 03:49 | The output is displayed as:
"Block 1: Raghu" "Block 2: Other" "Outside Block: " there is no output. |
| 03:59 | So, the scope of the 'my' variable is accessed only within a particular block of code. |
| 04:06 | Now, let us change the existing program a little. |
| 04:10 | Let us add my $fname = within double quotes John semicolon outside the blocks, before the last print statement.
Save the changes. |
| 04:23 | Switch back to the terminal and execute as before. |
| 04:28 | Analyze the output that is displayed. |
| 04:32 | Hope you are able to understand the scope of using 'my' variable within a block and outside a block. |
| 04:41 | Next, we will see about dynamically scoped variable in Perl. |
| 04:47 | Local keyword gives a temporary scope to a global variable. |
| 04:52 | The variable is visible to any function called from the original block. |
| 04:58 | You can declare a local variable as:
local $fValue = 100 semicolon local $fname = within double quotes "Rakesh" semicolon |
| 05:13 | Let us understand this, using a sample program. |
| 05:17 | Open the terminal and type: gedit scope hyphen local dot pl ampersand and press Enter. |
| 05:27 | This will open scope hyphen local dot pl file, in gedit. |
| 05:33 | Type the following code as displayed on the screen. Let me explain the code now. |
| 05:40 | Here, in the first line, we have declared a variable $fname and initialized it. |
| 05:47 | Inside the function Welcome(), we have declared a local variable by the same name, $fname. |
| 05:54 | Notice the local keyword before the variable name |
| 05:59 | and we have assigned the value "Rakesh" to this variable. |
| 06:03 | So, basically, inside function Welcome(), $fname is modified as a new temporary local variable.Then, the function Hello() is being called. |
| 06:15 | Here is the function definition of Hello(). |
| 06:18 | At the end of the program, we are calling both the functions Welcome() and Hello(). |
| 06:25 | Now press Ctrl + S to save the program. |
| 06:29 | Let us execute the program. |
| 06:31 | Switch back to the terminal and type: perl scope hyphen local.pl and press Enter. |
| 06:41 | The output is displayed as:
"Hello, Rakesh"! "Hello, Welcome to Spoken tutorials!" |
| 06:48 | Let us understand the output. |
| 06:51 | When the function Welcome() is called, the function Hello() within it, accesses the local variable. |
| 06:59 | Within Welcome(), $fname has the value "Rakesh". |
| 07:04 | After this, the function Hello() accesses the variable $fname once again. |
| 07:11 | But this time, it is the variable $fname which was initialized to "Welcome to spoken tutorials". |
| 07:19 | It does not access the local variable $fname within the function Welcome(). |
| 07:25 | Which means that, the local variable restores the scope, after leaving the block Welcome(). |
| 07:32 | Next, we will see about global variables in Perl. |
| 07:38 | A global variable can be accessed anywhere in the program. |
| 07:43 | Global variables are declared with 'our' keyword. |
| 07:47 | Here are some examples.
our $fvalue = 100 semicolon </nowiki> our $fname =within double quotes Priya semicolon |
| 08:01 | Now, let us look at a working example of global variables. |
| 08:06 | Switch back to the terminal and type: gedit scope hyphen our dot pl ampersand and press Enter. |
| 08:16 | This will open the file scope hyphen our.pl in gedit. |
| 08:22 | Let me explain the sample program which I have written. |
| 08:27 | I have declared package main and a global variable as our $i and I have initialized it to 100. |
| 08:37 | Notice the package First declaration. |
| 08:40 | A package is a collection of code which has its own namespace. |
| 08:46 | Namespace prevents variable name collisions between packages. |
| 08:51 | We will see more about package and namespace in future tutorials. |
| 08:56 | Within package First, the global variable "i" holds the value 10. |
| 09:02 | In package Second, the global variable "i" is assigned the value 20. |
| 09:08 | The main package uses both package First variable and the package Second variable. |
| 09:15 | In my program, I have declared the same variable "i" in all the packages. |
| 09:21 | The package variable is referred by package name colon colon variable name. |
| 09:29 | In our example, it is $First colon colon i, $Second colon colon i. |
| 09:39 | We have multiple packages within one file and the global variable will be accessed by all the packages. |
| 09:47 | Now, save the file and execute the program. |
| 09:51 | So, switch to terminal and type: perl scope hyphen our dot pl and press Enter. |
| 09:59 | The output is as displayed on the terminal. |
| 10:03 | Analyze the output by yourself to understand how the assignment to the variable i was done. |
| 10:11 | This brings us to the end of this tutorial. Let us summarize. |
| 10:16 | In this tutorial, we learnt:
scope of variables
declaration of private variables
dynamically scoped variables and
global variables with examples. |
| 10:29 | It is preferred to use 'my' than local, as the compilation is faster. |
| 10:35 | Here is an assignment for you. |
| 10:37 | Write the code for the following assignment and execute it. |
| 10:42 | Declare a package as FirstModule. |
| 10:46 | Declare a variable $age as our and assign the value 42. |
| 10:52 | Declare another package as SecondModule. |
| 10:56 | Declare a variable $ageword as our and assign the value within double quotes "Forty-Two". |
| 11:05 | Declare a subroutine First(). |
| 11:08 | Inside the subroutine, declare two variables with local and my keyword as below: |
| 11:16 | local $age = 52 semicolon |
| 11:20 | my $ageword = within double quotes Fifty-two semicolon |
| 11:27 | Call another subroutine as Result(). |
| 11:31 | Print the values of $age and $ageword inside this function. |
| 11:37 | End the subroutine. |
| 11:39 | Declare the subroutine Result(). |
| 11:42 | Again print the values of $age and $ageword. |
| 11:47 | End the subroutine. |
| 11:49 | Call the function First(). |
| 11:51 | Print the Package First and Package Second as below: |
| 11:57 | The video at the following link summarizes the Spoken Tutorial project.
Please download and watch it. |
| 12:05 | The Spoken Tutorial project team conducts workshops and gives certificates for those who pass an online test.
For more details, please write to us. |
| 12:18 | Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.
More information on this mission is available at this link. |
| 12:31 | This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching. |