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

I ask your help monks - I am increasingly starting to dislike XML::Simple, as a library that actively promotes bad quality code. It's "discouraged" in its documentation.

It does pop up rather a lot on Stack Overflow. I think this is because it has "Simple" in the title, and the people using it don't realise that it's lying.

I'm of the opinion that there is simply no good reason to ever use this module.

However, I'm cognisant that I may be wrong - there may actually be reasons to use this module, and that's why I need your help.

I'm seeking examples where XML::Simple is a superior choice to either XML::Twig or XML::LibXML. Ideally on the basis of cleaner, nicer code. But I will accept performance or memory footprint arguments. (And if you can give me an example where it's better than _both_ then I will be particularly impressed)

My starter for 10 - if you're stuck with ActiveState, and can't install Strawberry - it's quite hard to get XML::LibXML installed and running. (Although I still think XML::Twig would be better there, even so).

With reference to: Stack Overflow: Why is XML::Simple "discouraged"?

  • Comment on Examples where XML::Simple is the optimal choice?

Replies are listed 'Best First'.
Re: Examples where XML::Simple is the optimal choice?
by Anonymous Monk on Oct 23, 2015 at 09:15 UTC

    XML::Rules obsoletes XML::Simple

    If you don't feel like learning xpath or trees or twigs , if you feel hohaohoaoho...hashes of arrays and arrays of hashes ... are what you want then use XML::Rules

      I would largely agree. I mean, XML::Twig has "simplify" which will likewise dump a data structure, which IMO means it also obseletes XML::Simple.

      What I'm trying to get to is - I'm feeling like I'm banging on saying "Just don't use XML::Simple, there are no good reasons to" and I'm after counter examples. Niches where it isn't just plain worse.

        What I'm trying to get to is - I'm feeling like I'm banging on saying "Just don't use XML::Simple, there are no good reasons to" and I'm after counter examples. Niches where it isn't just plain worse.

        You need to ask more noobs

        :) new things are scary, only noobs can come up with examples, because they like trying to write {foo}{bar} (but can't debug it)

        {foo}{bar} is better documented and more accessible to noobs than the others

        twig has too much documentation :D

        libxml docs don't teach you xpath or trees/dom or ...perlintro

        so they think "xml hash" and go searching .... after half hour of frustration they stumble upon XML::Simple

        Its why I try to give libxml examples often ... its 98% copy/paste

        Its also why I link threads where libxml/rules/simple/twig solutions are present....

        but even with all that compare/contrast, noobs like to inch through xml/hashes while simultaneously trying to learn perlintro

        they need handholding

        I used to :D

Re: Examples where XML::Simple is the optimal choice?
by Jenda (Abbot) on Oct 24, 2015 at 23:34 UTC

    The reason people choose XML::Simple over XML::LibXML is not the cleanliness of the code when already written, but rather the expected time to learn either so that you can write anything. I can't imagine anyone not feeling intimidated by the XML::LibXML docs. I just reread the main docs and, after many years of using Perl and being the author of one of the other modules already mentioned, I have no idea whatsoever how to use it or where to start looking.

    I know I'm supposed to binmode() the filehandles I pass it, I know it supports threads, I know how to print the module's version and that's it.

    I'm not surprised beginners do not use that module, I'm surprised anyone does.

    I don't think there are reasons to use XML::Simple (see Simpler than XML::Simple), but except in case you already have code using libxml in some other language and just need to transform it into Perl, I do not see a reason to learn and use XML::LibXML either.

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

Re: Examples where XML::Simple is the optimal choice?
by Anonymous Monk on Oct 23, 2015 at 11:21 UTC
    The only case where I think XML::Simple is still fine to use is reading simple configuration files whose structure has been designed to map cleanly to a Perl hash/arrayref structure. Otherwise, especially when writing XML, I agree that other XML modules are much more reliable.
      No, it's awful at that. It can't even do [ 'foo', [ 'bar' ] ]. JSON is far more optimal.
      my $config = XML::Twig -> parsefile ( 'config.xml') -> simplify;

      I suppose the XML::Simple is technically winning here, due to fewer keystrokes needed:

      my $config = XMLin ('config.xml' );

      I wonder a little about performance, but don't think there'll be much in it for the size of config we'll be talking about

Re: Examples where XML::Simple is the optimal choice?
by locked_user sundialsvc4 (Abbot) on Oct 23, 2015 at 14:10 UTC

    Ordinarily, I use either Twig or LibXML ... most often the latter.   Twig is good if the file is so gigantic that you need to process it incrementally ... it is very good for that.   But LibXML has the advantage of being all-inclusive, and of being based on an industry-standard binary library that everyone else also uses.   (The file that you are parsing by means of libxml2 is quite likely to have been built using the same library.)

      They're both good. I default to Twig, because I find it a little more accessible for the more straightforward examples. And I did a lot with it, when I couldn't get XML::LibXML installed on my ActivePerl installation. But I don't think there's much in it - the're both eminently solid libraries for most use cases.

        I agree with your endorsement of Twig ... it is very solid and does not flinch under pressure.   (It used to be important that it also ran well on small machines ... y’know, back when machines were small.)   I like LibXML because of its completeness and underpinnings.   And, well, “those are the (only) two tools that I use to do this sort of thing (in Perl).”

        A reply falls below the community's threshold of quality. You may see it by logging in.