Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl Need to Compare Two Data Structures and Return Differences

I have two data structures with a mix of hashes and arrays. How can I compare the two data structures and return their differences, something like perl's Test::Harness module, but I don't want to actually run a unit test. ...or is there a way to tun Test::Harness without actually running a unit test?

like image 438
Paul Avatar asked Mar 09 '26 19:03

Paul


2 Answers

Perl Monks says that Test::Deep, Data::Compare, and Data::Match are your friends. Those packages don't seem to be geared towards producing detailed diffs but you might be able to hack in a callback to keep track of the precise differences.

like image 70
mu is too short Avatar answered Mar 12 '26 17:03

mu is too short


Test::Deep::NoTest (from Test::Deep) might be what you are looking for, giving the functions of Test::Deep outside a test script (eq_deeply, cmp_deeply, etc). Look at using deep_diag() to see what the differences are.

Data::Compare - also gives functions returning boolean responses (rather like using cmp on the command line for files), but (from memory) is harder to return what those differences are.

I used the former most recently, probably to get the deep_diag() details that Data::Compare didn't provide, but I haven't tried parsing the response.

like image 21
plusplus Avatar answered Mar 12 '26 18:03

plusplus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!