| 1 | # You may distribute under the terms of either the GNU General Public License | ||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||
| 3 | # | ||
| 4 | # (C) Paul Evans, 2021-2022 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | package Object::Pad::MOP::FieldAttr 0.825; | ||
| 7 | |||
| 8 | use v5.18; | ||
| 9 | use warnings; | ||
| 10 | |||
| 11 | # This is an XS-implemented object type provided by Object::Pad itself | ||
| 12 | require Object::Pad; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | C<Object::Pad::MOP::FieldAttr> - meta-object representation of a field attribute for C<Object::Pad> | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | =for highlighter language=perl | ||
| 21 | |||
| 22 | This API provides a way for pure-perl implementations of field attributes to be | ||
| 23 | provided. Pure-perl attributes cannot currently add new I<behaviour> to the | ||
| 24 | way that fields work, but they do provide a means for class authors to annotate | ||
| 25 | extra metadata onto fields, that can be queried by other code. | ||
| 26 | |||
| 27 | Primilarily this is done by using the L<Object::Pad::MOP::Field/get_attribute_value> | ||
| 28 | accessor method on a field metadata instance. | ||
| 29 | |||
| 30 | This API should be considered B<experimental>, and will emit warnings to that | ||
| 31 | effect. They can be silenced with | ||
| 32 | |||
| 33 | use Object::Pad qw( :experimental(custom_field_attr) ); | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | =head1 METHODS | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | =head2 register | ||
| 42 | |||
| 43 | Object::Pad::MOP::FieldAttr->register( $name, %args ); | ||
| 44 | |||
| 45 | I<Since version 0.60.> | ||
| 46 | |||
| 47 | Creates a new field attribute of the given name. The name must begin with a | ||
| 48 | capital letter, in order to distinguish this from any of the built-in core | ||
| 49 | attributes, whose names are lowercase. | ||
| 50 | |||
| 51 | The attribute is only available if the hints hash contains a key of the name | ||
| 52 | given by the attribute's C<permit_hintkey> argument. This would typically be | ||
| 53 | set in the hints hash by the C<import> method of the module implementing it, | ||
| 54 | and would be named based on the name of the module providing the attribute: | ||
| 55 | |||
| 56 | sub import { $^H{"Some::Package::Name/Attrname"} } | ||
| 57 | |||
| 58 | Takes the following additional named arguments: | ||
| 59 | |||
| 60 | =over 4 | ||
| 61 | |||
| 62 | =item permit_hintkey => STRING | ||
| 63 | |||
| 64 | Required. A string giving a key that must be found in the hints hash (C<%^H>) | ||
| 65 | for this attribute name to be visible. | ||
| 66 | |||
| 67 | =item no_value => BOOL | ||
| 68 | |||
| 69 | An optional flag; if set to true then no value is permitted on the attribute's | ||
| 70 | declaration. A compiletime error will be generated if a value is provided | ||
| 71 | |||
| 72 | =item must_value => BOOL | ||
| 73 | |||
| 74 | An optional flag; if set to true then a value is required on the attribute's | ||
| 75 | declaration. A compiletime error will be generated if a value is not provided. | ||
| 76 | |||
| 77 | If neither of these flags are provided, then a value is optional. It is not | ||
| 78 | permitted to set both flags at once. | ||
| 79 | |||
| 80 | =item apply => CODE | ||
| 81 | |||
| 82 | An optional code reference for a callback function to invoke when the | ||
| 83 | attribute is applied to a field. If present, it is passed the field metadata | ||
| 84 | instance as a L<Object::Pad::MOP::Field> reference, and a string containing | ||
| 85 | the contents of the attribute's parenthesized value. The return value of the | ||
| 86 | callback will be stored as the attribute's value and can be accessed by the | ||
| 87 | C<get_attribute_value> method on the field metadata. | ||
| 88 | |||
| 89 | $result = $apply->( $fieldmeta, $value ) | ||
| 90 | |||
| 91 | If the C<apply> callback is absent then the string value itself is stored. | ||
| 92 | |||
| 93 | =back | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | =head1 AUTHOR | ||
| 98 | |||
| 99 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | 0x55AA; |