Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Encapsulation is one of the core principles of Object-Oriented Programming (OOP). It involves bundling data (attributes) and methods (functions) into a single unit (class) and restricting direct access to some of the object's components, ensuring that the internal representation of the object is hidden from outside.
Perl is a multi-paradigm language, and it supports OOP, albeit in a more flexible manner than languages like Java or C++. Encapsulation in Perl can be achieved using packages and blessed references.
Here's a step-by-step guide to implementing encapsulation in Perl:
In Perl, a class is essentially a package. Let's create a simple class Person
.
package Person; sub new { my $class = shift; my %args = @_; my $self = { _name => $args{name}, _age => $args{age}, }; bless $self, $class; return $self; } 1; # Always end a module with a true value
Notice the _
(underscore) prefix for the attributes _name
and _age
. It's a convention in Perl to denote private attributes/methods. Perl doesn't have true private attributes like other OOP languages. The underscore is a way to tell programmers, "Hey, don't access this directly!"
To implement encapsulation, provide public methods to access or modify private attributes. This way, you can control and validate the data that's set or retrieved.
# Getter for name sub get_name { my $self = shift; return $self->{_name}; } # Setter for name sub set_name { my $self = shift; my $new_name = shift; # Add validation if needed $self->{_name} = $new_name; } # Similarly, you can have getters and setters for _age
You can make methods private by not providing them outside of the package or by using a convention like prefixing them with an underscore.
# A private method sub _private_method { # ... code here ... }
To use the encapsulated class:
use Person; my $person = Person->new(name => 'John', age => 30); # Access name using getter print $person->get_name(); # Outputs: John # Modify name using setter $person->set_name('Doe'); print $person->get_name(); # Outputs: Doe
As with many aspects of Perl, encapsulation relies a lot on the discipline of the programmer. Perl gives you the tools to encapsulate, but it also trusts you with the freedom to decide how and when to use them.
Perl object-oriented programming encapsulation:
package MyClass; sub new { my $class = shift; my $self = { _attribute => shift, }; bless $self, $class; return $self; } sub get_attribute { my ($self) = @_; return $self->{_attribute}; } sub set_attribute { my ($self, $value) = @_; $self->{_attribute} = $value; } 1;
Private methods and attributes in Perl:
package MyClass; sub new { my $class = shift; my $self = { _attribute => shift, }; bless $self, $class; return $self; } sub _private_method { # private method implementation } 1;
Perl encapsulation example:
package MyClass; sub new { my $class = shift; my $self = { _attribute => shift, }; bless $self, $class; return $self; } sub get_attribute { my ($self) = @_; return $self->{_attribute}; } sub set_attribute { my ($self, $value) = @_; $self->{_attribute} = $value; } 1;
Perl Moose encapsulation:
package MyClass; use Moose; has '_attribute' => ( is => 'rw', isa => 'Str', ); 1;
Information hiding in Perl:
package MyClass; sub new { my $class = shift; my $self = { _attribute => shift, }; bless $self, $class; return $self; } sub get_attribute { my ($self) = @_; return $self->{_attribute}; } sub set_attribute { my ($self, $value) = @_; $self->{_attribute} = $value; } 1;
Perl accessors and mutators:
package MyClass; sub new { my $class = shift; my $self = { _attribute => shift, }; bless $self, $class; return $self; } sub get_attribute { my ($self) = @_; return $self->{_attribute}; } sub set_attribute { my ($self, $value) = @_; $self->{_attribute} = $value; } 1;
Encapsulation vs. abstraction in Perl:
# Encapsulation example (previous examples) # Abstraction example: abstract class with method signatures package Shape; sub area { die "Subclasses must implement area method"; } 1;
Perl encapsulation in object-oriented design:
package MyClass; sub new { my $class = shift; my $self = { _attribute => shift, }; bless $self, $class; return $self; } sub get_attribute { my ($self) = @_; return $self->{_attribute}; } sub set_attribute { my ($self, $value) = @_; $self->{_attribute} = $value; } 1;