I find that XML::Parser works really well for parsing XML files. The problem is that if I try to use XML::Parser from within a Perl Class, then it cannot alter any of the fields of that class.

Generally the first argument passed to functions in Perl classes is an instance of the class (basically a 'this' pointer).

However when using XML:Parser, I define a series of handler functions, which are passed to the parser object during construction, like so:
sub parse() { my ($currentObject) = @_; my $parser = new XMLParser(Handlers => { Init => \&handleDocumentInit, Start => \&handleStartTag, End => \&handleEndTag, Char => \&handleCharacterStrings, Default => \&handleDefault,}); $parser->parse($currentObject->{xmlAsScalar}); }


And these handler functions are NOT passed an instance of the current object, but are passed an instance of expat, the underlying XML parsing engine. As such, when I have finished parsing a tag, I cannot add its information to the variables of the current object, because I have no reference to the current object. My only option appears to be adding them to a variable with a package scope. (i.e. if my package is MyXMLProcessor, then I can store the results in %MyXMLProcessor::xmlTagContainer or somesuch)

The problem arises in that my code could get called in rapid succession in parallel. Could the two copies of %MyXMLProcessor::xmlTagContainer overwrite each other and cause issues? My scripts are sitting on an Apache HTTP Server, and are started by HTTP Requests from an agent external to the server.

My other idea is to have a package level hash, with a key that is the Expat instance, then each instance of Expat (which should be unique) gets its own copy of the container variable to fill. Is this idea legit?

And my last alternative is to either use a different XML Parser, or to override the functionality of this one to accept an additional parameter, the current object. Any suggestions?

In reply to XML::Parser Object Orientation problem by shug94

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.