![]() |
VOOZH | about |
Encapsulation in Perl is the process of wrapping up of data to protect it from the outside sources which need not have access to that part of the code. Encapsulation is a part of the Object-oriented programming, it is used to bind the data and the subroutines that are used to manipulate that data. In a different way, encapsulation is a protective shield that prevents the data from being accessed by the code outside this shield.
👁 Image
Consider a real-life example of encapsulation, in a company, there are different sections like the accounts section, finance section, sales section, etc. The finance section handles all the financial transactions and keeps records of all the data related to finance. Similarly, the sales section handles all the sales related activities and keep records of all the sales. Now there may arise a situation when for some reason an official from the finance section needs all the data about sales in a particular month. In this case, he is not allowed to directly access the data of the sales section. He will first have to contact some other officer in the sales section and then request him to give the particular data. This is what encapsulation is. Here the data of the sales section and the employees that can manipulate them are wrapped under a single name “sales section”. Example:
In the above code, if there is a need to access the data of the class for any modifications or just to print the data of the class then it can not be done directly. There is a need to create an object of that class and then access the data using the get_details() method. This process is termed as Data Encapsulation. Advantages of Encapsulation:
Encapsulation is one of the fundamental principles of object-oriented programming (OOP), which refers to the practice of hiding the implementation details of an object from the outside world and restricting access to that object's internal state. This is done to improve code maintainability, reduce coupling between classes, and prevent unintended modification of object state.
In Perl, encapsulation can be achieved through the use of private and public methods and data members. Private methods and data members are not accessible from outside the class, while public methods and data members can be accessed by any code that has access to the object.
In this example, we define a class Employee with a private method _calculate_bonus and a public method print_details. The new method is used to create an object of the class and initialize its data members. The print_details method prints the employee's name, age, salary, and bonus, which is calculated using the private method. The _calculate_bonus method is not accessible from outside the class and cannot be called directly.
This example demonstrates how encapsulation can be used to hide the implementation details of an object from the outside world, while still allowing access to its public interface. By doing so, we can improve code maintainability, reduce coupling between classes, and prevent unintended modification of object state.