What do you mean by "fails?" It would be helpful if you showed us what your expected output is and where the actual output differs. To me it looks like your code is working just fine. To get a better sense of what's actually going on, you might try replacing
foreach my $value (values %data){
print "$value\n";
}
with something more like
foreach my $value (values %data) {
if (ref $value ne "HASH") {
print "$value\n";
} else {
print "$_\n" foreach values %$value;
}
}
The point being, that when creating your main hash, some of your values are strings, others are hash references. That's the way your code is designed, so you'll need to change your handling routines to accomodate it.