http://qs321.pair.com?node_id=11113439


in reply to Behaviour of parsed XML

Try:

my $xml = XMLin($filename, ForceArray => ['item'];

perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'

Replies are listed 'Best First'.
Re^2: Behaviour of parsed XML
by dalgetty (Acolyte) on Feb 26, 2020 at 16:17 UTC

    Wow.

    cavac

    That just worked. Like a turnaround jumpshot. Thanks to everybody for your help, it's all good now...

      It might solve this particular issue, but in general moving away from XML::Simple will serve you better long term. The module is conceptually broken. It treats XML as being basically weird-looking JSON. But XML's data model is entirely different than JSON's. Not going to get into a debate about which data model is better, but treating them the same is like writing flight-planning software that will only generate flight routes that follow highways.

      Somebody else recommended XML::Rules, which is probably the easiest thing to transition to from XML::Simple. It still allows you to treat the XML like glorified JSON, but requires you to give it rules about how to translate between the models, with some sane defaults.

      The other thing I'd recommend looking at is XML::LibXML, which provides a full DOM API for XML, which will be pretty familiar if you've done any client-side Javascript programming.

        Well, that really depends on what you are trying to do. I'm using XML::Simple as my config parser for most of my projects reliably for over a decade.

        Would i use XML::Simple to work with Wikipedia exports? Heck no, that would be a major pain.

        Would i use XML::LibXML to parse a simple config file? Heck no, this would be total overkill and would waste a lot of developer time.

        The right tool for the right job. You know, TIMTOWTDI

        perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'