Object...Struct(3pm)

πŸ“¦
version 0.06 Β· 5 versions
, in debian trixie, ubuntu resolute

πŸ’– 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 :reader field 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