How to get structure and inheritance history of a Perl object (not a class)?
Asked Answered
V

1

1
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
my $tx = $ua->get( shift );

How to get structure and inheritance history of these Perl objects ($ua and $tx)?

Data::Dumper shows only small part of structure and inheritance history.

Volotta answered 28/12, 2019 at 16:31 Comment(3)
See also Devel::Isa::Explainer and mro::get_linear_isa()Chesser
What are you really trying to find out? The only thing you need to know about an object for 95% of cases is its documented interface. Doing anything based on only the programmatic structure of the object is unsupported.Silversmith
You are right. All things are well documented. But, to explore the UserAgent object I have to spend a lot of time reading documentation and writing notes. On the other hand, if I will see the complete structure of UserAgent object - it will save my time reading documentation.Volotta
W
3

Perl doesn't keep track of historical values of variables.

Perl doesn't keep track of historical inheritance relationships.

Objects don't have inheritance relationships; classes do.


The current structure of an object can be found using the following:

use Data::Dumper qw( Dumper );

{
   local $Data::Dumper::Purity = 1;
   print(Dumper($o));
}

(It has limitations: Only one value of dualvars is shown; associated magic isn't shown; etc. If you need a more precise representation, Devel::Peek's Dump can be used.)

The classes from which an object's class currently inherits can be found using the following:

use mro          qw( );
use Scalar::Util qw( blessed );

say join ", ", @{ mro::get_linear_isa(blessed($o)) };
Wharve answered 28/12, 2019 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.