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

Classes in Perl

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.

1. Defining a Class:

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;

2. Explanation:

  • 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.

3. Using the 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";

4. Inheritance:

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

5. Overriding Methods:

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.

6. Encapsulation:

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.

Summary:

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.

  1. Defining classes in Perl:

    • Description: Create a basic class in Perl.
    • Code:
      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
      
  2. Constructors and destructors in Perl classes:

    • Description: Implement constructors and destructors in a Perl class.
    • Code:
      sub new {
          my $class = shift;
          my $self = { @_ };
          bless $self, $class;
          return $self;
      }
      
      sub DESTROY {
          my $self = shift;
          # Destructor code
      }
      
  3. Inheritance in Perl object-oriented programming:

    • Description: Create a subclass inheriting from a superclass.
    • Code:
      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
      
  4. Encapsulation in Perl classes:

    • Description: Encapsulate data within a Perl class.
    • Code:
      sub set_name {
          my ($self, $name) = @_;
          $self->{name} = $name;
      }
      
  5. Polymorphism in Perl classes:

    • Description: Achieve polymorphism through method overriding.
    • Code:
      sub greet {
          my $self = shift;
          return "Hello, I am a Person.";
      }
      
  6. Method overriding in Perl:

    • Description: Override methods in a subclass.
    • Code:
      sub greet {
          my $self = shift;
          return "Hello, I am an Employee.";
      }
      
  7. Perl class attributes and methods:

    • Description: Define attributes and methods in a Perl class.
    • Code:
      sub get_employee_id {
          my $self = shift;
          return $self->{employee_id};
      }
      
  8. Perl Moose for modern class construction:

    • Description: Use Moose for modern and concise class construction.
    • Code:
      package Person;
      use Moose;
      
      has 'name' => (is => 'rw');
      
      sub greet {
          my $self = shift;
          return "Hello, my name is " . $self->name;
      }
      
      __PACKAGE__->meta->make_immutable;