Perl module to deal with numbered nested objects I wrote a small perl module that dealing with PHP-ish array structures, and I'm going to release it to CPAN.

I want it to be reviewed before the release about the module naming and the code itself.

Could you please give me improvements?  Any suggestions are appreciated.

https://github.com/ernix/p5-Object-Squash

use strict;
use warnings;
package Object::Squash;
# ABSTRACT: Remove numbered keys from a nested object

use parent 'Exporter';
use List::Util qw/max/;

use version; our $VERSION = version-&gt;declare("v0.0.1");

our @EXPORT_OK = qw(squash);

sub squash {
    my $obj = shift;
    return $obj unless ref $obj;

    $obj = _squash_hash($obj);
    $obj = _squash_array($obj);

    return $obj;
}

sub _squash_hash {
    my $obj = shift;
    return $obj unless ref $obj eq 'HASH';

    my @keys = keys %{$obj};

    if (grep {/\D/} @keys) {
        return +{
            map { $_ =&gt; squash($obj-&gt;{$_}) } @keys,
        };
    }

    my $max = max(@keys) || 0;

    my @ar;
    for my $i (0 .. $max) {
        push @ar, sub {
            return (undef) unless exists $obj-&gt;{$i};
            return squash($obj-&gt;{$i});
        }-&gt;();
    }

    return \@ar;
}

sub _squash_array {
    my $obj = shift;
    return $obj unless ref $obj eq 'ARRAY';

    return (undef) if @{$obj} == 0;
    $obj = squash($obj-&gt;[0]) if @{$obj} == 1;

    return $obj;
}

1;
__END__

=head1 NAME

Object::Squash - Remove numbered keys from a nested object

=head1 DESCRIPTION

This package provides B&lt;squash&gt; subroutine to simplify hash/array structures.

I sometimes want to walk through a data structure that consists only of a bunch
of nested hashes, even if some of them should be treated as arrays or single
values.  This module removes numbered keys from a hash.

=head1 SYNOPSIS

=head2 C&lt;squash&gt;

    use Object::Squash qw(squash);
    my $hash = squash(+{
        foo =&gt; +{
            '0' =&gt; 'nested',
            '1' =&gt; 'numbered',
            '2' =&gt; 'hash',
            '3' =&gt; 'structures',
        },
        bar =&gt; +{
            '0' =&gt; 'obviously a single value',
        },
    });

$hash now turns to:

    +{
        foo =&gt; [
            'nested',
            'numbered',
            'hash',
            'structures',
        ],
        bar =&gt; 'obviously a single value',
    };

=head1 AUTHOR

Shin Kojima &lt;shin@kojima.org&gt;

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
Just a few remarks about parts which poke the eye,

    push @ar, sub {
        return (undef) unless exists $obj-&gt;{$i};
        return squash($obj-&gt;{$i});
    }-&gt;();


introduces unnecessary subroutine call/overhead which can be replaced with do {} block, or even simpler,

    push @ar, exists $obj-&gt;{$i} ? squash($obj-&gt;{$i}) : undef;


This one is only matter of taste, so

return $obj unless ref $obj eq 'HASH';


could also be written as

return $obj if ref $obj ne 'HASH';


Usually you don't have to explicitly return undef, as in list context you may end up with list one element long, and that might not be what is intended. Instead just return; or return (); will produce empty list, or undef in scalar context. So

return (undef) if @{$obj} == 0;


could be better as

return () if @{$obj} == 0;


or perhaps,

@$obj or return;