Perl | Lexical Binding and Dynamic Binding

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Perl program to demonstrate 
# lexical binding
  
{
    # begin lexical scope
    my $var = "GeeksforGeeks";
    print "\$var is: ", (defined $var
                                 $var : "Geeks"), $/;
  
} # end lexical scope
  
# Executing outside lexical scope
print "\$var is: ", (defined $var
                             $var : "Geeks"), $/;

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Perl program to demonstrate 
# Dynamic binding
package Animal;
  
sub new 
{
    my $class = shift;
      
    # Bless into the correct class
    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); # cat is a subclass of Animal
  
sub method2
{
    my $this = shift;
    print("\nThis is cat class Method 1");
}
  
package main;
  
# Calls Animal::new but creates a 'cat' object
my $cat = new cat; 
  
# Calls Animal::new but creates a 'dog' object
my $dog = new Animal; 
  
$dog->method1(); # Calls Animal::method1
$cat->method2(); # Calls cat::method2

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.