File: Diff.pm

package info (click to toggle)
libhash-diff-perl 0.010-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 108 kB
  • sloc: perl: 88; makefile: 2
file content (115 lines) | stat: -rw-r--r-- 2,303 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package Hash::Diff;

use strict;
use warnings;
use Carp;
use Hash::Merge;

use base 'Exporter';
use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);

$VERSION     = 0.010;
@EXPORT_OK   = qw( diff left_diff );

sub left_diff {
    my ($h1, $h2) = @_;
    my $rh = {}; # return_hash
    
    foreach my $k (keys %{$h1}) {
        if (not defined $h1->{$k} and exists $h2->{$k} and not defined $h2->{$k}) {
            # Empty
        }
        elsif (ref $h1->{$k} eq 'HASH') {
            if (ref $h2->{$k} eq 'HASH') {
                my $d = left_diff($h1->{$k}, $h2->{$k});
                $rh->{$k} = $d if (%$d);
            }
            else {
                $rh->{$k} = $h1->{$k}                
            }
        }
        elsif ((!defined $h1->{$k})||(!defined $h2->{$k})||($h1->{$k} ne $h2->{$k})) {
            $rh->{$k} = $h1->{$k}
        }
    }
    
    return $rh;

}

sub diff {
    my ($h1, $h2) = @_;

    return Hash::Merge::merge(left_diff($h1,$h2),left_diff($h2,$h1));
}


1;

__END__

=head1 NAME

Hash::Diff - Return difference between two hashes as a hash

=head1 SYNOPSIS

    use Hash::Diff qw( diff );
    my %a = ( 
		'foo'    => 1,
	    'bar'    => { a => 1, b => 1 },
	);
    my %b = ( 
		'foo'     => 2, 
		'bar'    => { a => 1 },
	);

    my %c = %{ diff( \%a, \%b ) };
    
    # %c = %{ foo => 1, bar => { b => 1} }

=head1 DESCRIPTION

Hash::Diff returns the difference between two hashes as a hash.

=over 

=item diff ( <hashref>, <hashref> )

Diffs two hashes.  Returns a reference to the new hash.

=item left_diff ( <hashref>, <hashref> )

Returns the values in the left hash that is not, or different from the right hash.

=back

=head1 CAVEATS

This will not handle self-referencing/recursion within hashes well.  
This will only handle HASH and SCALAR. 

Plans for a future version include incorporate deep recursion protection.
And support for ARRAY.

=head1 BUGS

Sure!
Report here: http://rt.cpan.org/NoAuth/Bugs.html?Dist=Hash::Diff

=head1 AUTHOR

Bjorn-Olav Strand E<lt>bo@startsiden.noE<gt>

=head1 CONTRIBUTOR

Charles McGarvey E<lt>ccm@cpan.orgE<gt>

=head1 COPYRIGHT

Copyright (c) 2010 ABC Startsiden AS. All rights reserved.

This library is free software.  You can redistribute it and/or modify it 
under the same terms as Perl itself.

=cut