Perl – Attributes in Object Oriented Programming
Last Updated :
03 Dec, 2021
In Perl, Object Oriented concept is very much based on references and Arrays/Hashes. The few main terms of object-oriented programming with respect to Perl programming are object, class, and method.
- In Perl, an object is like a reference to a data type that knows about the class it belongs to. The object is stored as a reference present in the scalar variable. And since scalar only contains a reference to the object, it can even hold different objects present in separate classes.
- In Perl, a package containing corresponding methods which are required to create and manipulate objects is known as a class.
- In Perl, the method is a subroutine that is defined within the package. A package name or an object reference is the first argument to the method depending on whether the method affects the current class or object.
Attributes
Attributes can be defined by each class. When we represent them as objects, we assign values to those attributes. For eg, even every ‘file’ object has a path. Attributes are also known as properties.
Attributes are typically defined as read-only or read-write. Read-only attributes can be set only when the object is created, while read-write attributes can be altered at any time. The value of an attribute can itself be another object. And it’s not necessary for every class to have attributes and methods.
Perl has no special syntax for attributes. In the back, attributes are often stored as keys in the object’s underlying hash.
Perl
sub new{
my ( $class , $args ) = @_ ;
my $self = bless { serial => $args ->{serial},
name => $args ->{name},
price => $args ->{price}
}, $class ;
}
|
Whenever we call the new() method, Perl automatically passes the class name as the first argument to the special array @_.
When we create an object, we actually create a reference that knows about the class it belongs to. Bless (which is a built-in function) is used to bless the reference to the class and then return an instance of the class. In the above example, we have passed a hash reference to the bless() function. But one can pass any kind of reference to the bless function e.g., array reference, which makes it much easier to work with a hash reference.
Creating default attribute values
Now we can know how to apply attributes. But what will happen if we don’t pass all the arguments in our Perl program? In this case, the attributes will be initialized as ‘null’ by the object. Hence there is a way to avoid that by setting default values that are overridden if the argument is present when the object is constructed. Logical or operator || can be used to achieve this effect.
Perl
sub new{
sub new{
my ( $class , $args ) = @_ ;
my $self = {
name => $args ->{name} || ‘iPhone XR’,
price => $args ->{price} || ‘52K’,
},
Return bless $self , $class ;
}
1;
|
Accessing attributes
The best way to accessing attributes is via accessor methods. These are methods that help us in Perl to get or set the value of each attribute.
The two types of accessor are:
- Getter: It gets the attribute’s value.
- Setter(also known as Mutator): It sets the attributes value.

Perl
package Person;
use strict;
use warnings;
sub new {
my ( $class , %args ) = @_ ;
my $self = \ %args ;
bless $self , $class ;
return $self ;
}
sub name {
my ( $self , $value ) = @_ ;
if ( @_ == 2)
{
$self ->{name} = $value ;
}
return $self ->{name};
}
1;
my $teacher = Person->new;
$teacher ->name( 'Foo' );
printf $teacher ->name;
|
We are calling the constructor here Person->new, which returns an object we assign to $teacher and then we are calling the accessor $teacher->name(‘Foo’) using it as a setter by providing it a value and then using the same accessor as a getter $teacher->name (without passing a value) making it fetch the current value of the attribute while using the same method called ‘name’.
When we call the $teacher->name(‘Foo’), Perl will notice that $teacher is a blessed reference to a hash and it was blessed into the Person (name-space). If there wasn’t a blessed reference, it wouldn’t have known what to do with the arrow and the “name” after that and it would’ve throw an exception: (Can’t call method .. on unblessed reference)
Well since it is blessed into the Person name-space, Perl will start to look for the “name” function in the Person name-space and once that function is found, Perl will call that function with the parameters which we’ve passed onto it, but it will also take the variable we had on the left-hand side of the arrow and pass it as the first argument. ($teacher in our case) .
In our example this “name” function is once called as a “setter” when we pass a value to it, and once as “getter” when we don’t pass any value through it. Because Perl passes the object as the first parameter this means that when it is called a “setter” we are actually going to get 2 parameters and when it is called a “getter” we are going to get one parameter.
The first statement in the “name” subroutine assigns the parameters to local variables. In the second statement now, Perl checks whether the function should act as a getter or as a setter? Perl checks the number of parameters. If it gets two parameters then this is a setter. In this case, it takes the content of $self.
If we use the ‘name’ function as a ‘getter’, then we don’t pass any value to it, which means $value will be undefined, but more importantly @_ will only have one element. This it will skip the assignment and the only thing it’ll do is to return the value of the ‘name’ key from the hash reference.
Conclusion:
This shows that the attributes of an object in Perl are just key/value pairs in a hash reference.
In the above example, we can see that the setter/getter is just a plain Perl function.
Therefore it also implements that the attributes of an object are simple entries in the HASH reference representing the object. The key in the hash is the name of the attribute and the respective value in the HASH is the value of the attribute.
Similar Reads
Object Oriented Programming (OOPs) in Perl
Object-oriented programming: As the name suggests, Object-Oriented Programming or OOPs refers to languages that uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind to
7 min read
Design Patterns in Object-Oriented Programming (OOP)
Software Development is like putting together a puzzle. Object-oriented programming (OOP) is a popular way to build complex software, but it can be tricky when you face the same design problems repeatedly. That's where design patterns come in. Design patterns are like well-known recipes for common p
15+ min read
Object Oriented Programming | HCI
In computer programming, Object-Oriented Programming (OOP) stands out as a paradigm that has converted the manner software program is conceptualized and built. OOP is based on foremost and concepts and plays a crucial role in the introduction of efficient and adaptable software solutions. This artic
8 min read
R - Object Oriented Programming
In this article, we will discuss Object-Oriented Programming (OOPs) in R Programming Language. We will discuss the S3 and S4 classes, the inheritance in these classes, and the methods provided by these classes. OOPs in R Programming Language:In R programming, OOPs in R Â provide classes and objects a
8 min read
How to Print Object Attributes in Python
In Python, objects are the cornerstone of its object-oriented programming paradigm. An object is an instance of a class, and it encapsulates both data (attributes) and behaviors (methods). Understanding how to access and print the attributes of an object is fundamental for debugging, inspecting, and
2 min read
What are Objects in Programming?
In object-oriented programming (OOP), objects are the basic entities that actually exists in the memory. Each object is based on a blueprint of attributes and behaviours (variables and functions) defined as Class. The basic purpose of a Class is to identify the common attributes and behaviours and g
2 min read
Assignment Operators in Programming
Assignment operators in programming are symbols used to assign values to variables. They offer shorthand notations for performing arithmetic operations and updating variable values in a single step. These operators are fundamental in most programming languages and help streamline code while improvin
7 min read
Object-Oriented Programing(OOP) Concepts for Designing Sytems
Modeling real-world entities is a powerful way to build systems with object-oriented programming, or OOP. In order to write modular, reusable, and scalable code, it focuses on fundamental ideas like classes, objects, inheritance, and polymorphism. Building effective and maintainable systems is made
15+ min read
Operator Overloading in Programming
Operator Overloading is a feature in some programming languages used to redefine or "overload" the standard behavior of operators (such as +, -, *, etc.) to work with user-defined data types. This is useful when working with objects of custom classes. In this article, we will learn about the basics
4 min read
Spring Boot - AOP(Aspect Oriented Programming)
The Java applications are developed in multiple layers, to increase security, separate business logic, persistence logic, etc. A typical Java application has three layers namely they are Web layer, the Business layer, and the Data layer. Web layer: This layer is used to provide the services to the e
4 min read