How do I compare two hashes in Perl without using Data::Compare?

后端 未结 7 1451
春和景丽
春和景丽 2020-12-10 03:08

How do I compare two hashes in Perl without using Data::Compare?

7条回答
  •  心在旅途
    2020-12-10 03:33

    Quick, dirty, and I'm sure not that efficient:

    use strict;
    use warnings;
    
    use Data::Dumper;
    
    sub compare ($$) {
        local $Data::Dumper::Terse  = 1;
        local $Data::Dumper::Indent = 0;
        Dumper(shift) eq Dumper(shift);
    }
    
    my %a = ( foo => 'bar', bar => [ 0 .. 3 ] );
    my %b = ( foo => 'bar', bar => [ 0 .. 3 ] );
    my %c = ( foo => 'bar', bar => [ 0 .. 4 ] );
    
    print Dumper compare \%a, \%b;
    print Dumper compare \%a, \%c;
    

提交回复
热议问题