Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: diff vs diff -y

by roboticus (Chancellor)
on Oct 19, 2018 at 17:53 UTC ( [id://1224353]=note: print w/replies, xml ) Need Help??


in reply to diff vs diff -y

MissPerl:

While awaiting responses on my question, I whipped this up. You could use it as a base to work from:

$ cat ~/bin/diffident_html.pl #!env perl # # diffident_html.pl <FName> <FName> # # Build a dumb HTML table showing the diff (via Algorithm::Diff) of tw +o files # use strict; use warnings; use Algorithm::Diff; my $LFName = shift // die "Expected two file names!"; my @lfile = readfile($LFName); my $RFName = shift // die "Expected *TWO* file names!"; my @rfile = readfile($RFName); my $diff = Algorithm::Diff->new( \@lfile, \@rfile ); $diff->Base(1); print qq{<html><body><table border="1">\n}; print qq{<tr><th colspan="2" align="left" bgcolor="#c0c0c0">$LFName</t +h>} . qq{<th colspan="2" align="left" bgcolor="#c0c0c0">$RFName</t +h></tr>\n}; print qq{<tr><th width="1%" align="right" bgcolor="#c0c0c0">#</th>} . qq{<th width="49%" align="left" bgcolor="#c0c0c0">Source</th +>} . qq{<th width="1%" align="right" bgcolor="#c0c0c0">#</th>} . qq{<th width="49%" align="left" bgcolor="#c0c0c0">Source</th +></tr>\n}; while ($diff->Next()) { if ($diff->Same()) { # Left and Right file have a block of identical lines print qq{<tr><td colspan="4" align="center">.&nbsp;.&nbsp;.&nb +sp;. } . qq{ block of matching lines .&nbsp;.&nbsp;.&nbsp;.</td>< +/tr>\n}; next; } my ($min1, $min2) = $diff->Get(qw( Min1 Min2 )); my @L = $diff->Items(1); my @R = $diff->Items(2); while (@L or @R) { print qq{<tr>}; if (@L) { print qq{<td align="right">$min1</td><td align="left">$L[0 +]</td>}; ++$min1; shift @L; } else { print qq{<td colspan="2" bgcolor="#c0c0c0">}; } if (@R) { print qq{<td align="right">$min2</td><td align="left">$R[0 +]</td>}; ++$min2; shift @R; } else { print qq{<td colspan="2" bgcolor="#c0c0c0">}; } print qq{</tr>\n}; } } print qq{</table></body></html>\n}; sub readfile { my $fname = shift; open my $FH, '<', $fname or die "Can't open file $fname: $!\n"; return <$FH>; }

I simply took an Anonymous Monk's suggestion to use Algorithm::Diff to get the differences between two files, and from that glued the data together into an HTML table. If you wanted to *really* use it, you'd probably want to use a template handling package, work through the error cases, etc.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: diff vs diff -y
by MissPerl (Sexton) on Oct 22, 2018 at 13:29 UTC
    Thank you @roboticus, wow algorithm::diff definitely new to me, and it seems pretty effective!

    That's a lot for me to learn in this code ! It's definitely useful as a newbie like me !

    I love Perl more now !

    I would like to know why is readfile sub is used for ?

    Why can't just use declaration in below?
    my $LFName = shift // die "Expected two file names!"; my @lfile = readfile($LFName); my $RFName = shift // die "Expected *TWO* file names!"; my @rfile = readfile($RFName);
    Since it's receiving input this way, I would like to ask,

    what is it's for remote files in remote server ?

    Will it be any difference ? Still using the same to readfile() ?

      MisPerl:

      The readfile subroutine has two purposes: First it abstracts away the fetching of the data that we'll compare with Algorithm::Diff, and secondly to reduce (even if only a little) code duplication.

      By replacing the readfile() routine, you can source the data from wherever you like, without impacting the diff table generator. The way it's currently written, it'll just read whatever file you specify on the local machine. But you can rewrite readfile() to fetch from a remote server or whatever you may like. The only thing readfile() needs to return is the data you want to take the difference of.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-26 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found