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;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|