in reply to Re: Compare/Diff on nested data structures
in thread Compare/Diff on nested data structures
Below is the code to produce difference -- with help of diff(1) -- in two scalars, to be understood by a human (well, at least the ones who can read diff(1) output).
package DumpDiff; # Usage: # # use DumpDiff qw[ dump_diff ]; # print dump_diff( [ 0 .. 5 ] , [ 3 .. 12 ] , "-u -U 8"); use warnings; use strict; use Exporter 'import'; BEGIN { sub dump_diff; our @EXPORT = qw[ dump_diff ]; } use Carp qw[ croak ]; use Data::Dumper; use Fatal qw[ :void open close ]; use File::Temp; # Pass in two scalar to get the difference. Optional third parameter # is passed as is to diff(1), instead the default of "-uBb". # # diff(1) output is returned as either a list or a string based on # calling context. sub dump_diff { my ( $one , $two , $opt ) = @_; local $Data::Dumper::Sortkeys = 1; local $Data::Dumper::Indent = 1; local $Data::Dumper::Deepcopy = 1; my $oh = File::Temp->new; print $oh Dumper( $one ); close $oh; my $th = File::Temp->new; print $th Dumper( $two ); close $th; my $one_dump = $oh->filename; my $two_dump = $th->filename; $opt ||= '-uBb'; my @diff = qx{diff $opt $one_dump $two_dump}; if ( $? == 0 && !$! ) { return; } elsif ( $! ) { croak "cannot run 'diff $opt $one_dump $two_dump'': $!"; } return wantarray ? @diff : join '' , @diff ; } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Compare/Diff on nested data structures
by Anonymous Monk on Feb 25, 2014 at 17:18 UTC |