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
Object-oriented programming (OOP) is a paradigm that uses "objects" (which can contain both data and code) to design and implement software. Perl, traditionally procedural in nature, offers robust support for OOP, even if it approaches it a bit differently than some other languages.
Here's an introduction to OOP in Perl:
In Perl, packages are used to create separate namespaces. They serve as the foundation for creating classes in OOP.
A package is defined with the package
keyword:
package MyClass;
An object is an instance of a class. In Perl, objects are usually implemented as references (often references to hashes) that "know" to which class they belong.
A constructor is a subroutine in a class that returns a new object. The most common name for a constructor in Perl is new
.
package MyClass; sub new { my $class = shift; my $self = {}; # It's an empty hash reference bless $self, $class; # This makes the hash an object of 'MyClass' return $self; }
Methods are subroutines defined in the package that operate on objects.
sub set_name { my $self = shift; $self->{name} = shift; } sub get_name { my $self = shift; return $self->{name}; }
Perl supports single inheritance via the @ISA
array. If a method is not found in a class, Perl searches for it in the classes listed in the @ISA
array of the original class.
package MyDerivedClass; our @ISA = ("MyClass");
This means MyDerivedClass
inherits from MyClass
.
Given the class definitions above, here's how you'd use them:
use MyClass; my $obj = MyClass->new(); # Create a new object $obj->set_name("Alice"); print $obj->get_name(); # Outputs "Alice"
OOP in Perl is flexible, allowing a blend of procedural and object-oriented styles. While it may seem a bit unconventional when compared to stricter OOP languages like Java or C++, it provides the power and flexibility Perl is known for. If you're transitioning from OOP in other languages, it might take a moment to adapt, but the underlying principles remain consistent.
Perl OOP basics and concepts:
# Perl class definition package Animal; sub new { my ($class, $name) = @_; my $self = { name => $name }; bless $self, $class; return $self; } sub speak { die "Subclass must implement speak method"; } 1; # Package must return a true value
Defining classes and objects in Perl:
# Creating objects from the Animal class use Animal; my $cat = Animal->new("Whiskers"); my $dog = Animal->new("Buddy");
Perl inheritance and polymorphism:
# Inheriting from Animal class package Dog; use parent -norequire, 'Animal'; sub speak { my ($self) = @_; return $self->{name} . " says Woof!"; } 1;
Encapsulation in Perl OOP:
# Encapsulation in Animal class sub get_name { my ($self) = @_; return $self->{name}; } # Accessing name property my $cat_name = $cat->get_name();
Perl constructor and destructor methods:
# Constructor and destructor in Animal class sub new { my ($class, $name) = @_; my $self = { name => $name }; bless $self, $class; return $self; } sub DESTROY { my ($self) = @_; print "Object for $self->{name} is being destroyed.\n"; }
Perl OOP encapsulation and data hiding:
# Private property in Animal class my $secret_data = "This is hidden"; # Private method sub _private_method { # Implementation details... }
Overloading in Perl object-oriented programming:
# Overloading in Animal class sub speak { my ($self, $volume) = @_; if ($volume) { return $self->{name} . " says Woof loudly!"; } else { return $self->{name} . " says Woof!"; } }
Perl inheritance examples and usage:
# Inheriting from Animal class package Cat; use parent -norequire, 'Animal'; sub speak { my ($self) = @_; return $self->{name} . " says Meow!"; } 1;
Introduction to Moose for OOP in Perl:
use Moose; # Moose class definition class Dog { has 'name' => (is => 'ro', isa => 'Str', required => 1); has 'breed' => (is => 'ro', isa => 'Str'); has 'age' => (is => 'rw', isa => 'Int'); method bark { return $self->name . " says Woof!"; } }