views:

48

answers:

2

I'm using XML::Simple to parse an XML file which I then want to use to write an output file in a very specific format. Thus, the output order is important.
As I understand it, when the XML is converted to the perl hashref the order is lost (because perl hashes have no order). But what about when an array is used by XML::Simple.

For example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <price>10.0</price>
  </cd>
  <cd>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <price>9.99</price>
  </cd>
  <cd>
    <title>Hello</title>
    <artist>Say Hello</artist>
    <price>0001</price>
  </cd>
</catalog>

gives us a data structure resembling:

$VAR1 = {
      'cd' => [
              {
                'artist' => 'Bonnie Tyler',
                'price' => '10.0',
                'title' => 'Hide your heart'
              },
              {
                'artist' => 'Dolly Parton',
                'price' => '9.99',
                'title' => 'Greatest Hits'
              },
              {
                'artist' => 'Say Hello',
                'price' => '0001',
                'title' => 'Hello'
              }
            ]
    };

The 3 'cd' structures get inserted into an array, so is their order always going to be the same as they were in the input file?

A: 

On input, yes. Using arrays will keep them in order. I haven't found a way to keep output ordered in many cases using XML::Simple.

Axeman
I don't care about the other tags, as long as the array maintains it's order.
curtisvo
@curtisvo: "On input, yes."
Axeman
+1  A: 

It's not guaranteed to be in order, in a sense of it's not documented anywhere in POD or the FAQ for XML::Simple that I ever saw.

However, since re-arranging the order of an array is always more work - both for the developer and the computer - than keeping it - you can reasonably assume that order changing would be VERY unlikely.

The ONLY way to be sure would be to:

  1. Look at the source code for XML::Simple and ensure it works this way. This may be a bit tricky as far as "XMLin"=>datastructure order maintenance since this might depend on which parser is used underneath by XML::Simple (that's configurable); I don't know enough about the XML::Simple code to be sure that array population is parser independent.

    But as per my logic above, it'd be HARDER to rearrange the order than not to rearrange it.

  2. Convince the maintainer to make it a declared feature that's contracted via POD and add a unit test to ensure that.

DVK
Guaranteed may have been a strong word; I think very unlikely is pretty good. As you've said, there would be no reason to put the extra work in to reorder the array. I'll take a look at the source just to see if anything glaring jumps out.
curtisvo
The perl-doc states "the second address on the server 'kalahari' could be referenced as: $config->{server}->{kalahari}->{address}->[1];". This may not be strictly guaranteed, but if the doc suggests that this is the way to refer to a specific array element that's good enough for me...
curtisvo