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
In Perl, object-oriented programming (OOP) is achieved through the use of packages (often used synonymously with "modules") and references. By using packages to define classes and methods, and references to create instances (objects), Perl developers can harness the power of OOP.
This tutorial will introduce you to objects in Perl, including how to define a class, create objects, and invoke methods.
In Perl, a class is usually represented by a package. Let's create a simple Person
class:
package Person; # Constructor sub new { my $class = shift; my $self = { _name => shift, _age => shift, }; bless $self, $class; return $self; } # Method to set name sub setName { my ($self, $name) = @_; $self->{_name} = $name; } # Method to get name sub getName { my $self = shift; return $self->{_name}; } 1; # end of Person class
new
method is the constructor. It initializes the object with the given parameters.bless
associates an object with a class.$self
).To use our Person
class and create a new object:
use Person; my $person = Person->new("John", 30);
Here, we're creating an instance of Person
with the name "John" and age "30".
You can invoke an object's methods using the arrow (->
) operator:
$person->setName("Doe"); print $person->getName(); # Outputs "Doe"
Perl supports inheritance, allowing a class to inherit attributes and methods from another class:
package Employee; use base 'Person'; sub new { my ($class, $name, $age, $position) = @_; my $self = $class->SUPER::new($name, $age); $self->{_position} = $position; bless $self, $class; return $self; } sub setPosition { my ($self, $position) = @_; $self->{_position} = $position; } sub getPosition { my $self = shift; return $self->{_position}; } 1;
In this example, Employee
inherits from Person
. The use base
pragma establishes the inheritance relationship.
While Perl doesn't have strict access controls like some languages, a convention is to prefix attributes with an underscore (e.g., _name
) to indicate that they're meant to be private. It's on the developer to respect this convention.
Perl doesn't offer destructors in the same way some other languages do, but it has a special method named DESTROY
. This method is called when an object is garbage collected:
package Person; sub DESTROY { my $self = shift; print "Person $self->{_name} is being destroyed\n"; } 1;
This tutorial provides a basic introduction to object-oriented programming in Perl. With packages to represent classes and the bless
function to create objects, Perl allows developers to harness the principles of OOP like encapsulation, inheritance, and polymorphism. As with many aspects of Perl, while the language offers a lot of flexibility, it relies on developers to adhere to conventions and best practices.
Object-oriented programming in Perl:
# Creating a simple class package Dog; sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } # Creating an object my $dog_object = Dog->new();
Creating and using objects in Perl:
new
constructor and used to invoke class methods.# Using a simple class my $dog_object = Dog->new(); # Accessing methods of the object $dog_object->bark();
Perl classes and objects example:
package Person; sub new { my $class = shift; my $self = { name => shift, age => shift }; bless $self, $class; return $self; } sub get_name { my $self = shift; return $self->{name}; } # Creating and using objects my $person_object = Person->new("John", 30); my $name = $person_object->get_name();
Perl constructor and destructor methods:
new
) initializes objects, and destructor (DESTROY
) is called when an object goes out of scope.package MyClass; sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } sub DESTROY { # Destructor logic } # Creating and using objects my $obj = MyClass->new();
Encapsulation in Perl objects:
package BankAccount; sub new { my $class = shift; my $self = { balance => 0 }; bless $self, $class; return $self; } sub deposit { my ($self, $amount) = @_; $self->{balance} += $amount; } # Using encapsulation my $account = BankAccount->new(); $account->deposit(100);
Inheritance in Perl OOP:
package Shape; sub new { my $class = shift; my $self = { color => "red" }; bless $self, $class; return $self; } package Circle; use base qw(Shape); # Using inheritance my $circle = Circle->new(); print $circle->{color}; # Inherited property
Polymorphism in Perl:
package Animal; sub speak { die "Subclass must implement speak method"; } package Dog; use base qw(Animal); sub speak { print "Woof!\n"; } package Cat; use base qw(Animal); sub speak { print "Meow!\n"; } # Using polymorphism my $dog = Dog->new(); my $cat = Cat->new(); $dog->speak(); $cat->speak();
Perl object methods and properties:
package Car; sub new { my $class = shift; my $self = { speed => 0 }; bless $self, $class; return $self; } sub accelerate { my ($self, $amount) = @_; $self->{speed} += $amount; } # Using object methods and properties my $car = Car->new(); $car->accelerate(50);
Object-oriented design in Perl:
package BankAccount; sub new { my $class = shift; my $self = { balance => 0 }; bless $self, $class; return $self; } sub deposit { my ($self, $amount) = @_; $self->{balance} += $amount; } sub withdraw { my ($self, $amount) = @_; if ($amount <= $self->{balance}) { $self->{balance} -= $amount; } else { die "Insufficient funds"; } } # Object-oriented design in action my $account = BankAccount->new(); $account->deposit(100); $account->withdraw(50);