Perl | Scope of Variables
Last Updated :
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.
Scope of Global 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
# 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 print
print "$name\n";
Output:
GFG
GFG
GeeksforGeeks
GeeksforGeeks
Example 2:
Perl
# Perl program to illustrate the
# Scope of Global variables
# declaration of global variables
$name = "GFG";
$count = 1;
# printing global variables
print $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 function
sub func {
# Global variable, $count and $name,
# are accessible within function
print $count." ".$name."\n";
}
# calling the function
func();
Output:
1 GFG
2 GFG
3 GFG
Scope of Lexical Variables(Private Variables)
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
# Perl program to illustrate the
# scope of private variables
# declaration of global variable
$name = "Global";
$count = 1;
# printing global variables
print $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 function
sub 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 function
func();
Output:
1 Global
2 Global
Global and Private
Variable defined in above block:
3 Hide
Package Variables
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
# 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";
Output:
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
# Perl program to illustrate the use
# of our keyword
# Pack1 namespace declared
# by using the package keyword
package 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 declared
package 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";
Output:
first_name = Shashank
second_name =
Similar Reads
Special variables in Perl
Special Variables in Perl are those which are already defined to carry out a specific function when required. The differentiating factor between a special Variable in Perl and a random character is the use of Punctuation mark after the variable, these could be @, $ or %, etc, for example, $_. Perl V
7 min read
Perl | Variables
Variables in Perl are used to store and manipulate data throughout the program. When a variable is created it occupies memory space. The data type of a variable helps the interpreter to allocate memory and decide what to be stored in the reserved memory. Therefore, variables can store integers, deci
4 min read
Perl | Scope of a Subroutine
Subroutines in Perl, are reusable unit of code. It is a dynamic concept. Functions and subroutines are the two terms that can be used interchangeably. If you want to be strict on the semantics, small pieces of named blocks of code that accept arguments and return values are called subroutines. The b
7 min read
C# Variables
In C#, variables are containers used to store data values during program execution. So basically, a Variable is a placeholder of the information which can be changed at runtime. And variables allows to Retrieve and Manipulate the stored information. In Brief Defination: When a user enters a new valu
4 min read
Perl | Scalars
A scalar is a variable that stores a single unit of data at a time. The data that will be stored by the scalar variable can be of the different type like string, character, floating point, a large group of strings or it can be a webpage and so on.Example : Perl # Perl program to demonstrate # scalar
2 min read
C# Properties
Properties are the special types of class members that provide a flexible mechanism to read, write, or compute the value of a private field. Properties function like public data members but are accessors which makes easy data access. Properties also support encapsulation and abstraction through "get
4 min read
Can Global Variables be dangerous ?
In a small code, we can track values of global variables. But if the code size grows, they make code less understandable (hence less maintainable). It becomes difficult to track which function modified the value and how. C++ // A C++ program to demonstrate that a // global variables make long code l
2 min read
Problem Solving on Storage Classes and Scoping of Variables
Storage class of variables includes the scope, visibility and life-time which help to trace the existence of a particular variable during the runtime of a program. There exist four types of storage classes in C: auto, register, static and extern. Auto and Register Storage Classes - The scope of vari
8 min read
Global and Local Variables in Python
Python Global variables are those which are not defined inside any function and have a global scope whereas Python local variables are those which are defined inside a function and their scope is limited to that function only. In other words, we can say that local variables are accessible only insid
7 min read
Use of print() and say() in Perl
Perl uses statements and expressions to evaluate the input provided by the user or given as Hardcoded Input in the code. This evaluated expression will not be shown to the programmer as it's been evaluated in the compiler. To display this evaluated expression, Perl uses print() function and say() fu
3 min read