Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Compare/Diff on nested data structures

by Anonymous Monk
on Sep 29, 2009 at 09:46 UTC ( #798088=note: print w/replies, xml ) Need Help??


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
    It might be useful to use Data::Dumper as text output for each comparable data structure, and do an actual unix-diff on each. This has the limitations of 1) treating both objects and code references as opaque, 2) relying on a unix command.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://798088]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2019-04-24 17:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I am most likely to install a new module from CPAN if:
















    Results (122 votes). Check out past polls.

    Notices?