2013-10-08 58 views
0

讓我們說,我有以下DIFF與單獨添加和刪除部分數據結構

my $clean = { 
    foo => 1, 
    bar => 2, 
    baz => 3, 
}; 


my $dirty => { 
    foo => 1, 
    bar => 3, 
    blargh => 4, 
}; 


my $differ = Differ->new; #made up object 

$differ->compare($clean, $dirty); 


$differ->added; # blargh 
$differ->removed; # baz 
$differ->updated; # bar 

以上,也許是什麼I'my試圖做(一個非常新穎的例子,我的收益可能不足夠複雜)。有沒有可以爲我做這個的模塊?

我基本上試圖找出如何計算通過改變發送到遠程服務器的內容。模塊理論上應該能夠處理包括陣列在內的複雜數據結構(不需要自引用)

+0

參見:http://stackoverflow.com/questions/4011632/perl-need-to-compare-two-data-structures-and-返回差異 – xxfelixxx

+0

您的示例代碼中存在拼寫錯誤。 '我的$髒=> {'應該'我的$髒= {'。 – Kaoru

回答

0

Data :: Diff並不完全具備您所期望的漂亮的OO接口,但它似乎可以招。

#!/usr/bin/env perl 

use strict; 
use warnings; 
use feature 'say'; 

use Data::Diff; 

my $clean = { 
    foo => 1, 
    bar => 2, 
    baz => 3, 
}; 

my $dirty = { 
    foo => 1, 
    bar => 3, 
    blargh => 4, 
}; 

my $diff = Data::Diff->new($clean, $dirty)->raw(); 

say "Added: ", join(", ", sort keys %{ $diff->{uniq_b} }); 
say "Removed: ", join(", ", sort keys %{ $diff->{uniq_a} }); 
say "Updated: ", join(", ", sort keys %{ $diff->{diff} }); 

其中按您的輸出示例:

[email protected]:~$ ./data_diff.pl 
Added: blargh 
Removed: baz 
Updated: bar