Perl | Lexical Binding and Dynamic Binding
Last Updated :
31 Dec, 2019
Lexical binding and Dynamic binding in Perl, is a system by which value and scope of variables are defined and it makes the variable easy to find by its respective name. Perl5 supports both Lexical and Dynamic Binding
Lexical Binding: It is also referred to as static binding. A binding scope is like syntax, functions, etc that makes whole new table values, and names of variables are classified in ranking order which is referred to as the environment. This binding was introduced in Emacs. This created many opportunities for maximum utilization, so the programs which use it have an advantageous feature for running faster in the future versions of Emacs. They are more reconcilable with concurrency that was later added to the 26.1 version of Emacs. Lexical scope means that the reference made to a variable bound lexically should be located in the construct of the binding.
Working of lexical binding: Lexical environment is best defined by a binding construct, this specifies the local values of the variables that are bounded in the construct. The lexical environment is the first place where the specific value of the variable is looked upon by the Lisp evaluator. The scope of lexical binding is indefinite. It means even after the execution of the lexical environment is over, it will not be discarded and can be used again whenever it is called.
Example:
{
my $var = "GeeksforGeeks" ;
print "\$var is: " , ( defined $var ?
$var : "Geeks" ), $/;
}
print "\$var is: " , ( defined $var ?
$var : "Geeks" ), $/;
|
Output:
$var is: GeeksforGeeks
$var is: Geeks
In the above example code, the scope of variable $var is defined for that particular body of code and gets changed when called outside. This shows that once the lexical scope has been exited then anything created in this lexical scope will be deleted.
Dynamic binding: This is also referred to as late binding an alternative option to the lexical or static binding. This binding is a system by which the program waits for the runtime to finish and then binds the method by the name which is referred to as an actual subroutine. This binding is costlier but at the same time, it has its own benefits like avoiding conflicts between versions. Java and C++ are common languages that can perform dynamic binding. The concept which is usually used in dynamic binding is method overriding.
Why to use Dynamic Binding?
- Easier implementation. The whole concept was understood by the mass way before lexical binding.
- It was more sensible to other people and easy to understand.
Example:
package Animal;
sub new
{
my $class = shift ;
bless {}, $class ;
}
sub method1
{
my $this = shift ;
print ( "This is Animal class Method 1" );
}
sub method2
{
my $this = shift ;
print ( "\nThis is Animal class Method 2" );
}
package cat;
our @ISA = (Animal);
sub method2
{
my $this = shift ;
print ( "\nThis is cat class Method 1" );
}
package main;
my $cat = new cat;
my $dog = new Animal;
$dog ->method1();
$cat ->method2();
|
Output:
This is Animal class Method 1
This is cat class Method 1
In the above example code, Perl uses the concept of Method Overriding to perform the operation of Dynamic binding. It shows that how Dynamic Binding allows accessing the content of the class outside of its scope.
Share your thoughts in the comments
Please Login to comment...