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) can be achieved using packages and modules to define classes and their associated methods. This tutorial will introduce you to creating and using classes in Perl.
In Perl, a class is typically defined using a package. A package in Perl is a namespace for variables and subroutines.
Here's an example of a basic class definition for a Person
:
package Person; sub new { my $class = shift; my $self = { _name => shift, _age => shift, }; bless $self, $class; return $self; } sub setName { my ($self, $name) = @_; $self->{_name} = $name; } sub getName { my $self = shift; return $self->{_name}; } sub setAge { my ($self, $age) = @_; $self->{_age} = $age; } sub getAge { my $self = shift; return $self->{_age}; } 1;
new: This method is a constructor. It's invoked to create a new object of the class. The bless
function associates the object with the class.
setName/getName and setAge/getAge: These are setter and getter methods, respectively, for the name
and age
attributes of the Person
class.
To use the class, you typically create an object of the class and then call its methods. Here's how you can use the Person
class:
use strict; use warnings; use Person; my $person = Person->new("Alice", 25); print $person->getName() . " is " . $person->getAge() . " years old.\n"; $person->setName("Bob"); $person->setAge(30); print $person->getName() . " is " . $person->getAge() . " years old.\n";
Perl supports class inheritance. If you want one class to inherit from another, you can use the @ISA
array.
For example, if you have a Student
class that should inherit from Person
:
package Student; use Person; our @ISA = qw(Person); # Student inherits from Person
In the derived class (like Student
), you can override methods from the parent class (like Person
) simply by defining a method with the same name.
In the Person
example above, we use _name
and _age
with underscores as a convention to denote them as "private" attributes. However, Perl doesn't enforce strict access restrictions. The underscore is a convention to indicate that those attributes should not be accessed directly outside of the class methods.
Perl provides object-oriented features using packages and modules. Classes are defined as packages with associated attributes and methods. Using the bless
function, you can associate an object with a class. Inheritance is achieved through the @ISA
array, and encapsulation is achieved conventionally using underscore prefixes for "private" attributes. While Perl's OOP isn't as strict or robust as in languages like Java or C++, it's flexible and offers a way to organize code in an object-oriented manner.
Defining classes in Perl:
package Person; sub new { my $class = shift; my $self = { @_ }; bless $self, $class; return $self; } sub get_name { my $self = shift; return $self->{name}; } 1; # End of package
Constructors and destructors in Perl classes:
sub new { my $class = shift; my $self = { @_ }; bless $self, $class; return $self; } sub DESTROY { my $self = shift; # Destructor code }
Inheritance in Perl object-oriented programming:
package Employee; use base 'Person'; sub new { my $class = shift; my $self = $class->SUPER::new(@_); $self->{employee_id} = shift; return $self; } 1; # End of package
Encapsulation in Perl classes:
sub set_name { my ($self, $name) = @_; $self->{name} = $name; }
Polymorphism in Perl classes:
sub greet { my $self = shift; return "Hello, I am a Person."; }
Method overriding in Perl:
sub greet { my $self = shift; return "Hello, I am an Employee."; }
Perl class attributes and methods:
sub get_employee_id { my $self = shift; return $self->{employee_id}; }
Perl Moose for modern class construction:
package Person; use Moose; has 'name' => (is => 'rw'); sub greet { my $self = shift; return "Hello, my name is " . $self->name; } __PACKAGE__->meta->make_immutable;