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;

In reply to Re^2: Compare/Diff on nested data structures by Anonymous Monk
in thread Compare/Diff on nested data structures by cosmicperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.