Subject: | Don't use _flatten for HASH_of_scalars |
Reading the diff of two HASH_of_scalars variables is actually quite difficult. Here's a sample script:
#!perl
use strict;
use Test::More 'no_plan';
use Test::Differences;
my $force_dumpit = shift;
my $expected = {
'id' => 1828781669,
'name' => 'Paul',
'city' => 'Cairo',
'country' => 'USA',
'version' => 1,
($force_dumpit ? ('DUMMY'=>[]) : ()),
};
my $got = {
'id' => 1828781670,
'name' => 'Paul',
'city' => 'Cairo',
'country' => 'USA',
'version' => 1,
($force_dumpit ? ('DUMMY'=>[]) : ()),
};
eq_or_diff $got, $expected;
__END__
The output is:
# +----+----------------------------------------+----------------------------------------+
# | Elt|Got |Expected |
# +----+----------------------------------------+----------------------------------------+
# | 0|'city','country','id','name','version' |'city','country','id','name','version' |
# * 1|'Cairo','USA',1828781670,'Paul',1 |'Cairo','USA',1828781669,'Paul',1 *
# +----+----------------------------------------+----------------------------------------+
Finding the difference here is quite difficult, and more difficult if the hashes are larger.
It's much easier to spot the difference if the comparison is done using Data::Dumper (here forced by adding a dummy pair with a non-scalar value):
# +----+---------------------+---------------------+
# | Elt|Got |Expected |
# +----+---------------------+---------------------+
# | 0|{ |{ |
# | 1| DUMMY => [], | DUMMY => [], |
# | 2| city => 'Cairo', | city => 'Cairo', |
# | 3| country => 'USA', | country => 'USA', |
# * 4| id => 1828781670, | id => 1828781669, *
# | 5| name => 'Paul', | name => 'Paul', |
# | 6| version => 1 | version => 1 |
# | 7|} |} |
# +----+---------------------+---------------------+
I am not sure if there are situations where the output created by _flatten() is easier to read.
Regards,
Slaven