Privacy and speed with Perl’s Object::Pad
Tags: perl, corinna, oop, moo
Published: 2021-08-10 14:11:05

---
title: Privacy and speed with Perl’s Object::Pad
published: true
date: 2021-08-10 14:00:00 UTC
tags: Perl,Corinna,OOP,Moo
canonical_url: https://phoenixtrap.com/2021/08/10/privacy-and-speed-with-perls-objectpad/
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cvyu4um8sy63zae5pra3.jpeg
---

[Last week](https://phoenixtrap.com/2021/08/03/whats-next-oo-perl/) found me exploring [Object::Pad](https://metacpan.org/pod/Object::Pad) as an alternative to the [Moo](https://metacpan.org/pod/Moo) object-oriented framework for Perl since the former is prototyping the syntax and concepts for a proposed built-in OO framework named [Corinna](https://github.com/Ovid/Cor/wiki). I had to put that particular project on hold as [dbcritic](https://mjgardner.github.io/dbcritic/)’s current design is a bit [too role-happy](https://twitter.com/markjgardner/status/1422729187908071426) and Object::Pad currently lacks [method modifiers](https://metacpan.org/pod/Class::Method::Modifiers) as in Moo. (Corinna is [explicitly skipping them](https://github.com/Ovid/Cor/wiki/Corinna-Overview#no-method-modifiers) for its current minimum viable product.) Thankfully, **development continues at a rapid pace**. For instance, author [Paul Evans](https://leonerds-code.blogspot.com/) has [already addressed](https://metacpan.org/release/PEVANS/Object-Pad-0.50/source/Changes#L29) a problem I ran into when attempting to examine slot values in the debugger.

But I wanted to highlight a point I [made in one of the comments last week](https://phoenixtrap.com/2021/08/03/whats-next-oo-perl/#comment-338): Object::Pad’s [slots](https://metacpan.org/pod/Object::Pad#has) (a.k.a. fields, attributes, whatever) are **private by default**, completely unexposed to other class instances unless they [monkey with the meta-object protocol](https://metacpan.org/pod/Object::Pad::MOP::Slot). Unless you explicitly define or [generate](https://metacpan.org/pod/Object::Pad#:reader,-:reader(NAME)) some kind of accessor method, these slots act like lexical (a.k.a. `my`) variables and are only available to [methods](https://metacpan.org/pod/Object::Pad#method) within the class.

Here’s an example:

{% gist https://gist.github.com/mjgardner/916de13c8f15ac7cb699fc18f137eef0 %}

This stands in stark contrast to Perl’s more low-tech [hashref-based objects](https://perldoc.perl.org/perlobj#An-Object-is-Simply-a-Data-Structure), where all attributes are available simply through [dereferencing the instance](https://perldoc.perl.org/perlobj#Attributes), e.g., `$object->{foo}`. Although [discouraged](https://perldoc.perl.org/perlref#Objects), **OO purists sometimes ding Perl for this kind of unenforced encapsulation**, and I myself have seen codebases that violate it despite the convention of preceding private method and attribute names with an underscore (`_`).

Unfortunately, there is **not yet any way to declare an Object::Pad method private**. You could use [lexical subroutines](https://perldoc.perl.org/perlsub#Lexical-Subroutines), but then you lose the convenience of a pre-made `$self` variable and [accessibility through the MOP](https://metacpan.org/pod/Object::Pad::MOP::Method). The Corinna proposal lists [several different types of methods](https://github.com/Ovid/Cor/wiki/Methods) including [private](https://github.com/Ovid/Cor/wiki/Methods#private-methods) ones, so maybe this is an area for future Object::Pad development.

Another [open question from the comments](https://phoenixtrap.com/2021/08/03/whats-next-oo-perl/#comment-339): “How is [Object::Pad] on mem­o­ry and speed com­pared to Moo and [`bless`](https://perldoc.perl.org/functions/bless)ed objects?” Luckily the prolific [perlancar](https://perlancar.wordpress.com) has already added Object::Pad to his [Bencher::Scenarios::Accessors](https://metacpan.org/pod/Bencher::Scenarios::Accessors) distribution, and from that it appears that between it and Moo, Object::Pad is faster on [startup](https://metacpan.org/pod/Bencher::Scenario::Accessors::ClassStartup), neck-and-neck on [object construction](https://metacpan.org/pod/Bencher::Scenario::Accessors::Construction) and [accessor generation](https://metacpan.org/pod/Bencher::Scenario::Accessors::GeneratorStartup), and slower on [reads](https://metacpan.org/pod/Bencher::Scenario::Accessors::Get) and [writes](https://metacpan.org/pod/Bencher::Scenario::Accessors::Set). (Note that Object::Pad is a fast-moving target so these figures may not track with the latest version’s changes.) It’s no surprise that plain `bless`ed objects fared better than both in most scenarios except for [reads](https://metacpan.org/pod/Bencher::Scenario::Accessors::Get), where Moo was faster than hash-based objects but slower than array-based.

I expect that should Corinna be built into Perl it would narrow that gap with `bless`ed objects, but in my mind, **the advantages of using an object system outweigh the performance hit** 95% of the time. As far as benchmarking memory goes, I still need to test that on a Linux box (maybe my [new VPS](https://phoenixtrap.com/2021/07/28/vps-upgrade/)?) once I get more familiar with the [Bencher](https://metacpan.org/pod/Bencher) framework.