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.
- Technically in encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of own class in which they are declared.
- As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding.
- Encapsulation can be achieved by: Declaring all the variables in the class as local and fetching methods of the class by importing packages to set and get the values of variables.
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”.
Name is: Rahul Age is: 25 Roll_no is: 12
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:
- Data Hiding: The user will have no idea about the inner implementation of the class. It will not be visible to the user that how the class is stored values in the variables. He only knows that we are passing the values to accessors and variables are getting initialized to that value.
- Increased Flexibility: We can make the variables of the class as read-only or write-only depending on our requirement. If we wish to make the variables as read-only then we have to only use Get Accessor in the code. If we wish to make the variables as write-only then we have to only use Set Accessor.
- Reusability: Encapsulation also improves the re-usability and easy to change with new requirements.
- Testing code is easy: Encapsulated code is easy to test for unit testing.
- Perl | Methods in OOPs
- Perl | Objects in OOPs
- Perl | Polymorphism in OOPs
- Perl | Inheritance in OOPs
- Perl | Method Overriding in OOPs
- Object Oriented Programming (OOPs) in Perl
- Perl Tutorial - Learn Perl With Examples
- Perl | Basic Syntax of a Perl Program
- OOPs | Object Oriented Design
- OOPS | Generalization as extension and restriction using Java
- Perl vs C/C++
- Perl | exp Function
- Perl | eq operator
- Recursion in Perl
- Perl | uc() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.