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

Anyone experienced any problems with the XML::Dumper module? I have previously used XML::Simple to persistently store data, but was hoping to use XML::Dumper as it looked to be even simpler (less configuration options).

But, while I could make the pl2xml method work fine, I am unable to get it to convert the XML output back into a perl structure.

Here's a simplification of the code I'm using:

use XML::Dumper; my $xml; my %config = ('a key'=>'a value', 'another key'=>'another value'); my $dumper = XML::Dumper->new(); $xml = $dumper->pl2xml(\%config); print "xml output: ", $xml; my $config = $dumper->xml2pl($xml); die "Uh oh" unless defined $config; exit;

The above code prints out a snippet of xml just fine, but dies because the return from the call to xml2pl is undefined. I was under the impression it would return a reference to a perl data structure. Why does this method not reverse the process - I'm not even getting any indication of why it's failing!

Any help appreciated.

Replies are listed 'Best First'.
(jeffa) Re: XML::Dumper
by jeffa (Bishop) on Jan 20, 2002 at 23:54 UTC
    I am starting to think that this is a bug in XML::Dumper. I followed the Perl debugger and found this oddity:
    ... main::(foo.pl:16): $config = $dumper->xml2pl($xml); DB<1> s XML::Dumper::xml2pl(.../XML/Dumper.pm:114): 114: my ($obj,$tree) = @_; DB<1> n XML::Dumper::xml2pl(.../XML/Dumper.pm:117): 117: my $TopItem = $tree->[1]; DB<1> n XML::Dumper::xml2pl(.../XML/Dumper.pm:118): 118: my $ref = &Undump($TopItem); DB<1> s XML::Dumper::Undump(.../XML/Dumper.pm:133): 133: my ($Tree) = shift; DB<1> n XML::Dumper::Undump(.../XML/Dumper.pm:134): 134: my $ref = undef; DB<1> n XML::Dumper::Undump(.../XML/Dumper.pm:135): 135: my $FoundScalar; DB<1> n XML::Dumper::Undump(.../XML/Dumper.pm:136): 136: my $i; DB<1> n XML::Dumper::Undump(.../XML/Dumper.pm:138): 138: for ($i = 1; $i < $#$Tree; $i+=2) { DB<1> n XML::Dumper::Undump(.../XML/Dumper.pm:178): 178: $ref = $FoundScalar unless defined($ref); DB<1> n XML::Dumper::Undump(/.../XML/Dumper.pm:180): 180: done: 181: return ($ref); DB<1> n XML::Dumper::xml2pl(/.../XML/Dumper.pm:120): 120: return($ref); DB<1> n main::(foo.pl:21): print Dumper $config;
    The problems i see are on lines 117 and 138 of XML/Dumper.pm. The code expects the argument to xml2pl to be an array ref, when it is really a scalar. The result is the for loop not being entered, and eventually an undef is returned.

    Additionally, the synopsis code is inconsistent (if not broken) and once i 'fixed' and ran it, it produced the same results as your script - undef for the return value of xml2pl.

    Personally, i'd just use XML::Simple. There maybe more configuration options to worry about, but i also think that you should worry about those config options.

    My apologies in advance to the author of XML::Dumper if i am wrong. If nobody tells me contrary within about 24 hours then i will notify the author.

    Update - i received a /msg from blakem that stated the same conclusion.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      ++jeffa.

      Thanks for confirming that I'm not the only one who can't make it work as advertised. I did briefly take a look at the .pm file to see if I could see anything obviously broken, but alas I was not. (But I also noticed the documentation was a little, er, inconsistent.) Hopefully the author will correct the problem, if it truly is a bug as jeffa's analysis points to.

      I have indeed fallen back to using XML::Simple. Thanks again.

      ... nearly one year later ...

      I received an email from the new maintainer of XML::Dumper, Mike Wong, yesterday (2002-12-09). This bug has been fixed. :) Also, Mike has added more features such as handling circular references, the ability to read and write zlib-compressed XML, and some more (check out the Changes file that ships with the tar ball for more).

      Here is the output from theguvnor's code using XML::Dumper 0.59:
      [jeffa@trinity perl]$ ./foo.pl
      xml output: <perldata>
       <hashref memory_address="0x8108634">
        <item key="a key">a value</item>
        <item key="another key">another value</item>
       </hashref>
      </perldata>
      

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
(crazyinsomniac) Re: XML::Dumper
by crazyinsomniac (Prior) on Jan 21, 2002 at 03:14 UTC