Object...::Final(3pm)

πŸ“¦
version 0.06 Β· 5 versions
, in ubuntu stonking

🌸 NAME 🌸

Okay bestie, so this page is the man page for Object...::Final(3pm) β€” yes, that's the manual name right there, cute little abbreviation and all β€” and what it actually documents is "Object::Pad::FieldAttr::Final", which is the module you want when you need your "Object::Pad" fields to go readonly after construction. Periodt. πŸ”’


✨ SYNOPSIS ✨

Like, here's the whole vibe in one screen:

use Object::Pad;
use Object::Pad::FieldAttr::Final;

class Rectangle {
   field $width  :param :reader :Final;
   field $height :param :reader :Final;

   field $area :reader :Final;

   ADJUST {
      $area = $width * $height;
   }
}

That's it. That's the whole thing. Slay. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

So this module hands you a third-party field attribute for classes built with Object::Pad. What does that attribute do, bestie? It declares that the field it's glued onto shall be set as readonly the moment the constructor returns β€” which means literally no further modification of it is allowed. Locked. Sealed. Iconic. πŸ”

WARNING πŸ’€ β€” the ability for Object::Pad to accept third-party field attributes is still new and highly experimental, and it's subject to much API change in the future. So as a result, this module should be considered equally experimental. No cap β€” things may absolutely shift under you. Don't say nobody warned you. 😭

πŸŽ€ FIELD ATTRIBUTES πŸŽ€

:Final

Usage, exactly as written:

field $name :Final ...;
field $name :Final ... = DEFAULT;

So here's the tea: it declares that the field variable will be set readonly at the end of the constructor β€” and that happens after any assignments coming from :param declarations or from ADJUST blocks. From that point on, the value can't otherwise be modified by directly writing into the field variable. Bye-bye mutation energy. πŸ‘‹

Watch how this plays out:

field $x :Final;

ADJUST { $x = 123; }    # this is permitted

method m { $x = 456; }  # this will fail

See? The ADJUST block gets to do its little assignment because it happens before the locking. The method trying to write later? Nope. That one will fail. F in the chat. 😭

Important: this is only a shallow readonly setting, okay? So if the field variable is holding a reference to a data structure, that structure itself stays mutable. The box is locked, not the contents. ✨
field $aref :Final;
ADJUST { $aref = []; }

method more { push @$aref, "another"; }   # this is permitted

Pushing onto it? Totally fine, permitted, go off. The field variable itself just can't be reassigned. 🌈


πŸ’– AUTHOR πŸ’–

Paul Evans <leonerd@leonerd.org.uk>


And that's the whole page, bestie! 🫢 This was written up from perl v5.42.3, dated 2026-08-22, and it lives in the manual as Object...::Final(3pm). Go forth and freeze your fields. πŸ‘‘βœ¨

πŸ“¦ where this page comes from