Object...Struct(3pm)
π Object...Struct(3pm) π
okay bestie, gather round, because today we're talking about Object::Pad::ClassAttr::Struct β that's the Object...Struct(3pm) page, and yes it's a Perl module, and yes it's about to make your classes so much cuter with basically zero effort. no cap. π«Ά
β¨ NAME β¨
Object::Pad::ClassAttr::Struct β declare an Object::Pad class to be struct-like π
πΈ SYNOPSIS πΈ
use Object::Pad;
use Object::Pad::ClassAttr::Struct;
class Colour :Struct {
# These get :param :mutator automatically
field $red = 0;
field $green = 0;
field $blue = 0;
# Additional methods are still permitted
method lightness {
return ($red + $green + $blue) / 3;
}
}
my $cyan = Colour->new( green => 1, blue => 1 );
# A positional constructor is created automatically
my $white = Colour->new_values(1, 1, 1);
likeβ¦ look at it. it's giving effortless. it's giving struct energy. π¦
π DESCRIPTION π
so this module hands you a third-party class attribute for classes built on Object::Pad. what does it do? it automatically slaps some attributes onto every single field you add to the class. it's a convenient shortcut for making structure-like classes, and honestly? it ate. π₯
you write a field, it gets the attributes. you don't. that's the whole vibe. periodt.
π CLASS ATTRIBUTES π
π :Struct
class Name :Struct ... { ... }
okay so this is the moment. :Struct automatically applies the :param and :mutator attributes to every field defined on the class. what does that mean in bestie terms?
- the constructor will accept parameters for each field, so you can initialise the value π
- each field will have an lvalue mutator method β¨
and it's not just the fields getting a glow-up, bestie β the class itself gains the :strict(params) attribute too, meaning the constructor will check parameter names and throw an exception for unrecognised names. so if you typo a param? it's over for you, respectfully. π
since version 0.04, a positional constructor class method called new_values is also provided into the class. it takes a value for every field positionally, in declared order. iconic behaviour tbh.
$obj = ClassName->new_values($v1, $v2, $v3, ...);
β οΈ heads up bestie: this positional constructor must receive as many positional arguments as there are fields in total in the class β even the optional ones. all arguments are required here. no skipping, no shortcuts, no "but I didn't want to set that one". all of them. π
since version 0.05, the following options are permitted inside the attribute value parentheses:
:Struct(readonly)β instances of this class do not permit fields to be modified after construction. the accessor is created using the:readerfield attribute rather than:mutator. so she's locked in, frozen, immutable, and honestly kind of main character energy for it. π§
π AUTHOR π
Paul Evans <leonerd@leonerd.org.uk> β the actual legend behind all this. π
and that's the whole page, bestie! no crumbs left, every flag and caveat accounted for. π
this little manual came from perl v5.40.1, dated 2025-04-15 β so go forth, write struct-like classes, and slay. mwah. π β¨
π¦ where this page comes from
- debian trixie: libobject-pad-classattr-struct-perl 0.06-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libobject-pad-classattr-struct-perl 0.06-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms