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, methods are essentially subroutines defined within a package (which can be thought of as a class in object-oriented terms). These methods can then be called on instances (objects) of that package. This tutorial will guide you through the basics of defining and using methods in Perl.
In the context of object-oriented programming in Perl, a method is simply a subroutine associated with an object. This means you can call the subroutine on the object and have access to the object's data within that subroutine.
To start, let's create a basic class named Person
with methods to set and get a person's name.
package Person; sub new { my $class = shift; my $self = { _name => undef, }; bless $self, $class; return $self; } sub set_name { my ($self, $name) = @_; $self->{_name} = $name; } sub get_name { my $self = shift; return $self->{_name}; } 1;
Here's what's happening:
The new
subroutine is a constructor. It creates and returns a new object.
set_name
sets the _name
attribute of the object.
get_name
retrieves the _name
attribute of the object.
In the main part of your script, you can create an instance of Person
and use its methods:
use Person; my $person = Person->new(); $person->set_name("Alice"); print $person->get_name(); # Outputs: Alice
Perl supports method inheritance and overriding. Here's a simple example with a Student
class inheriting from Person
and overriding the get_name
method:
package Student; use base 'Person'; sub new { my $class = shift; my $self = $class->SUPER::new(); $self->{_student_id} = undef; bless $self, $class; return $self; } sub set_student_id { my ($self, $id) = @_; $self->{_student_id} = $id; } sub get_student_id { my $self = shift; return $self->{_student_id}; } sub get_name { my $self = shift; return "Student: " . $self->SUPER::get_name(); } 1;
In the Student
class, we've added methods for _student_id
and overridden the get_name
method to prepend "Student: " to the name.
Methods in Perl provide a way to encapsulate behavior within objects. Through inheritance, objects can share behavior (methods) but still have the ability to override and provide specific implementations. This allows for the creation of structured, organized, and reusable code in your Perl applications. As always, Perl's flexibility gives developers many ways to achieve similar outcomes, so it's beneficial to be familiar with best practices and established conventions in the community.
Object-oriented programming in Perl:
package Person; sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } # Usage my $person = Person->new();
Defining methods in Perl classes:
package Calculator; sub add { my ($self, $a, $b) = @_; return $a + $b; } # Usage my $calc = Calculator->new(); my $result = $calc->add(3, 5);
Accessing methods in Perl objects:
->
).my $result = $calc->add(3, 5);
Using subroutines as methods in Perl:
sub multiply { my ($self, $a, $b) = @_; return $a * $b; } my $result = $calc->multiply(2, 4);
Private and public methods in Perl:
_
). Access modifiers are not enforced, so it's based on convention.package MyClass; sub public_method { # Public method } sub _private_method { # Private method }
Polymorphism and methods in Perl:
package Animal; sub speak { print "Animal speaks\n"; } package Dog; use base 'Animal'; sub speak { print "Dog barks\n"; } # Usage my $animal = Animal->new(); my $dog = Dog->new(); $animal->speak(); # Animal speaks $dog->speak(); # Dog barks
Inheritance and methods in Perl:
package Shape; sub draw { print "Drawing a shape\n"; } package Circle; use base 'Shape'; # Usage my $circle = Circle->new(); $circle->draw(); # Calls draw method in Shape
Perl method chaining:
package MyClass; sub new { bless {}, shift } sub method1 { my $self = shift; # Method logic return $self; } sub method2 { my $self = shift; # Method logic return $self; } # Usage my $obj = MyClass->new()->method1()->method2();