└── lib └── Devel └── Diagram.pm /lib/Devel/Diagram.pm: -------------------------------------------------------------------------------- 1 | package Devel::Diagram; 2 | 3 | use strict; 4 | use warnings; 5 | 6 | use Scalar::Util qw(isweak); 7 | 8 | sub diagram { 9 | my $obj = shift; 10 | my $depth = shift || 0; 11 | my $history = shift || {}; 12 | my $output = ''; 13 | 14 | unless (defined $obj) { 15 | return "(undef)\n"; 16 | } 17 | 18 | unless (ref $obj) { 19 | return "'$obj'\n"; 20 | } 21 | 22 | my $weak = eval { isweak($obj) } ? '(weak) ' : ''; 23 | 24 | $output .= "$weak$obj"; 25 | 26 | if ($history->{$obj}) { 27 | return "$output ^^\n"; 28 | } 29 | 30 | $output .= "\n"; 31 | 32 | $history->{$obj}++; 33 | 34 | $depth++; 35 | 36 | my $pad = ' ' x $depth; 37 | 38 | eval { 39 | while (my ($key, $val) = each %$obj) { 40 | $output .= "$pad$key: "; 41 | $output .= diagram($val, $depth, $history); 42 | } 43 | }; 44 | 45 | return $output unless $@; 46 | 47 | eval { 48 | foreach my $i (@$obj) { 49 | $output .= "$pad - "; 50 | $output .= diagram($i, $depth, $history); 51 | } 52 | }; 53 | 54 | return $output unless $@; 55 | 56 | eval { 57 | $output .= "$pad** " . diagram($$obj); 58 | }; 59 | 60 | return $output unless $@; 61 | 62 | return $output; 63 | } 64 | 65 | 1; 66 | --------------------------------------------------------------------------------