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

hi, i am just a perl beginner. I need to comapre two folders(directories) having xml files and show up the difference, I unable to do that. When I run the below code i get error saying: LWP Request Failed at C:/Perl/site/lib/XML/SAX/PurePerl/Reader/URI.pm

Below is the code:
use XML::Simple; use XML::SemanticDiff; $xml1 = new XML::Simple; $xml2 = new XML::Simple; $dirtoget1="folder1"; $dirtoget2="folder2"; opendir(IMD1, $dirtoget1) || die("Cannot open directory"); opendir(IMD2, $dirtoget2) || die("Cannot open directory"); while( ($filename1 = readdir(IMD1)) && ($filename2 = +readdir(IMD2))) { $file =$xml1->XMLin("$filename1"); $file2 =$xml2->XMLin("$filename2"); my $diff = XML::SemanticDiff->new(); foreach my $change ($diff->compare($filename1, $filename2 +)) { print "$change->{message} in context $change-> +{context}\n"; } # or, if you want line numbers: my $diff = XML::SemanticDiff->new(keeplinenums => 1); foreach my $change ($diff->compare($file, $file2)) { print "$change->{message} (between lines $chan +ge->{startline} and $change->{endline})\n"; } print("$filename1\n"); print("$filename2\n"); print("\n \n \n \n"); } closedir(IMD1); closedir(IMD2);

Replies are listed 'Best First'.
Re: error showing LWP request failed
by kcott (Archbishop) on Nov 25, 2010 at 08:45 UTC

    You have my $diff twice in the same scope.

    You'll be alerted to that (and other problems) by adding these lines to the top of your code:

    use strict; use warnings;

    As you're a beginner, you can get more verbose messages by also adding:

    use diagnostics;

    Perl can also provide details of any problems with opening those directories if you add $! to your error message - see readdir for example usage.

    Additional Information:

    You might also consider using XML::Simple STRICT MODE. The documentation says: "the following common mistakes will be detected and treated as fatal errors: ...".

    -- Ken