.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Object::Pad::ClassAttr::Struct 3pm" .TH Object::Pad::ClassAttr::Struct 3pm 2024-03-07 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME "Object::Pad::ClassAttr::Struct" \- declare an "Object::Pad" class to be struct\-like .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& 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); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This module provides a third-party class attribute for Object::Pad\-based classes, which applies some attributes automatically to every field added to the class, as a convenient shortcut for making structure-like classes. .SH "CLASS ATTRIBUTES" .IX Header "CLASS ATTRIBUTES" .SS :Struct .IX Subsection ":Struct" .Vb 1 \& class Name :Struct ... { ... } .Ve .PP Automatically applies the \f(CW\*(C`:param\*(C'\fR and \f(CW\*(C`:mutator\*(C'\fR attributes to every field defined on the class, meaning the constructor will accept parameters for each field to initialise the value, and each field will have an lvalue mutator method. .PP In addition, the class itself gains the \f(CW:strict(params)\fR attribute, meaning the constructor will check parameter names and throw an exception for unrecognised names. .PP \&\fISince version 0.04\fR a positional constructor class method called \&\f(CW\*(C`new_values\*(C'\fR is also provided into the class, which takes a value for every field positionally, in declared order. .PP .Vb 1 \& $obj = ClassName\->new_values($v1, $v2, $v3, ...); .Ve .PP 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. .PP \&\fISince version 0.05\fR the following options are permitted inside the attribute value parentheses: .PP \fI:Struct(readonly)\fR .IX Subsection ":Struct(readonly)" .PP Instances of this class do not permit fields to be modified after construction. The accessor is created using the \f(CW\*(C`:reader\*(C'\fR field attribute rather than \f(CW\*(C`:mutator\*(C'\fR. .SH AUTHOR .IX Header "AUTHOR" Paul Evans