The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

XML::Dumper - Perl module for dumping Perl objects from/to XML

SYNOPSIS

  use XML::Dumper;
  my $dump = new XML::Dumper;

  my $perl      = '';
  my $xml       = '';

  # ===== Convert Perl code to XML
  $perl = [
    {
                fname           => 'Fred',
                lname           => 'Flintstone',
                residence       => 'Bedrock'
    },
    {
                fname           => 'Barney',
                lname           => 'Rubble',
                residence       => 'Bedrock'
    }
  ];
  $xml = $dump->pl2xml($perl);

  # ===== Convert XML to Perl code
  $xml = q|
  <perldata>
   <arrayref>
    <item key="0">
     <hashref>
        <item key="fname">Fred</item>
        <item key="lname">Flintstone</item>
        <item key="residence">Bedrock</item>
     </hashref>
    </item>
    <item key="1">
     <hashref>
        <item key="fname">Barney</item>
        <item key="lname">Rubble</item>
        <item key="residence">Bedrock</item>
     </hashref>
    </item>
   </arrayref>
  </perldata>
  |;

  my $perl = $dump->xml2pl($xml);

DESCRIPTION

XML::Dumper dumps Perl data to XML format. XML::Dumper can also read XML data that was previously dumped by the module and convert it back to Perl. Perl objects are blessed back to their original packaging; if the modules are installed on the system where the perl objects are reconstituted from xml, they will behave as expected. Intuitively, if the perl objects are converted and reconstituted in the same environment, all should be well. And it is.

Another fine challenge that this module rises to meet is that it understands circular definitions. This includes doubly-linked lists, circular references, and the so-called 'Flyweight' pattern of Object Oriented programming. So it can take the gnarliest of your perl data, and should do just fine.

FUNCTIONS AND METHODS

  • new - XML::Dumper constructor.

    Creates a lean, mean, XML dumping machine. It's also completely at your disposal.

  • pl2xml -

    Converts Perl data to XML

    Usage: See Synopsis

  • xml2pl -

    Converts XML to a Perl datatype. If this method is given a second argument, XML::Dumper will use the second argument as a callback (if possible).

    Currently, the only supported invocation of callbacks is through soft references. That is to say, the callback argument ought to be a string that matches the name of a callable method for your classes. If you have a congruent interface, this should work like a peach. If your class interface doesn't have such a named method, it won't be called. The null-string method is not supported, because I can't think of a good reason to support it. (OK, that was lame, but that kind of thinking makes my head hurt. If you can prove that null-string methods ought to be allowed, I'll do it. If you don't know what the null-string method is, curse The Damian for having invoked such a beast, and move along in blissful ignorance).

  • xml_compare - Compares xml for content

    Compares two dumped Perl data structures (that is, compares the xml) for identity in content. Use this function rather than perl's built-in string comparison, especially when dealing with perl data that is memory-location dependent (which pretty much means all references). This function will return true for any two perl data that are either deep clones of each other, or identical. This method is exported by default.

  • xml_identity - Compares xml for identity

    Compares two dumped Perl data structures (that is, compares the xml) for identity in instantiation. This function will return true for any two perl data that are identical, but not for deep clones of each other. This method is also exported by default.

BUGS AND DEPENDENCIES

This module is guilty of several cardinal sins, not the least of which is a change of API.

XML::Dumper has changed API since 0.4. While this violates most every benefit of object-oriented programming, I felt it was necessary, as the functions simply didn't work as advertised. That is, xml2pl really didnt accept xml as an argument; what it wanted was an XML Parse tree. To correct for the API change, simply don't parse the XML before feeding it to XML::Dumper.

XML::Dumper also has no understanding of typeglobs (references or not), references to regular expressions, or references to Perl subroutines. If the whim strikes me, or if someone needs this feature, I may fix this. Turns out that Data::Dumper doesn't do references to Perl subroutines, either, so at least I'm in somewhat good company.

XML::Dumper requires two perl modules, both available from CPAN

        XML::Parser

XML::Parser itself relies on Clark Cooper's Expat implementation in Perl, which in turn requires James Clark's expat package itself. See the documentation for XML::Parser for more information.

REVISIONS

0.55 Removed documentation of non-implemented code, fixed MANIFEST errors. Fixed false dependency on Data::Dumper.

0.54 Added ability to handle soft referenced callbacks

0.53 Added ability to handle circular references

0.50 Added ability to dump and undump objects

0.40 I (Mike Wong) downloaded XML::Dumper from CPAN

CURRENT MAINTAINER

Mike Wong <mike_w3@pacbell.net>

ORIGINAL AUTHOR

Jonathan Eisenzopf <eisen@pobox.com>

CREDITS

Chris Thorman <ct@ignitiondesign.com>

L.M.Orchard <deus_x@pobox.com>

DeWitt Clinton <dewitt@eziba.com>

SEE ALSO

perl(1), XML::Parser(3).