WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Perl · Intermediate · question 24 of 100

What are Perl’s object-oriented programming (OOP) features, and how do you create and use objects in Perl?

📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

Perl has a comprehensive set of object-oriented programming (OOP) features. The language has native support for defining classes, objects, constructors, methods, inheritance, polymorphism, and encapsulation.

To create an object in Perl, you first define a class. A class is a blueprint for creating objects. It defines the attributes (data) and methods (functions) of an object. Here’s an example class definition:

package Person;

sub new {
  my $class = shift;
  my $self = {
    _firstName => shift,
    _lastName  => shift,
    _age       => shift,
  };
  bless $self, $class;
  return $self;
}

sub getFirstName {
  my ($self) = @_;
  return $self->{_firstName};
}

sub setFirstName {
  my ($self, $firstName) = @_;
  $self->{_firstName} = $firstName if defined($firstName);
  return $self->{_firstName};
}

sub getLastName {
  my ($self) = @_;
  return $self->{_lastName};
}

sub setLastName {
  my ($self, $lastName) = @_;
  $self->{_lastName} = $lastName if defined($lastName);
  return $self->{_lastName};
}

sub getAge {
  my ($self) = @_;
  return $self->{_age};
}

sub setAge {
  my ($self, $age) = @_;
  $self->{_age} = $age if defined($age);
  return $self->{_age};
}

1;

The ‘Person‘ class has three attributes (‘_firstName‘, ‘_lastName‘, and ‘_age‘) and six methods (‘new‘, ‘getFirstName‘, ‘setFirstName‘, ‘getLastName‘, ‘setLastName‘, and ‘getAge‘, and ‘setAge‘).

The ‘new‘ method is the constructor. When you call ‘Person->new‘ with arguments, it creates a new object and initializes its attributes.

Here’s an example of how to create and use a ‘Person‘ object:

use Person;

# create a new Person object
my $person = Person->new('John', 'Doe', 30);

# get and set its attributes
$person->setAge(31);
print $person->getFirstName();  # prints 'John'
print $person->getLastName();   # prints 'Doe'
print $person->getAge();        # prints 31

Inheritance is achieved in Perl by using the ‘@ISA‘ array, which specifies the parent classes of a child class. When a method is called on a child object, Perl searches for the method in the child class first, then in its parent classes in the order specified in ‘@ISA‘.

Here’s an example of a child class ‘Employee‘ that inherits from ‘Person‘:

package Employee;

@ISA = qw(Person);

sub new {
  my ($class, $firstName, $lastName, $age, $salary) = @_;
  my $self = $class->SUPER::new($firstName, $lastName, $age);
  $self->{_salary} = $salary;
  bless $self, $class;
  return $self;
}

sub getSalary {
  my ($self) = @_;
  return $self->{_salary};
}

sub setSalary {
  my ($self, $salary) = @_;
  $self->{_salary} = $salary if defined($salary);
  return $self->{_salary};
}

1;

The ‘Employee‘ class inherits from ‘Person‘ by including ‘Person‘ in its ‘@ISA‘ array. The ‘new‘ constructor calls the parent constructor using ‘$class->SUPER::new()‘ and then initializes its own attributes.

Here’s an example of how to create and use an ‘Employee‘ object:

use Employee;

# create a new Employee object
my $employee = Employee->new('John', 'Doe', 30, 50000);

# get and set its attributes
$employee->setAge(31);
$employee->setSalary(55000);
print $employee->getFirstName();  # prints 'John'
print $employee->getLastName();   # prints 'Doe'
print $employee->getAge();        # prints 31
print $employee->getSalary();     # prints 55000

Polymorphism is achieved in Perl by creating subroutines with the same name in different classes. When a method with a certain name is called on an object, Perl searches for the method in the object’s class first, then in its parent classes, and finally in imported packages.

Encapsulation is achieved in Perl by convention rather than by language enforcement. By convention, attributes that are intended to be private are prefixed with an underscore ‘_‘ and are not accessed directly by external code. Instead, accessor methods are provided for getting and setting the attribute values, which can include validation or other processing logic.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Perl interview — then scores it.
📞 Practice Perl — free 15 min
📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

All 100 Perl questions · All topics