I am failing terribly to return a Hash of the Parsed XML document using twig - in order to use it in OTHER subs for performing several validation checks. The goal is to do abstraction and create re-usable blocks of code.

XML Block

<?xml version="1.0" encoding="utf-8"?> <Accounts locale="en_US"> <Account> <Id>abcd</Id> <OwnerLastName>asd</OwnerLastName> <OwnerFirstName>zxc</OwnerFirstName> <Locked>false</Locked> <Database>mail</Database> <Customer>mail</Customer> <CreationDate year="2011" month="8" month-name="fevrier" day-of-mo +nth="19" hour-of-day="15" minute="23" day-name="dimanche"/> <LastLoginDate year="2015" month="04" month-name="avril" day-of-mo +nth="22" hour-of-day="11" minute="13" day-name="macredi"/> <LoginsCount>10405</LoginsCount> <Locale>nl</Locale> <Country>NL</Country> <SubscriptionType>free</SubscriptionType> <ActiveSubscriptionType>free</ActiveSubscriptionType> <SubscriptionExpiration year="1980" month="1" month-name="janvier" + day-of-month="1" hour-of-day="0" minute="0" day-name="jeudi"/> <SubscriptionMonthlyFee>0</SubscriptionMonthlyFee> <PaymentMode>Undefined</PaymentMode> <Provision>0</Provision> <InternalMail>asdf@asdf.com</InternalMail> <ExternalMail>fdsa@zxczxc.com</ExternalMail> <GroupMemberships> <Group>werkgroep X.Y.Z.</Group> </GroupMemberships> <SynchroCount>6</SynchroCount> <LastSynchroDate year="2003" month="12" month-name="decembre" day- +of-month="5" hour-of-day="12" minute="48" day-name="mardi"/> <HasActiveSync>false</HasActiveSync> <Company/> </Account> <Account> <Id>mnbv</Id> <OwnerLastName>cvbb</OwnerLastName> <OwnerFirstName>bvcc</OwnerFirstName> <Locked>true</Locked> <Database>mail</Database> <Customer>mail</Customer> <CreationDate year="2012" month="10" month-name="octobre" day-of-m +onth="10" hour-of-day="10" minute="18" day-name="jeudi"/> <LastLoginDate/> <LoginsCount>0</LoginsCount> <Locale>fr</Locale> <Country>BE</Country> <SubscriptionType>free</SubscriptionType> <ActiveSubscriptionType>free</ActiveSubscriptionType> <SubscriptionExpiration year="1970" month="1" month-name="janvier" + day-of-month="1" hour-of-day="1" minute="0" day-name="jeudi"/> <SubscriptionMonthlyFee>0</SubscriptionMonthlyFee> <PaymentMode>Undefined</PaymentMode> <Provision>0</Provision> <InternalMail/> <ExternalMail>qweqwe@qwe.com</ExternalMail> <GroupMemberships/> <SynchroCount>0</SynchroCount> <LastSynchroDate year="1970" month="1" month-name="janvier" day-of +-month="1" hour-of-day="1" minute="0" day-name="jeudi"/> <HasActiveSync>false</HasActiveSync> <Company/> </Account> </Accounts>

Perl Block

my $file = shift || (print "NOTE: \tYou didn't provide the name of the + file to be checked.\n" and exit); my $twig = XML::Twig -> new ( twig_roots => { 'Account' => \& parsing +} ); #'twig_roots' mode builds only the required sub-trees from the d +ocument while ignoring everything outside that twig. $twig -> parsefile ($file); sub parsing { my ( $twig, $accounts ) = @_; my %hash = @_; my $ref = \%hash; #because was getting an error of Odd number of h +ash elements return $ref; $twig -> purge; }

It gives a hash reference - which I'm unable to deference properly (even after doing thousands of attempts). Again - just need a single clean function (sub) for doing the Parsing and returning the hash of all elements ('Accounts' in this case) - to be used in other other function (valid_sub) for performing the validation checks. I'm literally stuck at this point - and will HIGHLY appreciate your HELP.

POSTED at StackOverflow as well - for the sole reason of having multiple answers in order to get the desired result.


In reply to Need an hash of the Parsed Document ! by Mr.Mick

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.