Object...ctions(3pm)

πŸ’– NAME πŸ’–

This is the manual page for Object::Pad::MetaFunctions β€” a User Contributed Perl Documentation moment, no cap β€” and it is literally your utility-function bestie for working with Object::Pad classes. The NAME line says it plain: Object::Pad::MetaFunctions β€” utility functions for Object::Pad classes. Slay. πŸ’…


✨ SYNOPSIS ✨

use v5.36;
use Object::Pad::MetaFunctions qw( deconstruct_object );

sub debug_print_object ( $obj )
{
   my ( $classname, @repr ) = deconstruct_object( $obj );

   say "An object of type $classname having:";

   foreach my ( $fieldname, $value ) ( @repr ) {
      printf "%30s = %s\n", $fieldname, $value;
   }
}

🌸 DESCRIPTION 🌸

Okay so this module is a little grab-bag of miscellaneous utility functions for working with Object::Pad-based classes, or instances of those classes. Very "tools for the girls who need to peek behind the curtain" energy. 🫣

The big thing to know: every one of these functions involves breaking encapsulation into the object instances you pass in. Like, actually reaching into their internals. The original page is very clear that this sort of thing shouldn't be encouraged in most regular code β€” but there are absolutely occasions when it's useful. Think debug printing of values, generic serialisation, or tightly-coupled unit tests that want to operate on the internals of the object instances they test. πŸ’€

So yeah: these functions are β€œlast-resort” territory. Consider carefully what you're trying to do with them, and whether this kind of reaching into an object's internals β€” bypassing all of its interface encapsulation β€” is really the best technique for your goal. The page is not being dramatic; it's being real. πŸ™ˆ


πŸŽ€ FUNCTIONS πŸŽ€

πŸ¦‹ metaclass πŸ¦‹

$metaclass = metaclass( $obj );

Since version 0.67.

Returns the Object::Pad::MOP::Class metaclass associated with the class that the object is an instance of. That's the whole vibe. ✨


πŸ¦‹ deconstruct_object πŸ¦‹

( $classname, @repr ) = deconstruct_object( $obj );

Since version 0.67.

Returns a list of Perl values containing a representation of all the fields in the object instance. This representation form may be useful for things like debug printing or serialisation of the instance. The list is prefixed by the name of the class of the instance as a plain string. πŸŽ€

The exact form of this representation is still experimental and may change in a later version β€” so like, don't build your entire personality on it. Currently, it takes the form of an even-sized list of key/value pairs, associating field names with their values. Each key gives the name of a component class and the full name of the field within it, separated by a dot (.).

'CLASSNAME.$FIELD1' => VALUE, 'CLASSNAME.@FIELD2' => VALUE, ...

In the case of scalar fields, the value is the actual value of that field. In the case of array or hash fields, the value in the repr list is a reference to an anonymous copy of the value stored in the field.

'CLASSNAME.$SCALARFIELD' => $VALUE,
'CLASSNAME.@ARRAYFIELD'  => [ @VALUE ],
'CLASSNAME.%HASHFIELD'   => { %VALUE },

The pairs are ordered, with the actual object class type first, followed by any roles added by that class, then each parent class recursively. Within each component class, the fields are given in declared order. This reliable ordering may be useful when printing values in human-readable form, or serialising to some stable storage. Iconic, honestly. πŸ‘‘


πŸ¦‹ ref_field πŸ¦‹

$fieldref = ref_field( $fieldname, $obj );

Since version 0.67.

Returns a reference to the named field storage variable of the given instance object. The $fieldname should be specified as the class name and the field name separated by a dot (.), as per deconstruct_object. 🫢

The class name may also be omitted; at which point the first occurrence of a field of the given name found in any component class it matched is used instead.

If no matching field is found, an exception is thrown. No silent little "oops", bestie β€” it throws. 😭

Be careful when using this function, because it has the ability to expose instance fields in a way that allows them to be modified. For a safer alternative when only read access is required, use get_field instead. This is the "look with your eyes, not your hands" rule. πŸ’…

πŸ¦‹ get_field πŸ¦‹

$scalar = get_field( $fieldname, $obj );
@array  = get_field( $fieldname, $obj );
%hash   = get_field( $fieldname, $obj );

Since version 0.67.

Returns the value of the named field of the given instance object. Behaves correctly given context β€” namely, when invoked on array or hash fields in scalar context it will return the number of elements or keys, or in list context it will return the list of elements or key/value pairs. Context-aware and thriving. 🌈


πŸ‘‘ AUTHOR πŸ‘‘

Paul Evans <leonerd@leonerd.org.uk> 🫢


Okay bestie, that's the page! Footer sign-off time: this came from source/version perl v5.42.3, dated 2026-08-22. Byeee, go use these responsibly πŸ’œβœ¨

πŸ“¦ where this page comes from