in reply to hashref with(out) arrays

Thank you all for the replies! The use of ref is something I have not ever used before but it is something I will be using more going forward.

I have been using XML::Simple but did not know about the ForceArray which would definitely have made past projects much easier and will also use it in combination with ref.

I have not looked at XML::Rules before but I will give it a glace as well.

Replies are listed 'Best First'.
Re^2: hashref with(out) arrays
by haukex (Archbishop) on Nov 21, 2017 at 15:36 UTC

    I have to caution against XML::Simple, it is really only still useful for reading very simple XML files, and its output is often unreliable - exactly the issue you were asking about in the root node may pop up again later, and often in a way that you can't get rid of it with a configuration option. And I have to very strongly recommend against using that module for any kind of XML writing for the same reason. XML::Simple's own documentation recommends against its use (see the section "Status of this Module"), and see also XML::Simple needs to go!

    I have successfully used XML::Rules as a replacement for XML::Simple several times, for example as I showed here. Once you get into how to configure it, you can produce data structures that look like the output of XML::Simple, but are much more robust (Update: their generation and layout, not the structures themselves). I have noticed that unfortunately the module is not perfect when writing/round-tripping XML files.

    Although I haven't worked with it much myself, another module is XML::Compile, which is useful if you have an XML Schema for your XML. The module takes a little bit of setting up, but once it's working, AFAICT so far it seems to be quite reliable.

    And in general there are lots of other good XML processing modules, for example XML::LibXML and XML::Twig are two good ones. The above two just help in producing Perl data structures similar to what XML::Simple does.

      If you plan to roundtrip and need the result to look like the original (except for the changed data of course) you may have to resort to using the "raw" and "raw extended" rules and/or use a data structure that maps to the right XML. The data structure produced by the module doesn't keep any extra information about the XML it results from and the XML produced from a data structure is the "least complex one that can hold the data". You have to be a bit careful to roundtrip successfully. :-)

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.