Difference between revisions of "PERL/C3/Access-Modifiers-in-PERL/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) (Created page with " {| Border = 1 | <center>''' Time '''</center> | <center>'''Narration'''</center> |- |00:01 |Welcome to the''' Spoken Tutorial''' on''' Access Modifiers in PERL''' |- |00:07...") |
PoojaMoolya (Talk | contribs) |
||
(9 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
|- | |- | ||
|00:01 | |00:01 | ||
− | |Welcome to the''' Spoken Tutorial''' on''' Access Modifiers in PERL''' | + | |Welcome to the''' Spoken Tutorial''' on''' Access Modifiers in PERL'''. |
|- | |- | ||
|00:07 | |00:07 | ||
− | |In this tutorial we will learn about | + | |In this tutorial, we will learn about: |
− | + | Scope of variables | |
− | + | '''Private''' variables | |
− | + | '''Dynamically scoped''' variables | |
− | + | '''Global''' variables | |
− | + | ||
|- | |- | ||
|00:19 | |00:19 | ||
− | |For this tutorial, I am using | + | |For this tutorial, I am using: |
− | + | '''Ubuntu Linux 12.04''' operating system | |
− | + | '''Perl 5.14.2''' and the | |
− | + | '''gedit''' Text Editor. | |
− | + | ||
|- | |- | ||
|00:32 | |00:32 | ||
− | |You can use any text editor of your choice. | + | |You can use any '''text editor''' of your choice. |
|- | |- | ||
|00:36 | |00:36 | ||
− | |You should have basic knowledge of '''Perl''' | + | |You should have basic knowledge of '''Perl''' programming. |
|- | |- | ||
Line 39: | Line 37: | ||
|- | |- | ||
|00:47 | |00:47 | ||
− | |Let us start with the introduction to the | + | |Let us start with the introduction to the scope of variables . |
|- | |- | ||
Line 53: | Line 51: | ||
|First, we will discuss about''' my, local''' and''' our''' modifiers in '''Perl.''' | |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 | |01:20 | ||
− | | | + | |Variables declared with''' my''' keyword will lose scope outside the '''block''' in which they are declared. |
|- | |- | ||
|01:28 | |01:28 | ||
− | |You can declare a | + | |You can declare a variable without giving it a value, like this: |
− | '''my $fvalue<nowiki>semicolon</nowiki>''' | + | '''my $fvalue<nowiki> semicolon </nowiki>''' |
|- | |- | ||
|01:37 | |01:37 | ||
− | |You can also declare a | + | |You can also declare a variable by assigning a value to it, as: |
|- | |- | ||
Line 76: | Line 82: | ||
|- | |- | ||
| 01:48 | | 01:48 | ||
− | |'''my $fname = within double quotes Rahul semicolon''' | + | |'''my $fname''' = within double quotes '''Rahul semicolon''' |
|- | |- | ||
| 01:55 | | 01:55 | ||
− | |The syntax to declare several | + | |The syntax to declare several variables with the same''' my''' statement is as follows: |
|- | |- | ||
| 02:02 | | 02:02 | ||
− | |'''my open bracket '''$fname comma $lname comma $age | + | |'''my''' open bracket '''$fname''' comma '''$lname''' comma '''$age''' close bracket semicolon |
|- | |- | ||
|02:12 | |02:12 | ||
− | |Let us understand '''private | + | |Let us understand '''private''' variables using a sample program. |
|- | |- | ||
|02:17 | |02:17 | ||
− | |I already have a sample program. Let me open it in '''gedit Text editor'''. | + | |I already have a sample program. Let me open it in ''''gedit' Text editor'''. |
|- | |- | ||
|02:24 | |02:24 | ||
− | |Open the '''terminal '''and type''' gedit scope hyphen my dot pl ampersand '''and press '''Enter''' | + | |Open the '''terminal '''and type:''' gedit scope hyphen my dot pl ampersand '''and press '''Enter'''. |
|- | |- | ||
Line 108: | Line 114: | ||
|- | |- | ||
|02:46 | |02:46 | ||
− | |Here, I have declared a '''private variable $fname''' with | + | |Here, I have declared a '''private''' variable '''$fname''' with 'my' keyword |
|- | |- | ||
| 02:52 | | 02:52 | ||
− | | | + | |and assigned the value "Raghu" to it. |
|- | |- | ||
|02:56 | |02:56 | ||
− | |Within this block, the '''print | + | |Within this block, the '''print''' statement prints the value in the '''fname''' variable i.e. "Raghu". |
|- | |- | ||
|03:04 | |03:04 | ||
− | |In the next block, I have assigned the value " | + | |In the next block, I have assigned the value "Other" to the same '''private''' variable '''$fname.''' |
|- | |- | ||
|03:11 | |03:11 | ||
− | |So, the '''print statement '''will print " | + | |So, the '''print statement '''will print "Other", within this particular block. |
|- | |- | ||
|03:17 | |03:17 | ||
− | |The last '''print statement '''in this program | + | |The last '''print statement '''in this program will not print any output. |
|- | |- | ||
|03:23 | |03:23 | ||
− | |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 144: | Line 150: | ||
|- | |- | ||
|03:40 | |03:40 | ||
− | |Switch back to the '''terminal '''and type '''perl scope hyphen my dot pl '''and press '''Enter'''. | + | |Switch back to the '''terminal '''and type: '''perl scope hyphen my dot pl '''and press '''Enter'''. |
|- | |- | ||
|03:49 | |03:49 | ||
− | |The output is displayed as | + | |The output is displayed as: |
− | + | "Block 1: Raghu" | |
− | + | "Block 2: Other" | |
− | + | "Outside Block: " there is no output. | |
− | + | ||
|- | |- | ||
| 03:59 | | 03:59 | ||
− | |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. |
|- | |- | ||
|04:06 | |04:06 | ||
− | |Now let us change the existing program a little. | + | |Now, let us change the existing program a little. |
|- | |- | ||
| 04:10 | | 04:10 | ||
− | |Let us add ''' my $fname = | + | |Let us add ''' my $fname = within double quotes John semicolon''' outside the blocks, before the last '''print''' statement. |
− | + | Save the changes. | |
− | Save the changes | + | |
|- | |- | ||
Line 174: | Line 178: | ||
|- | |- | ||
| 04:28 | | 04:28 | ||
− | | | + | |Analyze the output that is displayed. |
|- | |- | ||
| 04:32 | | 04:32 | ||
− | | Hope you are able to understand the scope of using'' | + | | Hope you are able to understand the scope of using 'my' variable within a block and outside a block. |
|- | |- | ||
| 04:41 | | 04:41 | ||
− | | Next we will see about '''dynamically scoped variable''' in '''Perl'''. | + | | Next, we will see about '''dynamically scoped variable''' in '''Perl'''. |
|- | |- | ||
|04:47 | |04:47 | ||
− | |'''Local | + | |'''Local''' keyword gives a temporary scope to a '''global''' variable. |
|- | |- | ||
| 04:52 | | 04:52 | ||
− | | The | + | | The variable is visible to any '''function''' called from the original block. |
|- | |- | ||
|04:58 | |04:58 | ||
− | |You can declare a '''local | + | |You can declare a '''local''' variable as: |
− | + | ||
'''local $fValue = 100 semicolon''' | '''local $fValue = 100 semicolon''' | ||
− | + | '''local $fname = within double quotes "Rakesh" semicolon''' | |
− | '''local $ | + | |
|- | |- | ||
|05:13 | |05:13 | ||
− | |Let us understand this using a sample program. | + | |Let us understand this, using a sample program. |
|- | |- | ||
|05:17 | |05:17 | ||
− | |Open the '''terminal''' and type '''gedit scope | + | |Open the '''terminal''' and type: '''gedit scope hyphen local dot pl ampersand''' and press '''Enter'''. |
|- | |- | ||
|05:27 | |05:27 | ||
− | |This will open''' scope | + | |This will open''' scope hyphen local dot pl''' file, in''' gedit'''. |
|- | |- | ||
| 05:33 | | 05:33 | ||
− | | Type the following code as displayed on the screen. | + | | Type the following code as displayed on the screen. Let me explain the code now. |
− | + | ||
− | Let me explain the code now. | + | |
|- | |- | ||
|05:40 | |05:40 | ||
− | |Here, in the first line we have declared a ''' | + | |Here, in the first line, we have declared a variable '''$fname '''and initialized it. |
|- | |- | ||
|05:47 | |05:47 | ||
− | |Inside the ''' | + | |Inside the function '''Welcome()''', we have declared a '''local''' variable by the same name,''' $fname.''' |
|- | |- | ||
| 05:54 | | 05:54 | ||
− | | Notice the '''local | + | | Notice the '''local''' keyword before the variable name |
|- | |- | ||
| 05:59 | | 05:59 | ||
− | | | + | | and we have assigned the value "Rakesh" to this variable. |
|- | |- | ||
| 06:03 | | 06:03 | ||
− | | So, basically, inside function '''Welcome(), $fname''' is modified as a new temporary '''local | + | | So, basically, inside function '''Welcome(), $fname''' is modified as a new temporary '''local''' variable.Then, the function '''Hello() '''is being '''call'''ed. |
− | + | ||
− | Then, the ''' | + | |
|- | |- | ||
|06:15 | |06:15 | ||
− | |Here is the | + | |Here is the function definition of '''Hello()'''. |
|- | |- | ||
|06:18 | |06:18 | ||
− | |At the end of the program, we are calling both the ''' | + | |At the end of the program, we are calling both the functions '''Welcome()''' and '''Hello()'''. |
|- | |- | ||
Line 258: | Line 256: | ||
|- | |- | ||
|06:31 | |06:31 | ||
− | |Switch back to the '''terminal''' and type | + | |Switch back to the '''terminal''' and type: '''perl scope hyphen local.pl''' and press '''Enter'''. |
|- | |- | ||
|06:41 | |06:41 | ||
− | |The output is displayed as | + | |The output is displayed as: |
+ | "Hello, Rakesh"! | ||
+ | "Hello, Welcome to Spoken tutorials!" | ||
|- | |- | ||
Line 270: | Line 270: | ||
|- | |- | ||
|06:51 | |06:51 | ||
− | |When the function '''Welcome()''' is called, the ''' | + | |When the function '''Welcome()''' is called, the function '''Hello()''' within it, accesses the '''local''' variable. |
|- | |- | ||
| 06:59 | | 06:59 | ||
− | |Within '''Welcome(), $fname''' has the value " | + | |Within '''Welcome(), $fname''' has the value "Rakesh". |
|- | |- | ||
| 07:04 | | 07:04 | ||
− | |After this, the ''' | + | |After this, the function '''Hello() '''accesses the variable '''$fname '''once again. |
|- | |- | ||
| 07:11 | | 07:11 | ||
− | |But this time, it is the ''' | + | |But this time, it is the variable '''$fname '''which was initialized to "Welcome to spoken tutorials". |
|- | |- | ||
| 07:19 | | 07:19 | ||
− | |It does not access the ''' | + | |It does not access the local variable '''$fname '''within the function '''Welcome()'''. |
|- | |- | ||
| 07:25 | | 07:25 | ||
− | |Which means that, the | + | |Which means that, the local variable restores the '''scope''', after leaving the block '''Welcome()'''. |
|- | |- | ||
| 07:32 | | 07:32 | ||
− | | Next, we will see about '''global | + | | Next, we will see about '''global''' variables in '''Perl'''. |
|- | |- | ||
Line 301: | Line 301: | ||
|- | |- | ||
|07:43 | |07:43 | ||
− | |'''Global variables '''are declared with'' | + | |'''Global variables '''are declared with 'our' keyword. |
|- | |- | ||
| 07:47 | | 07:47 | ||
− | |Here are some examples. '''our $fvalue = 100 semicolon </nowiki>''' '''our $fname = | + | |Here are some examples. |
+ | '''our $fvalue = 100 semicolon </nowiki>''' | ||
+ | '''our $fname =within double quotes Priya semicolon''' | ||
|- | |- | ||
|08:01 | |08:01 | ||
− | |Now let us look at a working example of '''global | + | |Now, let us look at a working example of '''global''' variables. |
|- | |- | ||
|08:06 | |08:06 | ||
− | |Switch back to the '''terminal '''and type '''gedit scope | + | |Switch back to the '''terminal '''and type: '''gedit scope hyphen our dot pl ampersand''' and press''' Enter'''. |
|- | |- | ||
|08:16 | |08:16 | ||
− | |This will open the file''' scope | + | |This will open the file''' scope hyphen our.pl''' in '''gedit'''. |
|- | |- | ||
Line 325: | Line 327: | ||
|- | |- | ||
|08:27 | |08:27 | ||
− | |I have declared '''package main '''and a | + | |I have declared '''package main '''and a global variable as''' our $i '''and I have initialized it to '''100'''. |
|- | |- | ||
Line 333: | Line 335: | ||
|- | |- | ||
| 08:40 | | 08:40 | ||
− | |A '''package''' is a collection of code | + | |A '''package''' is a collection of code which has its own '''namespace'''. |
|- | |- | ||
Line 345: | Line 347: | ||
|- | |- | ||
|08:56 | |08:56 | ||
− | |Within '''package First,''' the | + | |Within '''package First,''' the global variable "i" holds the value 10. |
|- | |- | ||
|09:02 | |09:02 | ||
− | |In '''package Second,''' the | + | |In '''package Second,''' the global variable "i" is assigned the value 20. |
|- | |- | ||
|09:08 | |09:08 | ||
− | |The | + | |The '''main package '''uses both '''package First variable '''and the '''package Second variable'''. |
|- | |- | ||
| 09:15 | | 09:15 | ||
− | |In my program, I have declared the same variable | + | |In my program, I have declared the same variable "i" in all the '''packages'''. |
|- | |- | ||
| 09:21 | | 09:21 | ||
− | |The '''package variable''' is referred by '''package name | + | |The '''package variable''' is referred by '''package name colon colon variable name'''. |
|- | |- | ||
| 09:29 | | 09:29 | ||
− | |In our example it is''' $First | + | |In our example, it is''' $First colon colon i, $Second colon colon i'''. |
|- | |- | ||
| 09:39 | | 09:39 | ||
− | |We have multiple '''packages '''within one file | + | |We have multiple '''packages '''within one file and the global variable will be accessed by all the '''packages'''. |
|- | |- | ||
Line 377: | Line 379: | ||
|- | |- | ||
|09:51 | |09:51 | ||
− | |So, switch to '''terminal '''and type | + | |So, switch to '''terminal '''and type: '''perl scope hyphen our dot pl''' and press '''Enter'''. |
|- | |- | ||
Line 389: | Line 391: | ||
|- | |- | ||
|10:11 | |10:11 | ||
− | |This brings us to the end of this tutorial. Let us | + | |This brings us to the end of this tutorial. Let us summarize. |
|- | |- | ||
| 10:16 | | 10:16 | ||
|In this tutorial, we learnt: | |In this tutorial, we learnt: | ||
− | + | scope of variables | |
− | + | declaration of '''private variables''' | |
− | + | '''dynamically scoped variables''' and | |
− | + | '''global variables''' with examples.<br/> | |
− | + | ||
|- | |- | ||
| 10:29 | | 10:29 | ||
− | |It is preferred to use | + | |It is preferred to use 'my' than '''local''', as the compilation is faster. |
|- | |- | ||
Line 414: | Line 415: | ||
|- | |- | ||
| 10:42 | | 10:42 | ||
− | | Declare a package as '''FirstModule''' | + | | Declare a '''package''' as '''FirstModule'''. |
|- | |- | ||
| 10:46 | | 10:46 | ||
− | | Declare a variable '''$age''' as '''our''' and assign the value '''42''' | + | | Declare a variable '''$age''' as '''our''' and assign the value '''42'''. |
|- | |- | ||
| 10:52 | | 10:52 | ||
− | |Declare another package as '''SecondModule''' | + | |Declare another package as '''SecondModule'''. |
|- | |- | ||
| 10:56 | | 10:56 | ||
− | |Declare a variable '''$ageword''' as '''our''' and assign the value within double quotes | + | |Declare a variable '''$ageword''' as '''our''' and assign the value within double quotes "Forty-Two". |
|- | |- | ||
| 11:05 | | 11:05 | ||
− | | Declare a subroutine''' First()''' | + | | Declare a subroutine''' First()'''. |
|- | |- | ||
Line 438: | Line 439: | ||
|- | |- | ||
| 11:16 | | 11:16 | ||
− | |''local''' '''$age = 52''' semicolon | + | |'''local''' '''$age = 52''' semicolon |
|- | |- | ||
| 11:20 | | 11:20 | ||
− | |'''my''' '''$ageword''' = within double quotes '''Fifty-two | + | |'''my''' '''$ageword''' = within double quotes '''Fifty-two semicolon ''' |
|- | |- | ||
| 11:27 | | 11:27 | ||
− | |Call another subroutine as '''Result()''' | + | |Call another subroutine as '''Result()'''. |
|- | |- | ||
Line 454: | Line 455: | ||
|- | |- | ||
| 11:37 | | 11:37 | ||
− | |End the subroutine | + | |End the subroutine. |
|- | |- | ||
| 11:39 | | 11:39 | ||
− | | Declare the subroutine '''Result()''' | + | | Declare the subroutine '''Result()'''. |
|- | |- | ||
| 11:42 | | 11:42 | ||
− | | Again print the values of '''$age''' and '''$ageword''' | + | | Again print the values of '''$age''' and '''$ageword'''. |
|- | |- | ||
| 11:47 | | 11:47 | ||
− | | End the subroutine | + | | End the subroutine. |
|- | |- | ||
| 11:49 | | 11:49 | ||
− | | Call the function '''First()''' | + | | Call the function '''First()'''. |
|- | |- | ||
Line 478: | Line 479: | ||
|- | |- | ||
|11:57 | |11:57 | ||
− | |The video at the following link | + | |The video at the following link summarizes the '''Spoken Tutorial''' project. |
− | + | ||
Please download and watch it. | Please download and watch it. | ||
|- | |- | ||
|12:05 | |12:05 | ||
− | |The Spoken Tutorial | + | |The '''Spoken Tutorial''' project team conducts workshops and gives certificates for those who pass an online test. |
− | + | ||
For more details, please write to us. | For more details, please write to us. | ||
Line 495: | Line 494: | ||
|- | |- | ||
|12:31 | |12:31 | ||
− | |This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching. | + | |This is Nirmala Venkat from '''IIT Bombay''', signing off. Thanks for watching. |
|} | |} |
Latest revision as of 18:12, 20 February 2017
|
|
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. |