如何在Perl中比较两个哈希值,而不使用Data :: Compare?
解决方法
最好的方法根据你的目的不同. Sinan提到的FAQ项目是一个很好的资源:
How do I test whether two arrays or hashes are equal?.在开发和调试(当然编写单元测试)时,我发现
Test::More
在比较数组,散列和复杂数据结构时是有用的.一个简单的例子:
use strict; use warnings; my %some_data = ( a => [1,2,'x'],b => { foo => 'bar',biz => 'buz' },j => '867-5309',); my %other_data = ( a => [1,j => '867-5309x',); use Test::More tests => 1; is_deeply(\%other_data,\%some_data,'data structures should be the same');
输出:
1..1 not ok 1 - data structures should be the same # Failed test 'data structures should be the same' # at _x.pl line 19. # Structures begin differing at: # $got->{j} = '867-5309x' # $expected->{j} = '867-5309' # Looks like you Failed 1 test of 1.