-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introspection into Object::Pad-based objects #164
Comments
Absolutely! Just let us know 🙃 |
This particular piece of code was a proof-of-concept from a unit test: use v5.26;
use feature 'signatures';
use List::Util 'max';
sub dump_object ( $obj )
{
my $metaclass = Object::Pad::MOP::Class->for_class( ref $obj );
my @metas;
while( $metaclass ) {
# TODO: roles
push @metas, $metaclass;
( $metaclass ) = $metaclass->superclasses;
}
my @output;
foreach my $metaclass ( @metas ) {
foreach my $metaslot ( $metaclass->slots ) {
push @output, [
join( "/", $metaclass->name, $metaslot->name ),
$metaslot->value( $obj )
];
}
}
# Technically a Unicode bug in here but printf is dumb
my $maxwidth = max map { length $_->[0] } @output;
return join "\n", map {
sprintf "%-*s | %s", $maxwidth, $_->[0], $_->[1] // "<undef>";
} @output;
} Gives output:
Obviously here all I've done is stringified the values of the slots, but you can see all the pieces are there to let any sort of recursive dumper module walk the slot values in the same way as it might e.g. walk the values of a hash and recurse into those values on the way down. It doesn't yet handle slots included from roles, but that's an open |
Hey @leonerd! Quick questions:
My proposal is that Object::Pad::MOP::Slot gains the following new methods:
Another approach is to get rid of What do you think? Does any of that make any sense to you? Cheers! |
Yes - it should come in declaration order, for each (partial) class.
Ooh, fun question. Currently you can't - you can't even know that a default exists. Would you need to know such, for
Should be simple enough.
Why would that need to take the instance reference? Surely the default is a single default for the class as a whole?
I'm not sure I follow this bit. |
I think so, yeah. If you are debugging an instance, it's important to see where each value came from.
You're right, it doesn't need to take the instance reference. For a second there I mistook slots for roles and assumed they had no idea which classes had them. My mistake :)
Oh, this was just me thinking out loud how to check all the other attributes available for $slot->has_param;
$slot->param_name; you would have: $slot->has_attribute('param');
$slot->attribute_name('param'); Then I imagined this same API could be used to fetch information regarding default values if they were changed to be attributes too (e.g. "has_attribute('default')"), but of course that would change the syntax from: has $x = 0; to has $x :default(0); which I'm quite confident is not what you want. So just ignore my ramblings :) |
You may or maynot be aware of Object::Pad - in brief it's an experimental syntax module in preparation of a true in-core object system. As part of the periphery around this we are wondering how it would interact with things like Data::Printer.
Would youfolks be open to some suggestions on how to operate this?
The text was updated successfully, but these errors were encountered: