| 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, 2022 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package Object::Pad::MetaFunctions 0.825; | ||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use v5.18; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | our @EXPORT_OK = qw( | |||||||||
| 13 | metaclass | ||||||||||
| 14 | deconstruct_object | ||||||||||
| 15 | ref_field | ||||||||||
| 16 | get_field | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | 2 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=8.4
| |||||||||||
| 20 | 2 | ▶ | if( defined &builtin::reftype ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 2 | warnings->unimport( 'experimental::builtin' ); | |||||||||
| 22 | 2 | builtin->import(qw( reftype )); | |||||||||
| 23 | } | ||||||||||
| 24 | else { | ||||||||||
| 25 | 0 | require Scalar::Util; | |||||||||
| 26 | 0 | Scalar::Util->import(qw( reftype )); | |||||||||
| 27 | } | ||||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | =head1 NAME | ||||||||||
| 31 | |||||||||||
| 32 | C<Object::Pad::MetaFunctions> - utility functions for C<Object::Pad> classes | ||||||||||
| 33 | |||||||||||
| 34 | =head1 SYNOPSIS | ||||||||||
| 35 | |||||||||||
| 36 | =for highlighter language=perl | ||||||||||
| 37 | |||||||||||
| 38 | use v5.36; | ||||||||||
| 39 | use Object::Pad::MetaFunctions qw( deconstruct_object ); | ||||||||||
| 40 | |||||||||||
| 41 | sub debug_print_object ( $obj ) | ||||||||||
| 42 | { | ||||||||||
| 43 | my ( $classname, @repr ) = deconstruct_object( $obj ); | ||||||||||
| 44 | |||||||||||
| 45 | say "An object of type $classname having:"; | ||||||||||
| 46 | |||||||||||
| 47 | foreach my ( $fieldname, $value ) ( @repr ) { | ||||||||||
| 48 | printf "%30s = %s\n", $fieldname, $value; | ||||||||||
| 49 | } | ||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | =head1 DESCRIPTION | ||||||||||
| 53 | |||||||||||
| 54 | This module contains a number of miscellaneous utility functions for working | ||||||||||
| 55 | with L<Object::Pad>-based classes or instances thereof. | ||||||||||
| 56 | |||||||||||
| 57 | These functions all involve a certain amount of encapsulation-breaking into | ||||||||||
| 58 | the object instances being operated on. This sort of thing shouldn't be | ||||||||||
| 59 | encouraged in most regular code, but there can be occasions when it is useful; | ||||||||||
| 60 | such as debug printing of values, generic serialisation, or tightly-coupled | ||||||||||
| 61 | unit tests that wish to operate on the internals of the object instances they | ||||||||||
| 62 | test. | ||||||||||
| 63 | |||||||||||
| 64 | Therefore, use of these functions should be considered "last-resort". Consider | ||||||||||
| 65 | carefully the sorts of things you are trying to do with them, and whether this | ||||||||||
| 66 | kind of reaching into the internals of an object, bypassing all of its | ||||||||||
| 67 | interface encapsulation, is really the best technique to achieve your goal. | ||||||||||
| 68 | |||||||||||
| 69 | =head1 FUNCTIONS | ||||||||||
| 70 | |||||||||||
| 71 | =cut | ||||||||||
| 72 | |||||||||||
| 73 | =head2 metaclass | ||||||||||
| 74 | |||||||||||
| 75 | $metaclass = metaclass( $obj ); | ||||||||||
| 76 | |||||||||||
| 77 | I<Since version 0.67.> | ||||||||||
| 78 | |||||||||||
| 79 | Returns the L<Object::Pad::MOP::Class> metaclass associated with the class | ||||||||||
| 80 | that the object is an instance of. | ||||||||||
| 81 | |||||||||||
| 82 | =head2 deconstruct_object | ||||||||||
| 83 | |||||||||||
| 84 | ( $classname, @repr ) = deconstruct_object( $obj ); | ||||||||||
| 85 | |||||||||||
| 86 | I<Since version 0.67.> | ||||||||||
| 87 | |||||||||||
| 88 | Returns a list of perl values containing a representation of all the fields in | ||||||||||
| 89 | the object instance. This representation form may be useful for tasks such as | ||||||||||
| 90 | debug printing or serialisation of the instance. This list is prefixed by the | ||||||||||
| 91 | name of the class of instance as a plain string. | ||||||||||
| 92 | |||||||||||
| 93 | The exact form of this representation is still experimental and may change in | ||||||||||
| 94 | a later version. Currently, it takes the form of an even-sized list of | ||||||||||
| 95 | key/value pairs, associating field names with their values. Each key gives the | ||||||||||
| 96 | name of a component class and the full name of the field within it, separated | ||||||||||
| 97 | by a dot (C<.>). | ||||||||||
| 98 | |||||||||||
| 99 | 'CLASSNAME.$FIELD1' => VALUE, 'CLASSNAME.@FIELD2' => VALUE, ... | ||||||||||
| 100 | |||||||||||
| 101 | In the case of scalar fields, the value is the actual value of that field. In | ||||||||||
| 102 | the case of array or hash fields, the value in the repr list is a reference to | ||||||||||
| 103 | an anonymous I<copy of> the value stored in the field. | ||||||||||
| 104 | |||||||||||
| 105 | 'CLASSNAME.$SCALARFIELD' => $VALUE, | ||||||||||
| 106 | 'CLASSNAME.@ARRAYFIELD' => [ @VALUE ], | ||||||||||
| 107 | 'CLASSNAME.%HASHFIELD' => { %VALUE }, | ||||||||||
| 108 | |||||||||||
| 109 | The pairs are ordered, with the actual object class type first, followed by | ||||||||||
| 110 | any roles added by that class, then each parent class recursively. Within each | ||||||||||
| 111 | component class, the fields are given in declared order. | ||||||||||
| 112 | |||||||||||
| 113 | This reliable ordering may be useful when printing values in human-readable | ||||||||||
| 114 | form, or serialising to some stable storage. | ||||||||||
| 115 | |||||||||||
| 116 | =head2 ref_field | ||||||||||
| 117 | |||||||||||
| 118 | $fieldref = ref_field( $fieldname, $obj ); | ||||||||||
| 119 | |||||||||||
| 120 | I<Since version 0.67.> | ||||||||||
| 121 | |||||||||||
| 122 | Returns a reference to the named field storage variable of the given instance | ||||||||||
| 123 | object. The I<$fieldname> should be specified as the class name and the field | ||||||||||
| 124 | name separated by a dot (C<.>) (as per L</deconstruct_object>). | ||||||||||
| 125 | |||||||||||
| 126 | The class name may also be omitted; at which point the first occurrence of a | ||||||||||
| 127 | field of the given name found in any component class it matched instead. | ||||||||||
| 128 | |||||||||||
| 129 | If no matching field is found, an exception is thrown. | ||||||||||
| 130 | |||||||||||
| 131 | Be careful when using this function as it has the ability to expose instance | ||||||||||
| 132 | fields in a way that allows them to be modified. For a safer alternative when | ||||||||||
| 133 | only read access is required, use L</get_field> instead. | ||||||||||
| 134 | |||||||||||
| 135 | =cut | ||||||||||
| 136 | |||||||||||
| 137 | =head2 get_field | ||||||||||
| 138 | |||||||||||
| 139 | $scalar = get_field( $fieldname, $obj ); | ||||||||||
| 140 | @array = get_field( $fieldname, $obj ); | ||||||||||
| 141 | %hash = get_field( $fieldname, $obj ); | ||||||||||
| 142 | |||||||||||
| 143 | I<Since version 0.67.> | ||||||||||
| 144 | |||||||||||
| 145 | Returns the value of the named field of the given instance object. Behaves | ||||||||||
| 146 | correctly given context; namely, that when invoked on array or hash fields in | ||||||||||
| 147 | scalar context it will return the number of elements or keys, or in list | ||||||||||
| 148 | context will return the list of elements or key/value pairs. | ||||||||||
| 149 | |||||||||||
| 150 | =cut | ||||||||||
| 151 | |||||||||||
| 152 | sub get_field | ||||||||||
| 153 | 8 | ▶ | { | ||||||||
|
Subroutine
get_field: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 154 | 8 | my $ref = ref_field( @_ ); | |||||||||
| 155 | 8 | my $type = reftype $ref; | |||||||||
| 156 | 8 | ▶ | return @$ref if $type eq "ARRAY"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 6 | ▶ | return %$ref if $type eq "HASH"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | 4 | return $$ref; | |||||||||
| 159 | } | ||||||||||
| 160 | |||||||||||
| 161 | =head1 AUTHOR | ||||||||||
| 162 | |||||||||||
| 163 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 164 | |||||||||||
| 165 | =cut | ||||||||||
| 166 | |||||||||||
| 167 | 2 | 0x55AA; | |||||||||