PiyaPerl has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks, I have two XML files say $file1 and $file2. I need to find a way to show the difference between them and highlight and format them in such a way that they can be easy to view. That difference then needs to be a mail attachment. I am unable to figure out how to do the same in perl5.8 version. Kindly help me with some information.

Replies are listed 'Best First'.
Re: XML file difference highlights
by hippo (Archbishop) on Jun 14, 2017 at 08:08 UTC

    Sounds like you need XML::DifferenceMarkup and one or more of the many, many email composition and sending modules.

    format them in such a way that they can be easy to view

    This suggests that you don't yet have a real spec, since "easy to view" is very subjective. As ever, the process should be:

    1. Detailed spec
    2. Algorithm
    3. Tests
    4. Code

    (although you might promote Tests to 2 if you fancy it)

      See also comparing XML files.

      I still use the following bash script to compare XML files and usually find its output informative enough, if not more readable than various XML oriented diff tools.

      #!/bin/bash a=($@) b=$# f2=${a[$((--b))]} f1=${a[$((--b))]} diff "${a[@]:0:$b}" \ <(xmllint --c14n "$f1" |xmllint --format -) \ <(xmllint --c14n "$f2" |xmllint --format -)

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: XML file difference highlights
by Corion (Patriarch) on Jun 14, 2017 at 08:04 UTC

    Which part do you have problems with?

    There is XML::Diff and/or Algorithm::Diff, depending on what kind of diff you need for your XML.

    For mailing, there is MIME::Lite, which makes it easy to send attachments.

      I am getting this error: Can't locate XML/Diff.pm in @INC upon using module XML:Diff ?

        This isn't a core module, so you have to install it:

        cpan XML::Diff or cpanm XML::Diff
          A reply falls below the community's threshold of quality. You may see it by logging in.