my $hierarchy = get_hierarchy(@dirs); while ( my ( $directory, $contents ) = each %$hierarchy ) { ... # do your thing here }