Nice!

BTW, the idea behind this module is not just laziness. Yes, as a Lazy Perl programmer I saw no need to have one line of extra code per element that could be avoided. There's another issue: This code is much more fit for my server.

XML::Writer generates code on the fly, that's nice and efficent, but inadequate for my needs. In my server, which uses XML I have basically this:

my $root = new XML::Maker("data"); $parser->parse("message"); send_to_client($root->make());

Here's how it works: The client sends one or more commands inside <data>. I make a <data> element, and start parsing, generating XML during the process. Then I send the whole reply at once.

With XML::Writer there's the problem of that <data></data> will be sent if there's no output because it's impossible to determine whether XML has been generated or not. In my case, however, it's trivial to count the number of children.

Another issue is rolling back XML generation. My parser works by calling callbacks for every registered command, which then may generate XML. A command might generate a tree and send it to the client. But, what happens when it's in the process of generating the tree and something suddenly goes wrong?

XML::Writer will have already sent the partial output to the socket. The client will get incomplete data, or very possibly, invalid XML. Since the server will continue processing and try to reply to the next command, the client might not be able to parse it. Of course I could make it close all the elements, and then append an error message, but that's still not good. Parsing goes from top to bottom, so first it'd interpret the partial results, and only then get to the error message.

My module lets me remove a subtree at any time I want. So I can start generating, remove the results I got so far, and put an error message in its place.


In reply to Re: Re: XML::Maker by vadim_t
in thread XML::Maker by vadim_t

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.