package Data::AsString; require Carp; use strict; use warnings; use overload q("") => sub { shift->as_string() }; sub new { my $class = shift; my $self = {}; $self->{struct} = shift; $self->{info} = { depth => 0 }; return bless $self, $class; } sub as_string { my $self = shift; $self->{'ref'} = $self->_traverse($self->{struct}); $self->{string} = $self->_collapse_info(); return $self->{string}; } sub _traverse { my $self = shift; my $ds = shift; my @values = (); if(ref($ds) eq 'HASH') { @values = values %$ds; } elsif(ref($ds) eq 'ARRAY') { @values = @$ds; } else { Carp::croak("can't iterate through a ".ref($ds)); } foreach my $el (@values) { if(ref($el) eq 'HASH' or ref($el) eq 'ARRAY') { my $key = "#".$self->{info}->{depth}; push @{$self->{info}->{$key}}, ref($el); $self->{info}->{depth}++; push @{$self->{info}->{$key}}, $self->_traverse($el); $self->{info}->{depth}--; } } return ref $ds; } sub _collapse_info { my $self = shift; my $str = substr($self->{'ref'}, 0, 1); for my $lvl (sort grep /^#\d+/, keys %{$self->{info}}) { my %uniq = (); $uniq{$_}++ for @{$self->{info}->{$lvl}}; my @type = keys %uniq; if(@type > 1) { Carp::carp("data is not homogeneous at depth $lvl"); $str .= '['.join("o", map { substr($_, 0, 1) } @type).']'; } else { $str .= 'o' . substr($type[0], 0, 1); } } return $str; } qq(and I'm spent); package main; use Data::AsString; my $data = { foo => { one => [qw(x y z)] }, bar => { two => [qw(a b c)] } }; my $sig = Data::AsString->new($data); print "signature of \$data is $sig\n";