Perl | Scope of Variables

12 Feb, 2019
The scope of a variable is the part of the program where the variable is accessible. A scope is also termed as the visibility of the variables in a program. In Perl, we can declare either Global variables or Private variables. Private variables are also known as lexical variables.
Global variables can be used inside any function or any block created within the program. It is visible in the whole program. Global variables can directly use and are accessible from every part of the program.
Example 1: The variable $name is declared at the beginning of the code. It will be visible till the end of the file everywhere. Even inside blocks. Even if those are in the function declarations. If we change the variable inside the block, that will change the value for the rest of the code. Even outside of the block.
# Perl program to illustrate the # Scope of Global variables  # declaration of global variable $name = "GFG";  # printing global variable print "$name\n";      # global variable can be used # inside a block, hence the we # are taking a block in which# we will print the value of # $name i.e. global variable{      # here GFG will print    print "$name\n";           # values in global variable can be    # changed even within a block,     # hence the value of $name is     # now changed to "GeeksforGeeks"    $name = "GeeksforGeeks";           # print function prints    # "GeeksforGeeks"    print "$name\n"; }  # changes made inside the above block'# are reflected in the whole program # so here GeeksforGeeks will printprint "$name\n"; 
GFG
GFG
GeeksforGeeks
GeeksforGeeks

Example 2:
# Perl program to illustrate the # Scope of Global variables  # declaration of global variables$name = "GFG"; $count = 1;  # printing global variablesprint $count." ".$name."\n";$count++;  # Block starting{          # global variable can be used inside    # a block, so below statement will     # print GFG and 1    print $count." ".$name."\n";          # incrementing the value of     # count inside the block    $count++;}  # taking a functionsub func {          # Global variable, $count and $name,    # are accessible within function    print $count." ".$name."\n";}  # calling the functionfunc();
1 GFG
2 GFG
3 GFG

Private variables in Perl are defined using my keyword before a variable. my keyword confines variables in a function or block in which it is declared. A block can either be a for loop, while loop or a block of code with curly braces around it. The local variable scope is local, its existence lies between those two curly braces(block of code), outside of that block this variable doesn’t exist. These variables are also known as lexical variables.
Note: When private variables are used within a function or block, then they hide the global variables created with the same name. When we call a subroutine with a private variable, it can be used within that function. As soon as the subroutine exits, the private variables can no longer be used.
Example:
# Perl program to illustrate the # scope of private variables  # declaration of global variable $name = "Global"; $count = 1;  # printing global variablesprint $count." ".$name."\n";  # incrementing the value of count# i.e it become 2$count++;  # block starting{          # declaring private variable by using my     # keyword which can only be used    # within this block    my $new_name = "Private";           # global variables are     # accessible inside block    print $count." ".$name."\n";          # incrementing the value    # of global variable    # here it become 3    $count++;          print $name." and ".$new_name."\n";}  # $new_name variable cannot # be used outside, hence nothing # is going to print print "Variable defined in above block: ".$new_name."\n";   # declaring functionsub func {      # this private variable declaration    # hides the global variable which define    # in the beginning of program    my $name = "Hide";     print $count." ".$name."\n";  }  # calling the functionfunc();
1 Global
2 Global
Global and Private
Variable defined in above block: 
3 Hide

In Perl, we have one more type of scoping called Package Scoping. This is used when we need to make variables which can be used exclusively in different namespaces.”main” is the default namespace in every Perl program. Namespaces in Perl are defined using the package keyword.
Example:
# Perl program to illustrate # the Package Variables  # variable declared in # main namespace$var1 = "Main Namespace";  print "Value of Var1: ".$var1."\n";   # package declaration# Pack1 is the package package Pack1;       # since $var1 belongs to main namespace,     # so nothing will print inside Pack1    # namespace    print "Value of var1: ".$var1."\n";            # variable declared in Pack1 namespace    # having same name as main namespace    $var1 = "Pack1 Namespace";          # here $var1 belongs to Pack1 namespace    print "Value of var1: ".$var1."\n";           # in-order to print variables     # from both namespace, use     # following method    print "Value of var1: ".$main::var1."\n";    print "Value of var1: ".$Pack1::var1."\n";
Value of Var1: Main Namespace
Value of var1: 
Value of var1: Pack1 Namespace
Value of var1: Main Namespace
Value of var1: Pack1 Namespace

Our Keyword in Perl: “our” keyword only creates an alias to an existing package variable of the same name. our keyword allows to use a package variable without qualifying it with the package name, but only within the lexical scope of the “our” declaration. A variable declared with our keyword declares an alias for a package variable that will be visible across its entire lexical scope, even across package boundaries.
# Perl program to illustrate the use # of our keyword  # Pack1 namespace declared# by using the package keywordpackage Pack1;       # declaring $Pack1::first_name     # for rest of lexical scope    our $first_name;        $first_name = "Shashank";           # declaring $Pack1::second_name for    # only this namespace    $second_name;        $second_name = "Sharma";   # Pack2 namespace declaredpackage Pack2;       # prints value of $first_name, as it     # refers to $Pack1::first_name    print "first_name = ".$first_name."\n";           # It will print nothing as $second_name    # does not exist in Pack2 package scope    print "second_name = ".$second_name."\n";  
first_name = Shashank
second_name =

perl-data-types
Perl
Perl
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Perl | Arrays (push, pop, shift, unshift)
Perl | Arrays
Perl Tutorial - Learn Perl With Examples
Perl | Polymorphism in OOPs
Perl | Boolean Values
Perl | Subroutines or Functions
Use of print() and say() in Perl
Perl | Basic Syntax of a Perl Program
Hello World Program in Perl
Introduction to Perl