#!/usr/bin/env perl use 5.012; use warnings; use Data::Dump qw/dump dumpf/; # This simulates the "resource" key you mentioned in (3) my $singleton = bless({ note => 'Defined outside $hairy_object', 1..20 # To really drive the point home :-) }, 'Singleton::Resource'); # A reasonably complex object, just enough to illustrate. my $hairy_object = bless { attr1 => 'val1', resource => $singleton, unwanted => bless({ thing => 'We want to ignore' }, 'Unwanted'), however => { this => { unwanted => bless({ is => 'actually wanted' }, 'Unwanted'), }}, Silly => { complex => { references => 'can be mangled', so => "they make more sense.", } } }, 'Hairy::Object'; # Throw in a couple of circular references $hairy_object->{circular_ref} = $hairy_object; $hairy_object->{internal_ref} = $hairy_object->{however}{this}; say "Before:"; say '-' x 76; say dump($hairy_object); say "\nAfter:"; say '-' x 76; $Data::Dump::INDENT = ' '; # Edit to taste. '| ' is nice, too. # Set up callback; called for every node in the $hairy_object tree # This is the heart of the logic that controls the dumpf() output my $dump = dumpf($hairy_object, sub { no warnings 'uninitialized'; my ($ctx, $obj) = @_; my %r; if ($ctx->depth == 0) { $r{hide_keys} = [ grep { ref($obj->{$_}) =~ /^(Unwanted|Something::Else)$/; } keys %$obj ]; }); # Munge the resulting dump a bit to remove extraneous bits $dump =~ s/^\Q$Data::Dump::INDENT\E//mg; # Remove one indent $dump =~ s/\Ado \{.+?my \$a = bless\(\{\n//s; $dump =~ s/\$a;\n\}//ms; say "\$a = bless( {\n$dump";