When we talk about local and global variables, we are in fact talking about scope of variable.
Local variable
Local variable scope is local, its existence lies between those two curly braces( generally referred as block of code), outside of that block this variable doesn’t exist. These variables also known as lexical variables.
These variables can be used in any block of code, it can be in any control structure block like if, if-else, etc or any loop block like for, while, do-while etc. or any subroutine’s block , it can even be present in an anonymous block. For example –
#!/usr/bin/perl use strict; use warnings; if (1<2) { my $age = 29; print "$age\n"; # prints 29 }
I know that this example doesn’t make any sense but my point here is to show you, how a local variable looks in a program. In the above program variable $age is declared inside if block, thus this variable is local to this block only. If we try to access this variable outside of if body, we would get an error. Let’s see this in action, run the following program.
#!/usr/bin/perl use strict; use warnings; if (1<2) { my $age = 29; print "$age\n"; # prints 29 } print "$age\n";
Output:
Global symbol "$age" requires explicit package name at demo.pl line 9. Execution of demo.pl aborted due to compilation errors.
Note:
1) Local variables are declared using my keyword as you could see in the above program.
2) Since the scope of local variables are limited to the block, you can use same name local variables in different blocks without any conflicts.
3) The variable declared using my keyword, at the beginning of the programs right after pragmas would be accessible throughout the program. For example, the variable $age in this program is accessible though out the program.
#!/usr/bin/perl use strict; use warnings; my $age =29; if (1<2) { print "$age\n"; } print "$age\n";
Output:
29 29
Few important things regarding my keyword:
Till now, we have learnt that my keyword is used to declare local variables, lets see few important point regarding my keyword.
1) When declaring multiple variables using my keyword, you must use parentheses, otherwise it would declare only one local variable. For example,
my $num1, $num2; # This would not declare $num2. my ($num1, $num2); # This is the correct way. It declares both
2) Apart from variables, you can also declare local arrays and local hashes using my keyword.
For example, this code declares local array @friends.
my @friends;
Global variables:
This doesn’t need any introduction as we are using them in our programs since when we started the perl tutorial. All the variables that are directly used without the declaration(using my keyword) are accessible from every part of the program. For example,
#!/usr/bin/perl use warnings; if (1<2) { $age =29; #no declaration using my keyword print "$age\n"; } print "$age\n"; #accessible outside the block
Output:
29 29
Note: In this program, we have removed the use strict pragma as it forces us to declare all the variables using my keyword before using them.
Leave a Reply