Object...Checked(3pm)

πŸ“¦ version 0.12, in alpine 3.24

πŸ’… Object...Checked(3pm) β€” like, the whole vibe

okay so bestie, welcome to Object::Pad::FieldAttr::Checked (docs for perl v5.42.2, dated 2026-02-15, no cap), the lil module that puts value constraint checks on your Object::Pad fields πŸŽ€


✨ NAME ✨

"Object::Pad::FieldAttr::Checked" β€” it applies value constraint checks to "Object::Pad" fields. that's it, that's the whole job, and it does it gorgeously 🫢


🌸 SYNOPSIS 🌸

so with Data::Checks you can do this whole moment:

use Object::Pad;
use Object::Pad::FieldAttr::Checked;
use Data::Checks qw( Num );

class Point {
   field $x :param :reader :Checked(Num);
   field $y :param :reader :Checked(Num);
}

Point->new( x => 123, y => "456" );         # this is fine

Point->new( x => "hello", y => "world" );   # throws an exception

see how the first one slays and the second one absolutely does not? that's the whole point of this module πŸ’€βœ¨


πŸ’– DESCRIPTION πŸ’–

so this module hands you a third-party field attribute for classes built with Object::Pad, and what it declares is: any value assigned to the field has to conform to a given constraint check. simple as that, iconic behaviour only πŸ‘‘

⚠️ WARNING β€” the ability for Object::Pad to take third-party field attributes is still new and highly experimental, and it's subject to much API change in the future. so yeah, this module should be considered equally experimental. like, we're all early adopters here and we're all brave for it 🫑

and also, ngl, the behaviour this module gives you should be considered more of a work-in-progress stepping stone. ideally, constraint syntax ought to be provided in a much more fundamental way by Perl itself β€” letting it be used on "my" lexicals, subroutine parameters, and other places as well as object fields. this module is more of a placeholder, letting some part of that behaviour be specified for object fields, while not getting in the way of a more general, more powerful system being added in future. it's giving "temporary but make it cute" 🧚


πŸ”₯ FIELD ATTRIBUTES πŸ”₯

πŸŽ€ :Checked

field $name :Checked(EXPRESSION) ...;

this declares that any value assigned to the field β€” whether during the constructor or via an accessor method β€” must conform to the constraint checker specified by the expression. attempts to assign a non-conforming value will throw an exception and the field will not be modified. no glow-downs allowed, periodt. currently only scalar fields are supported πŸ’…

at compiletime, the string given by EXPRESSION is eval()'ed in scalar context, and its result is stored as part of the field's definition. the expression must yield a value usable by Data::Checks. namely, one of these three gorgeous options:

  • any of the constraint checkers provided by the Data::Checks module itself πŸ“
  • an object reference with a "check" method:
$ok = $checkerobj->check( $value );
  • a plain string giving the name of a package with a "check" method:
$ok = $checkerpkg->check( $value );

and if you're using a plain package name as a checker, be sure to quote package names so it will not upset use strict πŸ™ˆ like this:

field $x :Checked('CheckerPackage');

and because this is the exact interface supported by Types::Standard, any constraint object provided by that module is already supported here. we love compatibility, we love a crossover episode πŸ’œ

use Types::Standard qw( Str Num );

field $name :Checked(Str);
field $age  :Checked(Num);

at runtime, this constraint checker is used every single time an attempt is made to assign a value to the field from outside the object class β€” whether that's from ":param" initialisation, or invoking a ":writer", ":accessor" or ":mutator". the checker is used as the invocant for invoking a "check" method, and the new value for the field is passed as an argument. if the method returns true, the assignment is allowed βœ… if false, it's rejected with an exception and the field itself remains unmodified 🚫

(for performance reasons, the "check" method is actually resolved into a function at compiletime when the ":Checked" attribute is applied, and this stored function is the one that is called at assignment time. if the method itself is replaced later by globref assignment or other trickery, this updated function will not be used. so like, don't be sneaky πŸ’€)

🚨 note carefully: direct assignment into the field variable by code within the class is not checked. partly because of design considerations, and partly because any way to implement that would be horribly slow, or flat-out impossible. prior to version 0.04 this module used to claim that even direct assignments would be checked. but that gave a false sense of safety if deeply-nested containers were involved and modified from within. so we're being honest now 🫢


πŸ¦‹ SEE ALSO πŸ¦‹

  • Object::Pad::FieldAttr::Isa β€” apply class type constraints to "Object::Pad" fields. the sibling, the other icon, the one you also need in your life ✨

🌈 AUTHOR 🌈

Paul Evans <leonerd@leonerd.org.uk> β€” the main character who made all this happen πŸ‘‘


and that's the whole page bestie!! this was perl v5.42.2, dated 2026-02-15, and honestly? iconic of it. go forth and constrain those fields πŸ’–πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from