Lesson 49 of 55
10 mins readPython Object-Oriented Perl: bless & Constructors
In Plain English
In classic Perl OOP, an object is simply a reference (usually a hash) tied to a package using the `bless($self, $class)` built-in. Methods receive the invocant as their first argument.
Deep Dive: How It Works
bless Function: Associates a data reference with a package namespace.
Constructor (new): A subroutine that creates a hash reference, blesses it, and returns it.
Invocant ($self / $class): Method calls `$obj->method()` pass `$obj` as the first argument in `@_`.
Core Rules to Remember

bless() Binds References to Classes: Transforms a simple hash reference into a full object instance.

First Parameter is Invocant: `my ($self, @args) = @_;` unpacks the calling object.
Live Interactive Example
Hit Run Code to see it liveCreating a Perl Class with bless
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Output Console
Click "Run Code" to view the rendered output.
How it works: Server->new created and blessed a hash reference, and ->get_endpoint accessed its fields.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Define User Object with bless
Define a `User` class with a `new` constructor taking `name`.
Add a method `get_name` returning `$self->{name}`.
Instantiate `my $u = User->new(name => "Alice");` and print `"User: " . $u->get_name() . "\n"`.
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.